connected api with card balance and and requisites
This commit is contained in:
1 parent
7dfac2bd59
commit
d8e903f24c
24 files changed
+2540
-2628
No files matched your search
+65
-26
@@ -24,7 +24,7 @@ import { useInfiniteRoles } from "@/features/roles/hooks/useRoles";
|
||||
import { useInfinitePermissions } from "@/features/permissions/hooks/usePermissions";
|
||||
import { useInfiniteBranches } from "@/features/shared/hooks/useSharedBranchUsers";
|
||||
import { useInfiniteStaff } from "@/features/operators/hooks/useOperators";
|
||||
import { useAssignRoleToUser, useAssignPermissionToUser, useAssignBranchToUser } from "@/features/shared/hooks/useSharedUserAssignments";
|
||||
import { useAssignRoleToUser, useAssignPermissionToRole, useAssignBranchToUser } from "@/features/shared/hooks/useSharedUserAssignments";
|
||||
import type { LocalizedText } from "@/features/branches/api/branchesApi";
|
||||
|
||||
type AssignDialogKind = "role" | "permission" | "branch";
|
||||
@@ -43,6 +43,7 @@ export default function ProfilePage() {
|
||||
const lang = (i18n.language === "ru" ? "ru" : i18n.language === "en" ? "en" : "tm") as keyof LocalizedText;
|
||||
|
||||
const branches = user?.branches ?? [];
|
||||
const permissions = user?.permissions ?? [];
|
||||
|
||||
// ── Form State for Editing ──
|
||||
const [formData, setFormData] = useState({
|
||||
@@ -233,7 +234,22 @@ export default function ProfilePage() {
|
||||
</InfoRow>
|
||||
|
||||
<InfoRow label={t("users.sections.permissions", "Rugsatlar")}>
|
||||
<span className="text-sm text-muted-foreground">—</span>
|
||||
{permissions.length > 0 ? (
|
||||
<div className="flex flex-wrap gap-1.5">
|
||||
{permissions.map((p) => (
|
||||
<Link
|
||||
key={p.id}
|
||||
to={`/settings/users/permissions/${p.id}`}
|
||||
className="inline-flex items-center gap-1 text-xs border border-border rounded-md px-2 py-1 hover:bg-muted transition-colors"
|
||||
>
|
||||
<KeyRound size={11} className="text-muted-foreground" />
|
||||
<span className="text-foreground font-medium">{p.name || p.code || p.id}</span>
|
||||
</Link>
|
||||
))}
|
||||
</div>
|
||||
) : (
|
||||
<span className="text-sm text-muted-foreground">—</span>
|
||||
)}
|
||||
</InfoRow>
|
||||
</BentoCard>
|
||||
|
||||
@@ -336,26 +352,31 @@ const ASSIGN_DIALOG_FIELD_LABELS: Record<AssignDialogKind, string> = {
|
||||
};
|
||||
|
||||
/**
|
||||
* Dialog for assigning a role / permission / branch to a staff user.
|
||||
* Two selects: target user (GET /users/staff) and the resource to assign.
|
||||
* Dialog for assigning a role / branch to a staff user, or assigning a permission to a role.
|
||||
*/
|
||||
function AssignDialog({ kind, onClose }: { kind: AssignDialogKind; onClose: () => void }) {
|
||||
const { t, i18n } = useTranslation();
|
||||
const lang = (i18n.language === "ru" ? "ru" : i18n.language === "en" ? "en" : "tm") as keyof LocalizedText;
|
||||
|
||||
const [userSearch, setUserSearch] = useState("");
|
||||
const [userValue, setUserValue] = useState("");
|
||||
const isPermission = kind === "permission";
|
||||
|
||||
const [targetSearch, setTargetSearch] = useState("");
|
||||
const [targetValue, setTargetValue] = useState("");
|
||||
const [resourceSearch, setResourceSearch] = useState("");
|
||||
const [resourceValue, setResourceValue] = useState("");
|
||||
const [error, setError] = useState<string | undefined>();
|
||||
|
||||
const staffQuery = useInfiniteStaff(userSearch);
|
||||
// Target query: Staff users when assigning role/branch to a user; Roles when assigning permission to a role.
|
||||
const staffQuery = useInfiniteStaff(targetSearch);
|
||||
const targetRolesQuery = useInfiniteRoles(targetSearch, isPermission);
|
||||
|
||||
// Resource query:
|
||||
const rolesQuery = useInfiniteRoles(resourceSearch, kind === "role");
|
||||
const permissionsQuery = useInfinitePermissions(resourceSearch, kind === "permission");
|
||||
const branchesQuery = useInfiniteBranches(resourceSearch, kind === "branch");
|
||||
|
||||
const roleMutation = useAssignRoleToUser();
|
||||
const permissionMutation = useAssignPermissionToUser();
|
||||
const permissionMutation = useAssignPermissionToRole();
|
||||
const branchMutation = useAssignBranchToUser();
|
||||
const isPending = roleMutation.isPending || permissionMutation.isPending || branchMutation.isPending;
|
||||
|
||||
@@ -370,6 +391,24 @@ function AssignDialog({ kind, onClose }: { kind: AssignDialogKind; onClose: () =
|
||||
[staffQuery.data],
|
||||
);
|
||||
|
||||
const targetRoleOptions: InfiniteSelectOption[] = useMemo(
|
||||
() =>
|
||||
(targetRolesQuery.data?.pages ?? [])
|
||||
.flatMap((p) => p?.roles ?? [])
|
||||
.map((r) => ({ value: String(r.id), label: r.name || r.code || String(r.id) })),
|
||||
[targetRolesQuery.data],
|
||||
);
|
||||
|
||||
const targetOptions = isPermission ? targetRoleOptions : staffOptions;
|
||||
const targetLabel = isPermission ? t("users.fields.role", "Rol") : t("profile.assign.userLabel", "Ulanyjy (staff)");
|
||||
const targetLoading = isPermission ? targetRolesQuery.isLoading : staffQuery.isLoading;
|
||||
const targetLoadingMore = isPermission ? targetRolesQuery.isFetchingNextPage : staffQuery.isFetchingNextPage;
|
||||
const targetHasNextPage = isPermission ? !!targetRolesQuery.hasNextPage : !!staffQuery.hasNextPage;
|
||||
const loadMoreTargets = () => {
|
||||
if (isPermission) targetRolesQuery.fetchNextPage();
|
||||
else staffQuery.fetchNextPage();
|
||||
};
|
||||
|
||||
const resourceOptions: InfiniteSelectOption[] = useMemo(() => {
|
||||
if (kind === "role") {
|
||||
return (rolesQuery.data?.pages ?? [])
|
||||
@@ -406,23 +445,23 @@ function AssignDialog({ kind, onClose }: { kind: AssignDialogKind; onClose: () =
|
||||
};
|
||||
|
||||
const handleSubmit = () => {
|
||||
if (!userValue || !resourceValue) {
|
||||
if (!targetValue || !resourceValue) {
|
||||
setError(t("common.errors.required", "Meýdança hökmany doldurylmaly"));
|
||||
return;
|
||||
}
|
||||
setError(undefined);
|
||||
|
||||
const userId = Number(userValue);
|
||||
const resourceId = Number(resourceValue);
|
||||
const firstId = Number(targetValue);
|
||||
const secondId = Number(resourceValue);
|
||||
const onSuccess = () => {
|
||||
setUserValue("");
|
||||
setTargetValue("");
|
||||
setResourceValue("");
|
||||
onClose();
|
||||
};
|
||||
|
||||
if (kind === "role") roleMutation.mutate({ userId, roleId: resourceId }, { onSuccess });
|
||||
else if (kind === "permission") permissionMutation.mutate({ userId, permissionId: resourceId }, { onSuccess });
|
||||
else branchMutation.mutate({ userId, branchId: resourceId }, { onSuccess });
|
||||
if (kind === "role") roleMutation.mutate({ userId: firstId, roleId: secondId }, { onSuccess });
|
||||
else if (kind === "permission") permissionMutation.mutate({ roleId: firstId, permissionId: secondId }, { onSuccess });
|
||||
else branchMutation.mutate({ userId: firstId, branchId: secondId }, { onSuccess });
|
||||
};
|
||||
|
||||
return (
|
||||
@@ -438,20 +477,20 @@ function AssignDialog({ kind, onClose }: { kind: AssignDialogKind; onClose: () =
|
||||
<div className="space-y-3">
|
||||
<InfiniteSelect
|
||||
required
|
||||
label={t("profile.assign.userLabel", "Ulanyjy (staff)")}
|
||||
value={userValue}
|
||||
label={targetLabel}
|
||||
value={targetValue}
|
||||
onChange={(v) => {
|
||||
setUserValue(v);
|
||||
setTargetValue(v);
|
||||
if (error) setError(undefined);
|
||||
}}
|
||||
options={staffOptions}
|
||||
options={targetOptions}
|
||||
placeholder={t("loanOrderForm.placeholders.searchableSelect") || "Saýlamak üçin basyň"}
|
||||
loading={staffQuery.isLoading}
|
||||
isLoadingMore={staffQuery.isFetchingNextPage}
|
||||
hasNextPage={!!staffQuery.hasNextPage}
|
||||
onLoadMore={() => staffQuery.fetchNextPage()}
|
||||
searchValue={userSearch}
|
||||
onSearchChange={setUserSearch}
|
||||
loading={targetLoading}
|
||||
isLoadingMore={targetLoadingMore}
|
||||
hasNextPage={targetHasNextPage}
|
||||
onLoadMore={loadMoreTargets}
|
||||
searchValue={targetSearch}
|
||||
onSearchChange={setTargetSearch}
|
||||
/>
|
||||
|
||||
<InfiniteSelect
|
||||
@@ -478,7 +517,7 @@ function AssignDialog({ kind, onClose }: { kind: AssignDialogKind; onClose: () =
|
||||
<Button variant="ghost" size="sm" onClick={onClose} disabled={isPending}>
|
||||
{t("common.cancel", "Yza")}
|
||||
</Button>
|
||||
<Button size="sm" onClick={handleSubmit} disabled={!userValue || !resourceValue || isPending}>
|
||||
<Button size="sm" onClick={handleSubmit} disabled={!targetValue || !resourceValue || isPending}>
|
||||
{t("common.save", "Ýatda sakla")}
|
||||
</Button>
|
||||
</AlertDialogFooter>
|
||||
|
||||
Reference in new issue
Block a user