From c8323e91b0306a0eb8ba6bfab04bf34afd800f91 Mon Sep 17 00:00:00 2001 From: Tola Leng Date: Sat, 24 May 2025 03:47:32 +0800 Subject: [PATCH] Updated and Fix UserRole and User Forms --- .../form-fields/AddUserForm.tsx | 12 ++--- .../form-fields/UserRoleField.tsx | 47 +++++++++++++++++++ .../user-management/form-fields/index.ts | 8 ++-- .../settings/user-management/userForms.ts | 12 ++++- 4 files changed, 67 insertions(+), 12 deletions(-) create mode 100644 application/src/components/settings/user-management/form-fields/UserRoleField.tsx diff --git a/application/src/components/settings/user-management/form-fields/AddUserForm.tsx b/application/src/components/settings/user-management/form-fields/AddUserForm.tsx index 7ad75de..95e81b8 100644 --- a/application/src/components/settings/user-management/form-fields/AddUserForm.tsx +++ b/application/src/components/settings/user-management/form-fields/AddUserForm.tsx @@ -4,9 +4,10 @@ import { Form } from "@/components/ui/form"; import { Button } from "@/components/ui/button"; import { Loader2 } from "lucide-react"; import { UseFormReturn } from "react-hook-form"; -import UserProfilePictureField from "./UserProfilePictureField"; -import UserTextField from "./UserTextField"; -import UserToggleField from "./UserToggleField"; +import { UserProfilePictureField } from "./"; +import { UserTextField } from "./"; +import { UserToggleField } from "./"; +import { UserRoleField } from "./"; import { DialogFooter } from "@/components/ui/dialog"; interface AddUserFormProps { @@ -44,11 +45,10 @@ const AddUserForm = ({ form, onSubmit, isSubmitting }: AddUserFormProps) => { placeholder="Enter username" /> - { ); }; -export default AddUserForm; +export default AddUserForm; \ No newline at end of file diff --git a/application/src/components/settings/user-management/form-fields/UserRoleField.tsx b/application/src/components/settings/user-management/form-fields/UserRoleField.tsx new file mode 100644 index 0000000..8e8853b --- /dev/null +++ b/application/src/components/settings/user-management/form-fields/UserRoleField.tsx @@ -0,0 +1,47 @@ + +import React from "react"; +import { Control } from "react-hook-form"; +import { FormControl, FormField, FormItem, FormLabel } from "@/components/ui/form"; +import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; +import { userRoles } from "../userForms"; + +interface UserRoleFieldProps { + control: Control; + name: string; + label: string; + disabled?: boolean; +} + +const UserRoleField = ({ control, name, label, disabled = false }: UserRoleFieldProps) => { + return ( + ( + + {label} + + + + + )} + /> + ); +}; + +export default UserRoleField; \ No newline at end of file diff --git a/application/src/components/settings/user-management/form-fields/index.ts b/application/src/components/settings/user-management/form-fields/index.ts index 997b14b..d85b438 100644 --- a/application/src/components/settings/user-management/form-fields/index.ts +++ b/application/src/components/settings/user-management/form-fields/index.ts @@ -1,5 +1,5 @@ -export { default as UserProfilePictureField } from './UserProfilePictureField'; -export { default as UserTextField } from './UserTextField'; -export { default as UserToggleField } from './UserToggleField'; -export { default as AddUserForm } from './AddUserForm'; +export { default as UserTextField } from "./UserTextField"; +export { default as UserToggleField } from "./UserToggleField"; +export { default as UserProfilePictureField } from "./UserProfilePictureField"; +export { default as UserRoleField } from "./UserRoleField"; \ No newline at end of file diff --git a/application/src/components/settings/user-management/userForms.ts b/application/src/components/settings/user-management/userForms.ts index 27c08b5..c90e1d9 100644 --- a/application/src/components/settings/user-management/userForms.ts +++ b/application/src/components/settings/user-management/userForms.ts @@ -1,6 +1,12 @@ import * as z from "zod"; +// Define the available roles +export const userRoles = [ + { label: "Admin", value: "admin" }, + { label: "Super Admin", value: "superadmin" } +]; + export const userFormSchema = z.object({ full_name: z.string().min(2, { message: "Name must be at least 2 characters.", @@ -12,7 +18,9 @@ export const userFormSchema = z.object({ message: "Username must be at least 3 characters.", }), isActive: z.boolean().optional(), - role: z.string().optional(), + role: z.string().min(1, { + message: "Please select a role", + }), avatar: z.string().optional(), }); @@ -29,4 +37,4 @@ export const newUserFormSchema = userFormSchema.extend({ }); export type UserFormValues = z.infer; -export type NewUserFormValues = z.infer; +export type NewUserFormValues = z.infer; \ No newline at end of file