diff --git a/src/app/dashboard/(user-dashboard)/settings/organization/roles-and-permissions/page.tsx b/src/app/dashboard/(user-dashboard)/settings/organization/roles-and-permissions/page.tsx index 2873b0226..df7b0c3ca 100644 --- a/src/app/dashboard/(user-dashboard)/settings/organization/roles-and-permissions/page.tsx +++ b/src/app/dashboard/(user-dashboard)/settings/organization/roles-and-permissions/page.tsx @@ -1,9 +1,12 @@ "use client"; -import { useState } from "react"; +import { useEffect, useState } from "react"; import CustomButton from "~/components/common/common-button/common-button"; import RoleCreationModal from "~/components/common/modals/role-creation"; +import LoadingSpinner from "~/components/miscellaneous/loading-spinner"; +import { useToast } from "~/components/ui/use-toast"; +import { getApiUrl } from "~/utils/getApiUrl"; type Role = { id: number; @@ -12,75 +15,116 @@ type Role = { }; type Permission = { - name: string; - enabled: boolean; -}; - -const rolesData: Role[] = [ - { id: 1, name: "Administrator", description: "Full access, control" }, - { id: 2, name: "Guest", description: "Read-only access" }, - { id: 3, name: "User", description: "Read, write, update" }, - { id: 4, name: "Manager", description: "Read, write, approve" }, - { id: 5, name: "Project Lead", description: "Manage, coordinate, oversee" }, -]; - -const permissionsData: { [key: number]: Permission[] } = { - 1: [ - { name: "Can view transactions", enabled: true }, - { name: "Can view refunds", enabled: false }, - { name: "Can log refunds", enabled: true }, - ], - 2: [ - { name: "Can view users", enabled: true }, - { name: "Can create users", enabled: false }, - { name: "Can edit users", enabled: true }, - { name: "Can blacklist/whitelist users", enabled: true }, - ], - 3: [ - { name: "Can view users", enabled: true }, - { name: "Can create users", enabled: true }, - { name: "Can edit users", enabled: false }, - { name: "Can blacklist/whitelist users", enabled: true }, - ], - 4: [ - { name: "Can view transactions", enabled: true }, - { name: "Can view refunds", enabled: true }, - { name: "Can log refunds", enabled: true }, - { name: "Can view users", enabled: true }, - { name: "Can create users", enabled: true }, - { name: "Can edit users", enabled: true }, - { name: "Can blacklist/whitelist users", enabled: true }, - ], - 5: [ - { name: "Can view transactions", enabled: true }, - { name: "Can view refunds", enabled: true }, - { name: "Can log refunds", enabled: true }, - { name: "Can view users", enabled: true }, - { name: "Can create users", enabled: true }, - { name: "Can edit users", enabled: true }, - { name: "Can blacklist/whitelist users", enabled: true }, - ], + [key: string]: boolean; }; const RolesAndPermission = () => { const [selectedRoleId, setSelectedRoleId] = useState(); - const [permissions, setPermissions] = useState([]); + const [permissions, setPermissions] = useState({}); const [isModalOpen, setIsModalOpen] = useState(false); + const [roles, setRoles] = useState([]); + const [apiUrl, setApiUrl] = useState(""); + const { toast } = useToast(); + const [loadingRoles, setLoadingRoles] = useState(true); + const [loadingPermissions, setLoadingPermissions] = useState(false); + const [loadingRequest, setLoadingRequest] = useState(false); + const org_id = "9ca4512c-f665-4901-ba63-0b6492e47d32"; + + useEffect(() => { + setLoadingRoles(true); + const fetchData = async () => { + try { + const url = await getApiUrl(); + setApiUrl(url); + const response = await fetch(`${url}/organisations/${org_id}/roles`); + const data = await response.json(); + setRoles(data); + setLoadingRoles(false); + } catch (error: unknown) { + toast({ + title: "Error occurred", + description: + error instanceof Error ? error.message : "Error fetching data", + variant: "destructive", + }); + setLoadingRoles(false); + } + }; + fetchData(); + }, [toast]); + + useEffect(() => { + const fetchPermissions = async () => { + if (selectedRoleId) { + setLoadingPermissions(true); + try { + const response = await fetch( + `${apiUrl}/organisations/${org_id}/roles/${selectedRoleId}`, + ); + const permissionsData = await response.json(); + setPermissions(permissionsData.permission_list); + setLoadingPermissions(false); + } catch { + toast({ + title: "An error occurred", + description: "Error fetching permissions", + variant: "destructive", + }); + setLoadingPermissions(false); + } + } + }; + fetchPermissions(); + }, [selectedRoleId, apiUrl, org_id, toast]); const handleRoleClick = (roleId: number) => { setSelectedRoleId(roleId); - setPermissions([...permissionsData[roleId]]); }; - const handleToggle = (index: number) => { - setPermissions((previous) => { - const newPermissions = [...previous]; - newPermissions[index].enabled = !newPermissions[index].enabled; - permissionsData[selectedRoleId!] = newPermissions; - return newPermissions; + const handleToggle = (permission: string, value: boolean) => { + setPermissions({ + ...permissions, + [permission]: value, }); }; + const handleSave = async () => { + setLoadingRequest(true); + try { + const response = await fetch( + `${apiUrl}/organisations/${org_id}/${selectedRoleId}/permissions`, + { + method: "PUT", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ permission_list: permissions }), + }, + ); + + if (response.ok) { + toast({ + title: "Success", + description: "Permissions updated successfully", + variant: "default", + }); + } else { + toast({ + title: "Error", + description: "Failed to update permissions", + variant: "destructive", + }); + } + } catch { + toast({ + title: "Error", + description: "Failed to update permissions", + variant: "destructive", + }); + } + setLoadingRequest(false); + }; + const handleModalOpen = () => { setIsModalOpen(true); }; @@ -95,7 +139,12 @@ const RolesAndPermission = () => {

Roles

    - {rolesData.map((role) => ( + {loadingRoles && ( +
    + +
    + )} + {roles.map((role) => (
  • { Click on a role to view permissions

+ ) : loadingPermissions ? ( +
+ +
) : (
- {permissions.map((permission, index) => ( + {Object.keys(permissions).map((permission) => (
- {permission.name} + {permission + .replaceAll("_", " ") + .replaceAll(/\b\w/g, (l) => l.toUpperCase())}
))}
-