Updated and Fix UserRole and User Forms
This commit is contained in:
@@ -4,9 +4,10 @@ import { Form } from "@/components/ui/form";
|
|||||||
import { Button } from "@/components/ui/button";
|
import { Button } from "@/components/ui/button";
|
||||||
import { Loader2 } from "lucide-react";
|
import { Loader2 } from "lucide-react";
|
||||||
import { UseFormReturn } from "react-hook-form";
|
import { UseFormReturn } from "react-hook-form";
|
||||||
import UserProfilePictureField from "./UserProfilePictureField";
|
import { UserProfilePictureField } from "./";
|
||||||
import UserTextField from "./UserTextField";
|
import { UserTextField } from "./";
|
||||||
import UserToggleField from "./UserToggleField";
|
import { UserToggleField } from "./";
|
||||||
|
import { UserRoleField } from "./";
|
||||||
import { DialogFooter } from "@/components/ui/dialog";
|
import { DialogFooter } from "@/components/ui/dialog";
|
||||||
|
|
||||||
interface AddUserFormProps {
|
interface AddUserFormProps {
|
||||||
@@ -44,11 +45,10 @@ const AddUserForm = ({ form, onSubmit, isSubmitting }: AddUserFormProps) => {
|
|||||||
placeholder="Enter username"
|
placeholder="Enter username"
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<UserTextField
|
<UserRoleField
|
||||||
control={form.control}
|
control={form.control}
|
||||||
name="role"
|
name="role"
|
||||||
label="Role"
|
label="Role"
|
||||||
placeholder="Enter role (e.g. admin, user)"
|
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<UserTextField
|
<UserTextField
|
||||||
|
|||||||
@@ -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<any>;
|
||||||
|
name: string;
|
||||||
|
label: string;
|
||||||
|
disabled?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
const UserRoleField = ({ control, name, label, disabled = false }: UserRoleFieldProps) => {
|
||||||
|
return (
|
||||||
|
<FormField
|
||||||
|
control={control}
|
||||||
|
name={name}
|
||||||
|
render={({ field }) => (
|
||||||
|
<FormItem>
|
||||||
|
<FormLabel>{label}</FormLabel>
|
||||||
|
<FormControl>
|
||||||
|
<Select
|
||||||
|
value={field.value}
|
||||||
|
onValueChange={field.onChange}
|
||||||
|
disabled={disabled}
|
||||||
|
>
|
||||||
|
<SelectTrigger className="w-full">
|
||||||
|
<SelectValue placeholder="Select role" />
|
||||||
|
</SelectTrigger>
|
||||||
|
<SelectContent>
|
||||||
|
{userRoles.map(role => (
|
||||||
|
<SelectItem key={role.value} value={role.value}>
|
||||||
|
{role.label}
|
||||||
|
</SelectItem>
|
||||||
|
))}
|
||||||
|
</SelectContent>
|
||||||
|
</Select>
|
||||||
|
</FormControl>
|
||||||
|
</FormItem>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default UserRoleField;
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
|
|
||||||
export { default as UserProfilePictureField } from './UserProfilePictureField';
|
export { default as UserTextField } from "./UserTextField";
|
||||||
export { default as UserTextField } from './UserTextField';
|
export { default as UserToggleField } from "./UserToggleField";
|
||||||
export { default as UserToggleField } from './UserToggleField';
|
export { default as UserProfilePictureField } from "./UserProfilePictureField";
|
||||||
export { default as AddUserForm } from './AddUserForm';
|
export { default as UserRoleField } from "./UserRoleField";
|
||||||
@@ -1,6 +1,12 @@
|
|||||||
|
|
||||||
import * as z from "zod";
|
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({
|
export const userFormSchema = z.object({
|
||||||
full_name: z.string().min(2, {
|
full_name: z.string().min(2, {
|
||||||
message: "Name must be at least 2 characters.",
|
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.",
|
message: "Username must be at least 3 characters.",
|
||||||
}),
|
}),
|
||||||
isActive: z.boolean().optional(),
|
isActive: z.boolean().optional(),
|
||||||
role: z.string().optional(),
|
role: z.string().min(1, {
|
||||||
|
message: "Please select a role",
|
||||||
|
}),
|
||||||
avatar: z.string().optional(),
|
avatar: z.string().optional(),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user