"use client"; import { Button } from "@/shared/components/ui/button"; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from "@/shared/components/ui/form"; import { Input } from "@/shared/components/ui/input"; import { Switch } from "@/shared/components/ui/switch"; import { ImageUpload } from "@/shared/components/image-upload"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/shared/components/ui/select"; import { useForm } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import { z } from "zod"; import { userSchema } from "@/shared/schemas/user"; // You should define this Zod schema import { useQuery } from "@tanstack/react-query"; import organizationApi from "@/shared/services/organization"; import { Organization } from "@/shared/types/organization"; import { Role } from "@/shared/types/role"; import roleApi from "@/shared/services/role"; import { getRoleLabelById } from "@/shared/config/role"; import { User } from "@/shared/types/user"; type UserFormValues = z.infer; // Helper component for required field labels const RequiredFormLabel = ({ children }: { children: React.ReactNode }) => ( {children} * ); interface UserFormProps { defaultValues?: Partial; onSubmitAction: (data: UserFormValues) => void; isSubmitting?: boolean; onCancelAction?: () => void; isAdministrator?: boolean; } export function UserForm({ defaultValues, onSubmitAction, isSubmitting, onCancelAction, isAdministrator = false, }: UserFormProps) { const form = useForm({ resolver: zodResolver(userSchema), defaultValues: { name: defaultValues?.name || "", email: defaultValues?.email || "", username: defaultValues?.username || "", employee_id: defaultValues?.employee_id || "", position: defaultValues?.position || "", profile_picture: defaultValues?.profile_picture || "", role_id: defaultValues?.role?.id || "", organization_id: defaultValues?.organization?.id || "", is_active: defaultValues?.is_active ?? true, }, }); const { data: organizations, isLoading: isOrgLoading } = useQuery< Organization[] >({ queryKey: ["organizations"], queryFn: () => organizationApi.getOrganizations().then((response) => response.items), }); const { data: roles, isLoading: isRolesLoading } = useQuery({ queryKey: ["roles"], queryFn: () => roleApi.getRoles().then((response) => response.items), }); return (
( Nama Lengkap )} /> ( Username )} /> ( Password )} /> ( Konfirmasi Password )} /> ( Email )} /> ( NIP )} /> ( Jabatan )} /> ( Role )} /> ( Organisasi )} /> ( Foto Profil field.onChange("")} /> )} /> (
Status
Aktifkan atau nonaktifkan pengguna
)} />
{onCancelAction && ( )}
); }