Added upsert role action
This commit is contained in:
parent
302e4d85b6
commit
bf41292b28
|
|
@ -1,7 +1,9 @@
|
||||||
"use client"
|
"use client";
|
||||||
|
import upsertRole from "@/features/dashboard/roles/actions/upsertRole";
|
||||||
import roleFormDataSchema, {
|
import roleFormDataSchema, {
|
||||||
RoleFormData,
|
RoleFormData,
|
||||||
} from "@/features/dashboard/roles/formSchemas/RoleFormData";
|
} from "@/features/dashboard/roles/formSchemas/RoleFormData";
|
||||||
|
import { showNotification } from "@/utils/notifications";
|
||||||
import {
|
import {
|
||||||
Flex,
|
Flex,
|
||||||
Modal,
|
Modal,
|
||||||
|
|
@ -14,6 +16,7 @@ import {
|
||||||
Checkbox,
|
Checkbox,
|
||||||
} from "@mantine/core";
|
} from "@mantine/core";
|
||||||
import { useForm, zodResolver } from "@mantine/form";
|
import { useForm, zodResolver } from "@mantine/form";
|
||||||
|
import { notifications } from "@mantine/notifications";
|
||||||
import { useRouter } from "next/navigation";
|
import { useRouter } from "next/navigation";
|
||||||
import React, { useState } from "react";
|
import React, { useState } from "react";
|
||||||
import { TbDeviceFloppy } from "react-icons/tb";
|
import { TbDeviceFloppy } from "react-icons/tb";
|
||||||
|
|
@ -23,27 +26,48 @@ interface Props {
|
||||||
readonly?: boolean;
|
readonly?: boolean;
|
||||||
data: RoleFormData;
|
data: RoleFormData;
|
||||||
opened: boolean;
|
opened: boolean;
|
||||||
onClose?: () => void
|
onClose?: () => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function FormModal(props: Props) {
|
export default function FormModal(props: Props) {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
|
||||||
const [isSubmitting, setSubmitting] = useState(false);
|
const [isSubmitting, setSubmitting] = useState(false);
|
||||||
const [value, setValue] = useState("")
|
const [value, setValue] = useState("");
|
||||||
|
|
||||||
const form = useForm<RoleFormData>({
|
const form = useForm<RoleFormData>({
|
||||||
initialValues: props.data,
|
initialValues: props.data,
|
||||||
validate: zodResolver(roleFormDataSchema),
|
validate: zodResolver(roleFormDataSchema),
|
||||||
validateInputOnChange: false,
|
validateInputOnChange: false,
|
||||||
onValuesChange: (values) => {console.log(values)}
|
onValuesChange: (values) => {
|
||||||
|
console.log(values);
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const closeModal = () => {
|
const closeModal = () => {
|
||||||
form.reset()
|
form.reset();
|
||||||
props.onClose ? props.onClose() : router.replace("?");
|
props.onClose ? props.onClose() : router.replace("?");
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const handleSubmit = (values: RoleFormData) => {
|
||||||
|
upsertRole(values)
|
||||||
|
.then((response) => {
|
||||||
|
if (response.success){
|
||||||
|
showNotification(response.message,"success");
|
||||||
|
return closeModal()
|
||||||
|
} else {
|
||||||
|
form.setErrors(response.errors ?? {});
|
||||||
|
if (!response.errors){
|
||||||
|
showNotification(response.message, "error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(e =>{
|
||||||
|
//TODO: Handle Error
|
||||||
|
console.log(e)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Modal
|
<Modal
|
||||||
opened={props.opened}
|
opened={props.opened}
|
||||||
|
|
@ -51,6 +75,7 @@ export default function FormModal(props: Props) {
|
||||||
title={props.title}
|
title={props.title}
|
||||||
scrollAreaComponent={ScrollArea.Autosize}
|
scrollAreaComponent={ScrollArea.Autosize}
|
||||||
>
|
>
|
||||||
|
<form onSubmit={form.onSubmit(handleSubmit)}>
|
||||||
<Stack mt="sm" gap="lg" px="lg">
|
<Stack mt="sm" gap="lg" px="lg">
|
||||||
{/* ID */}
|
{/* ID */}
|
||||||
{props.data.id ? (
|
{props.data.id ? (
|
||||||
|
|
@ -60,7 +85,9 @@ export default function FormModal(props: Props) {
|
||||||
variant="filled"
|
variant="filled"
|
||||||
{...form.getInputProps("id")}
|
{...form.getInputProps("id")}
|
||||||
/>
|
/>
|
||||||
) : <div></div>}
|
) : (
|
||||||
|
<div></div>
|
||||||
|
)}
|
||||||
|
|
||||||
{/* Code */}
|
{/* Code */}
|
||||||
<TextInput
|
<TextInput
|
||||||
|
|
@ -76,7 +103,7 @@ export default function FormModal(props: Props) {
|
||||||
label="Name"
|
label="Name"
|
||||||
readOnly={props.readonly}
|
readOnly={props.readonly}
|
||||||
disabled={isSubmitting}
|
disabled={isSubmitting}
|
||||||
// {...form.getInputProps("name")}
|
{...form.getInputProps("name")}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
{/* Description */}
|
{/* Description */}
|
||||||
|
|
@ -84,25 +111,15 @@ export default function FormModal(props: Props) {
|
||||||
label="Description"
|
label="Description"
|
||||||
readOnly={props.readonly}
|
readOnly={props.readonly}
|
||||||
disabled={isSubmitting}
|
disabled={isSubmitting}
|
||||||
// {...form.getInputProps("description")}
|
{...form.getInputProps("description")}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<Checkbox label="Active" labelPosition="right" />
|
<Checkbox
|
||||||
|
label="Active"
|
||||||
<Switch
|
|
||||||
label="Active jir"
|
|
||||||
labelPosition="right"
|
labelPosition="right"
|
||||||
// {...form.getInputProps("isActive", {
|
{...form.getInputProps("isActive", {
|
||||||
// type: "checkbox",
|
type: "checkbox",
|
||||||
// })}
|
})}
|
||||||
/>
|
|
||||||
|
|
||||||
<Switch
|
|
||||||
label="Active jir"
|
|
||||||
labelPosition="left"
|
|
||||||
// {...form.getInputProps("isActive", {
|
|
||||||
// type: "checkbox",
|
|
||||||
// })}
|
|
||||||
/>
|
/>
|
||||||
|
|
||||||
{/* Buttons */}
|
{/* Buttons */}
|
||||||
|
|
@ -126,6 +143,7 @@ export default function FormModal(props: Props) {
|
||||||
)}
|
)}
|
||||||
</Flex>
|
</Flex>
|
||||||
</Stack>
|
</Stack>
|
||||||
|
</form>
|
||||||
</Modal>
|
</Modal>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -19,10 +19,6 @@ interface Props {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const metadata: Metadata = {
|
|
||||||
title: "Roles",
|
|
||||||
};
|
|
||||||
|
|
||||||
export default async function RolesPage({searchParams}: Props) {
|
export default async function RolesPage({searchParams}: Props) {
|
||||||
if (!(await checkPermission("role.readAll"))) {
|
if (!(await checkPermission("role.readAll"))) {
|
||||||
return unauthorized();
|
return unauthorized();
|
||||||
|
|
|
||||||
68
src/features/dashboard/roles/actions/upsertRole.ts
Normal file
68
src/features/dashboard/roles/actions/upsertRole.ts
Normal file
|
|
@ -0,0 +1,68 @@
|
||||||
|
"use server"
|
||||||
|
import checkPermission from "@/features/auth/tools/checkPermission";
|
||||||
|
import roleFormDataSchema, { RoleFormData } from "../formSchemas/RoleFormData";
|
||||||
|
import { unauthorized } from "@/BaseError";
|
||||||
|
import mapObjectToFirstValue from "@/utils/mapObjectToFirstValue";
|
||||||
|
import prisma from "@/db";
|
||||||
|
import { revalidatePath } from "next/cache";
|
||||||
|
|
||||||
|
export default async function upsertRole(data: RoleFormData){
|
||||||
|
|
||||||
|
const isInsert = !!data.id;
|
||||||
|
|
||||||
|
if (isInsert && !await checkPermission("role.create")){
|
||||||
|
return unauthorized();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isInsert && !await checkPermission("role.update")){
|
||||||
|
return unauthorized();
|
||||||
|
}
|
||||||
|
|
||||||
|
const validatedFields = roleFormDataSchema.safeParse(data);
|
||||||
|
if (!validatedFields.success){
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
message: "Invalid Form Data",
|
||||||
|
errors: mapObjectToFirstValue(validatedFields.error.flatten().fieldErrors)
|
||||||
|
} as const
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update user data in the database
|
||||||
|
try {
|
||||||
|
if (isInsert){
|
||||||
|
await prisma.role.update({
|
||||||
|
where: { id: validatedFields.data.id!},
|
||||||
|
data: {
|
||||||
|
code: validatedFields.data.code,
|
||||||
|
description: validatedFields.data.description,
|
||||||
|
isActive: validatedFields.data.isActive,
|
||||||
|
name: validatedFields.data.name
|
||||||
|
},
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
await prisma.role.create({
|
||||||
|
data: {
|
||||||
|
code: validatedFields.data.code,
|
||||||
|
description: validatedFields.data.description,
|
||||||
|
name: validatedFields.data.name,
|
||||||
|
isActive: validatedFields.data.isActive
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Revalidate the cache
|
||||||
|
revalidatePath(".");
|
||||||
|
|
||||||
|
return {
|
||||||
|
success: true,
|
||||||
|
message: `Role ${validatedFields.data.name} has been successfully ${isInsert ? "Updated" : "Created"}`
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
// Consider handling specific database errors here
|
||||||
|
console.error('Error updating user data', error);
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
message: "Error updating user data"
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user