From 3d31713c69514814e48badc9f5cb6a483362df6e Mon Sep 17 00:00:00 2001 From: Abdulmalik Yusuf <31839174+abdulmalikyusuf@users.noreply.github.com> Date: Tue, 13 Aug 2024 21:32:16 +0100 Subject: [PATCH 1/5] feat: implemented create roles page --- src/actions/roles-and-permissions.ts | 114 ++++++++++++++ .../create-role/page.tsx | 144 ++++++++++++++++++ .../roles-and-permissions/page.tsx | 88 ++++++----- .../roles-permissions-creation/index.tsx | 110 +++++++++++++ src/schemas/index.ts | 12 ++ 5 files changed, 427 insertions(+), 41 deletions(-) create mode 100644 src/actions/roles-and-permissions.ts create mode 100644 src/app/dashboard/(admin)/admin/(settings)/settings/organization/roles-and-permissions/create-role/page.tsx create mode 100644 src/components/common/modals/roles-permissions-creation/index.tsx diff --git a/src/actions/roles-and-permissions.ts b/src/actions/roles-and-permissions.ts new file mode 100644 index 000000000..5125a7e71 --- /dev/null +++ b/src/actions/roles-and-permissions.ts @@ -0,0 +1,114 @@ +"use server"; + +import axios from "axios"; +import { z } from "zod"; + +import { auth } from "~/lib/auth"; +import { roleSchema } from "~/schemas"; + +const apiUrl = process.env.API_URL; + +export const getRoles = async (org_id: string) => { + const session = await auth(); + + try { + const response = await axios.get( + `${apiUrl}/api/v1/organisations/${org_id}/roles`, + { + headers: { + Authorization: `Bearer ${session?.access_token}`, + }, + }, + ); + + return { + data: response.data, + }; + } catch (error) { + return axios.isAxiosError(error) && error.response + ? { + error: error.response.data.message || "Failed.", + status: error.response.status, + } + : { + error: "An unexpected error occurred.", + }; + } +}; + +export const createRole = async ( + values: z.infer, + org_id: string, +) => { + const validatedFields = roleSchema.safeParse(values); + if (!validatedFields.success) { + return { + error: "Create Role Failed. Please check your inputs.", + }; + } + + const session = await auth(); + const payload = validatedFields.data; + + try { + const response = await axios.post( + `${apiUrl}/api/v1/organisations/${org_id}/roles`, + payload, + { + headers: { + Authorization: `Bearer ${session?.access_token}`, + }, + }, + ); + return { + data: response.data, + }; + } catch (error) { + return axios.isAxiosError(error) && error.response + ? { + error: error.response.data.message || "Role creation failed.", + status: error.response.status, + } + : { + error: "An unexpected error occurred.", + }; + } +}; +export const updatePermissions = async ( + values: z.infer, + org_id: string, +) => { + const validatedFields = roleSchema.safeParse(values); + if (!validatedFields.success) { + return { + error: "Create Role Failed. Please check your inputs.", + }; + } + + const session = await auth(); + const payload = validatedFields.data; + + try { + const response = await axios.post( + `${apiUrl}/api/v1/organisations/${org_id}/roles`, + payload, + { + headers: { + Authorization: `Bearer ${session?.access_token}`, + }, + }, + ); + return { + data: response.data, + }; + } catch (error) { + return axios.isAxiosError(error) && error.response + ? { + error: error.response.data.message || "Role creation failed.", + status: error.response.status, + } + : { + error: "An unexpected error occurred.", + }; + } +}; diff --git a/src/app/dashboard/(admin)/admin/(settings)/settings/organization/roles-and-permissions/create-role/page.tsx b/src/app/dashboard/(admin)/admin/(settings)/settings/organization/roles-and-permissions/create-role/page.tsx new file mode 100644 index 000000000..2d7322842 --- /dev/null +++ b/src/app/dashboard/(admin)/admin/(settings)/settings/organization/roles-and-permissions/create-role/page.tsx @@ -0,0 +1,144 @@ +"use client"; + +import { zodResolver } from "@hookform/resolvers/zod"; +import { ChevronLeftIcon } from "lucide-react"; +import Link from "next/link"; +import { useState } from "react"; +import { useForm } from "react-hook-form"; +import { z } from "zod"; + +import { createRole } from "~/actions/roles-and-permissions"; +import RolePermissionsCreationModal from "~/components/common/modals/roles-permissions-creation"; +import { + Select, + SelectContent, + SelectItem, + SelectTrigger, + SelectValue, +} from "~/components/ui/select"; +import { useLocalStorage } from "~/hooks/use-local-storage"; +import { roleSchema } from "~/schemas"; + +type UseFormInputs = z.infer; +type Permission = { + [key: string]: boolean; +}; + +function CreateNewRolePage() { + const [org_id] = useLocalStorage("current_orgid", ""); + const [permissions, setPermissions] = useState({}); + + const { register, handleSubmit } = useForm({ + mode: "onBlur", + resolver: zodResolver(roleSchema), + }); + + const onValid = async (values: UseFormInputs) => { + try { + const { error, status } = await createRole(values, org_id); + if (error && status) { + // show toast + } else { + // show toast dismiss modal + } + return; + } catch (error) { + console.log(error); + } + }; + return ( +
+
+
+ + + +

Create Role

+
+

+ Custom roles let you group permissions and assign them to principals + in your organization. You can manually select permissions or import + permissions from another role. +

+
+
+
+
+ + +
+
+ + +
+
+ +