added payment setting page and quota
This commit is contained in:
@@ -15,7 +15,7 @@ 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, useAssignPermissionToUser, useAssignBranchToUser } from "@/features/shared/hooks/useSharedUserAssignments";
|
||||
import { useAssignRoleToUser, useAssignPermissionToRole, useAssignBranchToUser } from "@/features/shared/hooks/useSharedUserAssignments";
|
||||
import type { LocalizedText } from "@/features/branches/api/branchesApi";
|
||||
|
||||
export type AssignDialogKind = "role" | "permission" | "branch";
|
||||
@@ -25,6 +25,8 @@ export interface AssignDialogProps {
|
||||
onClose: () => void;
|
||||
targetUserId?: number;
|
||||
targetUserName?: string;
|
||||
targetRoleId?: number;
|
||||
targetRoleName?: string;
|
||||
onAssigned?: () => void;
|
||||
}
|
||||
|
||||
@@ -41,21 +43,28 @@ const ASSIGN_DIALOG_FIELD_LABELS: Record<AssignDialogKind, string> = {
|
||||
};
|
||||
|
||||
/**
|
||||
* Dialog for assigning a role, permission, or branch to a user (staff).
|
||||
* Permissions are assigned directly to users via POST /permission-users.
|
||||
* 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, onAssigned }: AssignDialogProps) {
|
||||
export function AssignDialog({ kind, onClose, targetUserId, targetUserName, targetRoleId, targetRoleName, 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>(() => (targetUserId ? String(targetUserId) : ""));
|
||||
const [targetValue, setTargetValue] = useState<string>(() => {
|
||||
if (isPermission && targetRoleId) return String(targetRoleId);
|
||||
if (!isPermission && targetUserId) return String(targetUserId);
|
||||
return "";
|
||||
});
|
||||
const [resourceSearch, setResourceSearch] = useState("");
|
||||
const [resourceValue, setResourceValue] = useState("");
|
||||
const [error, setError] = useState<string | undefined>();
|
||||
|
||||
// Target query: admin users list (GET /users)
|
||||
const staffQuery = useInfiniteUsers(targetSearch);
|
||||
// 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 rolesQuery = useInfiniteRoles(resourceSearch, kind === "role");
|
||||
@@ -63,11 +72,25 @@ export function AssignDialog({ kind, onClose, targetUserId, targetUserName, onAs
|
||||
const branchesQuery = useInfiniteBranches(resourceSearch, kind === "branch");
|
||||
|
||||
const roleMutation = useAssignRoleToUser();
|
||||
const permissionMutation = useAssignPermissionToUser();
|
||||
const permissionRoleMutation = useAssignPermissionToRole();
|
||||
const branchMutation = useAssignBranchToUser();
|
||||
const isPending = roleMutation.isPending || permissionMutation.isPending || branchMutation.isPending;
|
||||
const isPending = roleMutation.isPending || permissionRoleMutation.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) => ({
|
||||
@@ -82,13 +105,16 @@ export function AssignDialog({ kind, onClose, targetUserId, targetUserName, onAs
|
||||
}
|
||||
}
|
||||
return opts;
|
||||
}, [staffQuery.data, targetUserId, targetUserName]);
|
||||
}, [isPermission, targetRolesQuery.data, targetRoleId, targetRoleName, staffQuery.data, targetUserId, targetUserName]);
|
||||
|
||||
const targetLabel = t("profile.assign.userLabel", "Ulanyjy");
|
||||
const targetLoading = staffQuery.isLoading;
|
||||
const targetLoadingMore = staffQuery.isFetchingNextPage;
|
||||
const targetHasNextPage = !!staffQuery.hasNextPage;
|
||||
const loadMoreTargets = () => staffQuery.fetchNextPage();
|
||||
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();
|
||||
};
|
||||
|
||||
const resourceOptions: InfiniteSelectOption[] = useMemo(() => {
|
||||
if (kind === "role") {
|
||||
@@ -132,7 +158,7 @@ export function AssignDialog({ kind, onClose, targetUserId, targetUserName, onAs
|
||||
}
|
||||
setError(undefined);
|
||||
|
||||
const userId = Number(targetValue);
|
||||
const firstId = Number(targetValue);
|
||||
const secondId = Number(resourceValue);
|
||||
const onSuccess = () => {
|
||||
setResourceValue("");
|
||||
@@ -141,11 +167,13 @@ export function AssignDialog({ kind, onClose, targetUserId, targetUserName, onAs
|
||||
};
|
||||
|
||||
if (kind === "role") {
|
||||
roleMutation.mutate({ userId, roleId: secondId }, { onSuccess });
|
||||
roleMutation.mutate({ userId: firstId, roleId: secondId }, { onSuccess });
|
||||
} else if (kind === "permission") {
|
||||
permissionMutation.mutate({ userId, permissionId: secondId, effect: "allow" }, { onSuccess });
|
||||
// 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, branchId: secondId }, { onSuccess });
|
||||
branchMutation.mutate({ userId: firstId, branchId: secondId }, { onSuccess });
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user