Align RBAC API contract
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -13,143 +13,74 @@ import {
|
||||
import { InfiniteSelect, type InfiniteSelectOption } from "@/components/infiniteSelect";
|
||||
import { useInfiniteUsers } from "@/features/allUsers/hooks/useAllUsers";
|
||||
import { useInfiniteRoles } from "@/features/roles/hooks/useRoles";
|
||||
import { useInfinitePermissions } from "@/features/permissions/hooks/usePermissions";
|
||||
import { useInfiniteBranches } from "@/features/shared/hooks/useSharedBranchUsers";
|
||||
import { useAssignRoleToUser, useAssignPermissionToRole, useAssignBranchToUser } from "@/features/shared/hooks/useSharedUserAssignments";
|
||||
import { useAssignRoleToUser, useAssignBranchToUser } from "@/features/shared/hooks/useSharedUserAssignments";
|
||||
import type { LocalizedText } from "@/features/branches/api/branchesApi";
|
||||
|
||||
export type AssignDialogKind = "role" | "permission" | "branch";
|
||||
export type AssignDialogKind = "role" | "branch";
|
||||
|
||||
export interface AssignDialogProps {
|
||||
kind: AssignDialogKind;
|
||||
onClose: () => void;
|
||||
targetUserId?: number;
|
||||
targetUserName?: string;
|
||||
targetRoleId?: number;
|
||||
targetRoleName?: string;
|
||||
onAssigned?: () => void;
|
||||
}
|
||||
|
||||
const ASSIGN_DIALOG_TITLES: Record<AssignDialogKind, string> = {
|
||||
role: "profile.actions.assignRole",
|
||||
permission: "profile.actions.assignPermission",
|
||||
branch: "profile.actions.assignBranch",
|
||||
};
|
||||
|
||||
const ASSIGN_DIALOG_FIELD_LABELS: Record<AssignDialogKind, string> = {
|
||||
role: "users.fields.role",
|
||||
permission: "users.sections.permissions",
|
||||
branch: "users.sections.branches",
|
||||
};
|
||||
|
||||
/**
|
||||
* Dialog for assigning a role / branch to a staff user, or assigning a permission to a role.
|
||||
* Permissions are assigned to roles via POST /permission-roles/bulk: { permissionIds: [...], roleId }.
|
||||
*/
|
||||
export function AssignDialog({ kind, onClose, targetUserId, targetUserName, targetRoleId, targetRoleName, onAssigned }: AssignDialogProps) {
|
||||
export function AssignDialog({ kind, onClose, targetUserId, targetUserName, onAssigned }: AssignDialogProps) {
|
||||
const { t, i18n } = useTranslation();
|
||||
const lang = (i18n.language === "ru" ? "ru" : i18n.language === "en" ? "en" : "tm") as keyof LocalizedText;
|
||||
|
||||
const isPermission = kind === "permission";
|
||||
|
||||
const [targetSearch, setTargetSearch] = useState("");
|
||||
const [targetValue, setTargetValue] = useState<string>(() => {
|
||||
if (isPermission && targetRoleId) return String(targetRoleId);
|
||||
if (!isPermission && targetUserId) return String(targetUserId);
|
||||
return "";
|
||||
});
|
||||
const [targetValue, setTargetValue] = useState(() => (targetUserId ? String(targetUserId) : ""));
|
||||
const [resourceSearch, setResourceSearch] = useState("");
|
||||
const [resourceValue, setResourceValue] = useState("");
|
||||
const [error, setError] = useState<string | undefined>();
|
||||
|
||||
// Target query: admin users list for role/branch; roles list for permission
|
||||
const staffQuery = useInfiniteUsers(targetSearch, !isPermission);
|
||||
const targetRolesQuery = useInfiniteRoles(targetSearch, isPermission);
|
||||
|
||||
// Resource queries
|
||||
const staffQuery = useInfiniteUsers(targetSearch);
|
||||
const rolesQuery = useInfiniteRoles(resourceSearch, kind === "role");
|
||||
const permissionsQuery = useInfinitePermissions(resourceSearch, kind === "permission");
|
||||
const branchesQuery = useInfiniteBranches(resourceSearch, kind === "branch");
|
||||
|
||||
const roleMutation = useAssignRoleToUser();
|
||||
const permissionRoleMutation = useAssignPermissionToRole();
|
||||
const branchMutation = useAssignBranchToUser();
|
||||
const isPending = roleMutation.isPending || permissionRoleMutation.isPending || branchMutation.isPending;
|
||||
const isPending = roleMutation.isPending || branchMutation.isPending;
|
||||
|
||||
const targetOptions: InfiniteSelectOption[] = useMemo(() => {
|
||||
if (isPermission) {
|
||||
const opts: InfiniteSelectOption[] = (targetRolesQuery.data?.pages ?? [])
|
||||
.flatMap((p) => p?.roles ?? [])
|
||||
.map((r) => ({ value: String(r.id), label: r.name || r.code || String(r.id) }));
|
||||
|
||||
if (targetRoleId) {
|
||||
const valStr = String(targetRoleId);
|
||||
if (!opts.some((o) => o.value === valStr)) {
|
||||
opts.unshift({ value: valStr, label: targetRoleName || valStr });
|
||||
}
|
||||
}
|
||||
return opts;
|
||||
}
|
||||
|
||||
const opts: InfiniteSelectOption[] = (staffQuery.data?.pages ?? [])
|
||||
.flatMap((p) => p?.users ?? [])
|
||||
.map((u) => ({
|
||||
value: String(u.id),
|
||||
label: [u.firstName, u.lastName].filter(Boolean).join(" ") || u.name || u.username || String(u.id),
|
||||
const options = (staffQuery.data?.pages ?? [])
|
||||
.flatMap((page) => page?.users ?? [])
|
||||
.map((user) => ({
|
||||
value: String(user.id),
|
||||
label: [user.firstName, user.lastName].filter(Boolean).join(" ") || user.name || user.username || String(user.id),
|
||||
}));
|
||||
|
||||
if (targetUserId) {
|
||||
const valStr = String(targetUserId);
|
||||
if (!opts.some((o) => o.value === valStr)) {
|
||||
opts.unshift({ value: valStr, label: targetUserName || valStr });
|
||||
}
|
||||
if (targetUserId && !options.some((option) => option.value === String(targetUserId))) {
|
||||
options.unshift({ value: String(targetUserId), label: targetUserName || String(targetUserId) });
|
||||
}
|
||||
return opts;
|
||||
}, [isPermission, targetRolesQuery.data, targetRoleId, targetRoleName, staffQuery.data, targetUserId, targetUserName]);
|
||||
|
||||
const targetLabel = isPermission ? t("users.fields.role", "Rol") : t("profile.assign.userLabel", "Ulanyjy");
|
||||
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();
|
||||
};
|
||||
return options;
|
||||
}, [staffQuery.data, targetUserId, targetUserName]);
|
||||
|
||||
const resourceOptions: InfiniteSelectOption[] = useMemo(() => {
|
||||
if (kind === "role") {
|
||||
return (rolesQuery.data?.pages ?? [])
|
||||
.flatMap((p) => p?.roles ?? [])
|
||||
.map((r) => ({ value: String(r.id), label: r.name || r.code || String(r.id) }));
|
||||
}
|
||||
if (kind === "permission") {
|
||||
return (permissionsQuery.data?.pages ?? [])
|
||||
.flatMap((p) => p?.permissions ?? [])
|
||||
.map((perm) => ({ value: String(perm.id), label: perm.name || perm.code || String(perm.id) }));
|
||||
.flatMap((page) => page?.roles ?? [])
|
||||
.map((role) => ({ value: String(role.id), label: role.name || role.code || String(role.id) }));
|
||||
}
|
||||
return (branchesQuery.data?.pages ?? [])
|
||||
.flatMap((p) => p?.branches ?? [])
|
||||
.map((b) => ({
|
||||
value: String(b.id),
|
||||
label: b.name?.[lang] || b.name?.tm || b.name?.en || b.name?.ru || b.uniqueCode || String(b.id),
|
||||
.flatMap((page) => page?.branches ?? [])
|
||||
.map((branch) => ({
|
||||
value: String(branch.id),
|
||||
label: branch.name?.[lang] || branch.name?.tm || branch.name?.en || branch.name?.ru || branch.uniqueCode || String(branch.id),
|
||||
}));
|
||||
}, [kind, rolesQuery.data, permissionsQuery.data, branchesQuery.data, lang]);
|
||||
}, [kind, rolesQuery.data, branchesQuery.data, lang]);
|
||||
|
||||
const resourceLoading =
|
||||
kind === "role" ? rolesQuery.isLoading : kind === "permission" ? permissionsQuery.isLoading : branchesQuery.isLoading;
|
||||
const resourceLoadingMore =
|
||||
kind === "role"
|
||||
? rolesQuery.isFetchingNextPage
|
||||
: kind === "permission"
|
||||
? permissionsQuery.isFetchingNextPage
|
||||
: branchesQuery.isFetchingNextPage;
|
||||
const resourceHasNextPage =
|
||||
kind === "role" ? !!rolesQuery.hasNextPage : kind === "permission" ? !!permissionsQuery.hasNextPage : !!branchesQuery.hasNextPage;
|
||||
const loadMoreResources = () => {
|
||||
if (kind === "role") rolesQuery.fetchNextPage();
|
||||
else if (kind === "permission") permissionsQuery.fetchNextPage();
|
||||
else branchesQuery.fetchNextPage();
|
||||
};
|
||||
const resourceQuery = kind === "role" ? rolesQuery : branchesQuery;
|
||||
|
||||
const handleSubmit = () => {
|
||||
if (!targetValue || !resourceValue) {
|
||||
@@ -157,24 +88,14 @@ export function AssignDialog({ kind, onClose, targetUserId, targetUserName, targ
|
||||
return;
|
||||
}
|
||||
setError(undefined);
|
||||
|
||||
const firstId = Number(targetValue);
|
||||
const secondId = Number(resourceValue);
|
||||
const payload = { userId: Number(targetValue), roleId: Number(resourceValue) };
|
||||
const onSuccess = () => {
|
||||
setResourceValue("");
|
||||
onAssigned?.();
|
||||
onClose();
|
||||
};
|
||||
|
||||
if (kind === "role") {
|
||||
roleMutation.mutate({ userId: firstId, roleId: secondId }, { onSuccess });
|
||||
} else if (kind === "permission") {
|
||||
// Usere permission berilýän ýeri comment-e alyndy: permission user-e däl, role-a assign edilýär ({ permissionIds: [id], roleId })
|
||||
// permissionMutation.mutate({ userId: firstId, permissionId: secondId, effect: "allow" }, { onSuccess });
|
||||
permissionRoleMutation.mutate({ roleId: firstId, permissionIds: [secondId] }, { onSuccess });
|
||||
} else {
|
||||
branchMutation.mutate({ userId: firstId, branchId: secondId }, { onSuccess });
|
||||
}
|
||||
if (kind === "role") roleMutation.mutate(payload, { onSuccess });
|
||||
else branchMutation.mutate({ userId: payload.userId, branchId: payload.roleId }, { onSuccess });
|
||||
};
|
||||
|
||||
return (
|
||||
@@ -186,48 +107,45 @@ export function AssignDialog({ kind, onClose, targetUserId, targetUserName, targ
|
||||
<AlertDialogDescription>{t(ASSIGN_DIALOG_TITLES[kind])}</AlertDialogDescription>
|
||||
</VisuallyHidden.Root>
|
||||
</AlertDialogHeader>
|
||||
|
||||
<div className="space-y-3">
|
||||
<InfiniteSelect
|
||||
required
|
||||
label={targetLabel}
|
||||
label={t("profile.assign.userLabel", "Ulanyjy")}
|
||||
value={targetValue}
|
||||
onChange={(v) => {
|
||||
setTargetValue(v);
|
||||
onChange={(value) => {
|
||||
setTargetValue(value);
|
||||
if (error) setError(undefined);
|
||||
}}
|
||||
disabled={!!targetUserId || isPending}
|
||||
options={targetOptions}
|
||||
placeholder={t("loanOrderForm.placeholders.searchableSelect") || "Saýlamak üçin basyň"}
|
||||
loading={targetLoading}
|
||||
isLoadingMore={targetLoadingMore}
|
||||
hasNextPage={targetHasNextPage}
|
||||
onLoadMore={loadMoreTargets}
|
||||
loading={staffQuery.isLoading}
|
||||
isLoadingMore={staffQuery.isFetchingNextPage}
|
||||
hasNextPage={!!staffQuery.hasNextPage}
|
||||
onLoadMore={() => staffQuery.fetchNextPage()}
|
||||
searchValue={targetSearch}
|
||||
onSearchChange={setTargetSearch}
|
||||
/>
|
||||
|
||||
<InfiniteSelect
|
||||
required
|
||||
label={t(ASSIGN_DIALOG_FIELD_LABELS[kind])}
|
||||
value={resourceValue}
|
||||
onChange={(v) => {
|
||||
setResourceValue(v);
|
||||
onChange={(value) => {
|
||||
setResourceValue(value);
|
||||
if (error) setError(undefined);
|
||||
}}
|
||||
error={error}
|
||||
disabled={isPending}
|
||||
options={resourceOptions}
|
||||
placeholder={t("loanOrderForm.placeholders.searchableSelect") || "Saýlamak üçin basyň"}
|
||||
loading={resourceLoading}
|
||||
isLoadingMore={resourceLoadingMore}
|
||||
hasNextPage={resourceHasNextPage}
|
||||
onLoadMore={loadMoreResources}
|
||||
loading={resourceQuery.isLoading}
|
||||
isLoadingMore={resourceQuery.isFetchingNextPage}
|
||||
hasNextPage={!!resourceQuery.hasNextPage}
|
||||
onLoadMore={() => resourceQuery.fetchNextPage()}
|
||||
searchValue={resourceSearch}
|
||||
onSearchChange={setResourceSearch}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<AlertDialogFooter>
|
||||
<Button variant="ghost" size="sm" onClick={onClose} disabled={isPending}>
|
||||
{t("common.cancel", "Yza")}
|
||||
|
||||
Reference in New Issue
Block a user