diff --git a/src/pages/roles/core/_requests.ts b/src/pages/roles/core/_requests.ts index 3e968fc..b990afb 100644 --- a/src/pages/roles/core/_requests.ts +++ b/src/pages/roles/core/_requests.ts @@ -16,33 +16,23 @@ import { export const getRoles = async () => { try { - const response = await httpGetRequest( + const response = await httpGetRequest<{ roles: Role[] | null }>( APIUrlGenerator(API_ROUTES.GET_ROLES) ); console.log("Roles API Response:", response); console.log("Roles data:", response.data); - // اگر response.data آرایه نیست، ممکن است در فیلد دیگری باشد - if (Array.isArray(response.data)) { - return response.data; + // ساختار API: {data: {roles: Role[] | null}} + if ( + response.data && + response.data.roles && + Array.isArray(response.data.roles) + ) { + return response.data.roles; } - // بررسی اگر در فیلدهای دیگر باشد - if (response.data && typeof response.data === "object") { - // چک کن آیا در results یا items یا data هست - if (Array.isArray((response.data as any).results)) { - return (response.data as any).results; - } - if (Array.isArray((response.data as any).items)) { - return (response.data as any).items; - } - if (Array.isArray((response.data as any).data)) { - return (response.data as any).data; - } - } - - // fallback: آرایه خالی برگردان - console.warn("Roles data is not an array:", response.data); + // اگر roles null باشد یا آرایه نباشد، آرایه خالی برگردان + console.warn("Roles is null or not an array:", response.data); return []; } catch (error) { console.error("Error fetching roles:", error); @@ -82,10 +72,30 @@ export const deleteRole = async (id: string) => { }; export const getRolePermissions = async (id: string) => { - const response = await httpGetRequest( - APIUrlGenerator(API_ROUTES.GET_ROLE_PERMISSIONS(id)) - ); - return response.data; + try { + const response = await httpGetRequest<{ permissions: Permission[] | null }>( + APIUrlGenerator(API_ROUTES.GET_ROLE_PERMISSIONS(id)) + ); + + console.log("Role Permissions API Response:", response); + console.log("Role Permissions data:", response.data); + + // Handle API response structure: {data: {permissions: Permission[] | null}} + if ( + response.data && + response.data.permissions && + Array.isArray(response.data.permissions) + ) { + return response.data.permissions; + } + + // If permissions is null or not an array, return empty array + console.warn("Role Permissions is null or not an array:", response.data); + return []; + } catch (error) { + console.error("Error fetching role permissions:", error); + return []; + } }; export const assignPermissionToRole = async ( diff --git a/src/pages/roles/role-permissions/RolePermissionsPage.tsx b/src/pages/roles/role-permissions/RolePermissionsPage.tsx index 7386315..a4a593f 100644 --- a/src/pages/roles/role-permissions/RolePermissionsPage.tsx +++ b/src/pages/roles/role-permissions/RolePermissionsPage.tsx @@ -4,7 +4,7 @@ import { useRole, useRolePermissions, usePermissions, useAssignPermission, useRe import { Button } from "@/components/ui/Button"; import { LoadingSpinner } from "@/components/ui/LoadingSpinner"; import { Permission } from "@/types/auth"; -import { ArrowRight, Plus, Trash2, Check } from "lucide-react"; +import { ArrowRight, Plus, Trash2, Check, Shield } from "lucide-react"; import { Modal } from "@/components/ui/Modal"; const RolePermissionsPage = () => { @@ -16,6 +16,13 @@ const RolePermissionsPage = () => { const { data: role, isLoading: roleLoading } = useRole(id); const { data: rolePermissions, isLoading: permissionsLoading } = useRolePermissions(id); const { data: allPermissions, isLoading: allPermissionsLoading } = usePermissions(); + + const assignedPermissionIds = (rolePermissions || []).map(p => p.id); + + // Access the permissions array from the object structure + const safeAllPermissions = Array.isArray((allPermissions as any)?.permissions) ? (allPermissions as any).permissions : []; + const availablePermissions = safeAllPermissions.filter((p: any) => !assignedPermissionIds.includes(p.id)); + const { mutate: assignPermission, isPending: assigning } = useAssignPermission(); const { mutate: removePermission, isPending: removing } = useRemovePermission(); @@ -52,222 +59,161 @@ const RolePermissionsPage = () => { if (isLoading) return ; if (!role) return
نقش یافت نشد
; - const assignedPermissionIds = rolePermissions?.map(p => p.id) || []; - const availablePermissions = allPermissions?.filter(p => !assignedPermissionIds.includes(p.id)) || []; - return (
-
-
-
- -
-

- مدیریت دسترسی‌های نقش -

-

- {role.title} -

+
+ {/* Header */} +
+ +
+

+ مدیریت دسترسی‌های نقش: {role?.title} +

+

+ تخصیص و حذف دسترسی‌ها برای این نقش +

+
+
+ +
+ {/* دسترسی‌های تخصیص یافته */} +
+
+

+ دسترسی‌های تخصیص یافته ({(rolePermissions || []).length}) +

+
+ +
+ {permissionsLoading ? ( +
+ +
+ ) : ( +
+ {(rolePermissions || []).length > 0 ? ( + (rolePermissions || []).map((permission: Permission) => ( +
+
+

+ {permission.title} +

+

+ {permission.description} +

+
+ +
+ )) + ) : ( +

+ هیچ دسترسی تخصیص داده نشده است +

+ )} +
+ )}
- -
-
+ {/* دسترسی‌های در دسترس */} +
+
+

+ دسترسی‌های قابل تخصیص ({availablePermissions.length}) +

+
-
- {/* دسترسی‌های تخصیص یافته */} -
-
-

- دسترسی‌های تخصیص یافته ({assignedPermissionIds.length}) -

-
- -
- {permissionsLoading ? ( -
- -
- ) : ( -
- {rolePermissions && rolePermissions.length > 0 ? ( - rolePermissions.map((permission: Permission) => ( -
-
-

- {permission.title} -

-

- {permission.description} -

-
- -
- )) - ) : ( -

- هیچ دسترسی تخصیص داده نشده است -

- )} -
- )} -
-
- - {/* دسترسی‌های در دسترس */} -
-
-

- دسترسی‌های قابل تخصیص ({availablePermissions.length}) -

-
- -
- {allPermissionsLoading ? ( -
- -
- ) : ( -
- {availablePermissions.length > 0 ? ( - availablePermissions.map((permission: Permission) => ( -
-
-

- {permission.title} -

-

- {permission.description} -

-
- -
- )) - ) : ( -

- تمام دسترسی‌ها به این نقش تخصیص داده شده است -

- )} -
- )} -
-
-
- - {/* Assign Permission Modal */} - setShowAssignModal(false)} - title="اختصاص دسترسی جدید" - size="lg" - > -
-

- دسترسی مورد نظر را برای تخصیص به نقش "{role.title}" انتخاب کنید: -

- -
- {availablePermissions.map((permission: Permission) => ( -
-
-

- {permission.title} -

-

- {permission.description} -

+
+ {allPermissionsLoading ? ( +
+
- -
- ))} -
- -
- + ) : ( +
+ {availablePermissions.length > 0 ? ( + availablePermissions.map((permission: Permission) => ( +
+
+

+ {permission.title} +

+

+ {permission.description} +

+
+ +
+ )) + ) : ( +

+ تمام دسترسی‌ها به این نقش تخصیص داده شده است +

+ )} +
+ )} +
- - {/* Remove Permission Confirmation Modal */} - -
-

- آیا از حذف این دسترسی از نقش اطمینان دارید؟ -

-
- - + {/* Modal تأیید حذف دسترسی */} + setRemovePermissionId(null)} + title="حذف دسترسی" + > +
+

+ آیا از حذف این دسترسی از نقش اطمینان دارید؟ +

+
+ + +
-
- + +
); };