176 lines
No EOL
5.3 KiB
TypeScript
176 lines
No EOL
5.3 KiB
TypeScript
'use client';
|
|
|
|
import z from 'zod';
|
|
import { useEffect } from 'react';
|
|
import { useForm, Controller } from 'react-hook-form';
|
|
import { zodResolver } from '@hookform/resolvers/zod';
|
|
|
|
import { Button } from '@/components/ui/button';
|
|
import { Checkbox } from '@/components/ui/checkbox';
|
|
import {
|
|
Dialog,
|
|
DialogClose,
|
|
DialogContent,
|
|
DialogDescription,
|
|
DialogFooter,
|
|
DialogHeader,
|
|
DialogTitle,
|
|
} from '@/components/ui/dialog';
|
|
import {
|
|
Form,
|
|
FormControl,
|
|
FormField,
|
|
FormItem,
|
|
FormLabel,
|
|
FormMessage,
|
|
} from '@/components/ui/form';
|
|
import { Input } from '@/components/ui/input';
|
|
import { Label } from '@/components/ui/label';
|
|
import { SituacoesEnum } from '@/shared/enums/SituacoesEnum';
|
|
import { UserSchema } from '../../schemas/User/UserSchema';
|
|
import LoadingButton from '@/shared/components/loadingButton/LoadingButton';
|
|
|
|
type FormValues = z.infer<typeof UserSchema>;
|
|
|
|
interface Props {
|
|
isOpen: boolean;
|
|
data: FormValues | null;
|
|
onClose: (item: null, isFormStatus: boolean) => void;
|
|
onSave: (data: FormValues) => void;
|
|
buttonIsLoading: boolean;
|
|
}
|
|
|
|
export default function UserForm({ isOpen, data, onClose, onSave, buttonIsLoading }: Props) {
|
|
const form = useForm<FormValues>({
|
|
resolver: zodResolver(UserSchema),
|
|
defaultValues: {
|
|
user_id: 0,
|
|
name: '',
|
|
email: '',
|
|
password: '',
|
|
team: '',
|
|
position: 'string',
|
|
status: SituacoesEnum.ATIVO,
|
|
user_id_create: null,
|
|
user_id_update: null
|
|
},
|
|
});
|
|
|
|
// Atualiza o formulário quando recebe dados para edição
|
|
useEffect(() => {
|
|
if (data) form.reset(data);
|
|
}, [data, form]);
|
|
|
|
return (
|
|
<Dialog
|
|
open={isOpen}
|
|
onOpenChange={(open) => {
|
|
if (!open) onClose(null, false);
|
|
}}
|
|
>
|
|
<DialogContent className="sm:max-w-[425px]">
|
|
<DialogHeader>
|
|
<DialogTitle>{data?.user_id ? 'Editar Usuário' : 'Novo Usuário'}</DialogTitle>
|
|
<DialogDescription>Gerencie os dados do usuário aqui.</DialogDescription>
|
|
</DialogHeader>
|
|
|
|
<Form {...form}>
|
|
<form onSubmit={form.handleSubmit(onSave)} className="space-y-6">
|
|
{/* Nome */}
|
|
<FormField
|
|
control={form.control}
|
|
name="name"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>Nome</FormLabel>
|
|
<FormControl>
|
|
<Input {...field} placeholder="Digite o nome completo" />
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
{/* Email */}
|
|
<FormField
|
|
control={form.control}
|
|
name="email"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>Email</FormLabel>
|
|
<FormControl>
|
|
<Input type="email" {...field} placeholder="exemplo@email.com" />
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
{/* Senha */}
|
|
<FormField
|
|
control={form.control}
|
|
name="password"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>Senha</FormLabel>
|
|
<FormControl>
|
|
<Input type="password" {...field} placeholder={data ? 'Deixe em branco para não alterar' : 'Digite a senha'} />
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
{/* Equipe */}
|
|
<FormField
|
|
control={form.control}
|
|
name="team"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>Equipe</FormLabel>
|
|
<FormControl>
|
|
<Input {...field} placeholder="Digite o nome da equipe" />
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
{/* Status */}
|
|
<Controller
|
|
name="status"
|
|
control={form.control}
|
|
render={({ field }) => (
|
|
<div className="flex items-center space-x-2">
|
|
<Checkbox
|
|
checked={field.value === 'A'}
|
|
onCheckedChange={(checked) => field.onChange(checked ? 'A' : 'I')}
|
|
/>
|
|
<Label>Ativo</Label>
|
|
</div>
|
|
)}
|
|
/>
|
|
{/* Rodapé */}
|
|
<DialogFooter className="mt-4">
|
|
<DialogClose asChild>
|
|
<Button
|
|
variant="outline"
|
|
type="button"
|
|
onClick={() => onClose(null, false)}
|
|
className="cursor-pointer"
|
|
>
|
|
Cancelar
|
|
</Button>
|
|
</DialogClose>
|
|
<LoadingButton
|
|
text={data?.user_id ? 'Salvar' : 'Cadastrar'}
|
|
textLoading="Aguarde..."
|
|
type="submit"
|
|
loading={buttonIsLoading}
|
|
/>
|
|
</DialogFooter>
|
|
{/* Campo oculto para o ID */}
|
|
<input type="hidden" {...form.register('user_id')} />
|
|
<input type="hidden" {...form.register('position')} />
|
|
</form>
|
|
</Form>
|
|
</DialogContent>
|
|
</Dialog>
|
|
);
|
|
} |