update form
This commit is contained in:
@@ -1,9 +1,17 @@
|
||||
import type { Route } from './+types'
|
||||
|
||||
import {
|
||||
Form,
|
||||
FormControl,
|
||||
FormField,
|
||||
FormItem,
|
||||
FormLabel,
|
||||
FormMessage
|
||||
} from '@/components/ui/form'
|
||||
import { isValidCPF } from '@brazilian-utils/brazilian-utils'
|
||||
import { zodResolver } from '@hookform/resolvers/zod'
|
||||
import { Loader2Icon } from 'lucide-react'
|
||||
import { useState } from 'react'
|
||||
import { useEffect, useState } from 'react'
|
||||
import { useForm } from 'react-hook-form'
|
||||
import { useFetcher } from 'react-router'
|
||||
import { z } from 'zod'
|
||||
@@ -16,12 +24,18 @@ import * as httpStatus from '@/lib/http-status'
|
||||
|
||||
import logo from '@/components/logo.svg'
|
||||
|
||||
const cpf = z.string().refine(isValidCPF, { message: 'CPF inválido' })
|
||||
const email = z.string().email({ message: 'Email inválido' })
|
||||
|
||||
const schema = z.object({
|
||||
username: z.union([cpf, email]),
|
||||
password: z.string().nonempty()
|
||||
username: z
|
||||
.string()
|
||||
.nonempty('Digite um Email ou CPF')
|
||||
.refine((val) => {
|
||||
const onlyDigits = val.replace(/\D/g, '')
|
||||
|
||||
return onlyDigits.length === 11
|
||||
? isValidCPF(val)
|
||||
: z.string().email().safeParse(val).success
|
||||
}, 'Deve ser um Email ou CPF válido'),
|
||||
password: z.string().nonempty('Digite uma senha')
|
||||
})
|
||||
|
||||
type Schema = z.infer<typeof schema>
|
||||
@@ -43,6 +57,13 @@ export async function action({ request, context }: Route.ActionArgs) {
|
||||
body: JSON.stringify(formData)
|
||||
})
|
||||
|
||||
if (r.status !== httpStatus.OK) {
|
||||
return Response.json(await r.json(), {
|
||||
status: r.status,
|
||||
headers: r.headers
|
||||
})
|
||||
}
|
||||
|
||||
const url = new URL(request.url)
|
||||
url.pathname = '/authorize'
|
||||
|
||||
@@ -54,7 +75,7 @@ export async function action({ request, context }: Route.ActionArgs) {
|
||||
headers
|
||||
})
|
||||
} catch {
|
||||
return new Response(null, { status: httpStatus.INTERNAL_SERVER })
|
||||
return Response.json({}, { status: httpStatus.INTERNAL_SERVER })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,14 +83,35 @@ export default function Index({}: Route.ComponentProps) {
|
||||
const [show, setShow] = useState(false)
|
||||
const fetcher = useFetcher()
|
||||
|
||||
const { register, handleSubmit, formState } = useForm({
|
||||
resolver: zodResolver(schema)
|
||||
const form = useForm({
|
||||
resolver: zodResolver(schema),
|
||||
defaultValues: { username: '', password: '' }
|
||||
})
|
||||
const { control, handleSubmit, formState, setError } = form
|
||||
|
||||
const onSubmit = async (data: Schema) => {
|
||||
await fetcher.submit(data, { method: 'post' })
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
if (fetcher.state === 'idle' && fetcher.data) {
|
||||
const message = fetcher.data?.message
|
||||
|
||||
switch (message) {
|
||||
case 'User not found':
|
||||
return setError('username', {
|
||||
message: 'Usuário não encontrado',
|
||||
type: 'manual'
|
||||
})
|
||||
case 'Invalid credentials':
|
||||
return setError('password', {
|
||||
message: 'A senha está incorreta',
|
||||
type: 'manual'
|
||||
})
|
||||
}
|
||||
}
|
||||
}, [fetcher.state, fetcher.data])
|
||||
|
||||
return (
|
||||
<>
|
||||
<div className="w-full max-w-xs grid gap-6">
|
||||
@@ -79,66 +121,80 @@ export default function Index({}: Route.ComponentProps) {
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<form onSubmit={handleSubmit(onSubmit)}>
|
||||
<div className="grid gap-6">
|
||||
<div className="text-center space-y-1.5">
|
||||
<h1 className="text-2xl font-semibold font-display text-balance">
|
||||
Faça login
|
||||
</h1>
|
||||
<p className="text-white/50 text-sm">
|
||||
Não tem uma conta?{' '}
|
||||
<a href="#" className="font-medium text-white">
|
||||
Cadastre-se
|
||||
</a>
|
||||
.
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div className="grid gap-3">
|
||||
<Label htmlFor="email">Email ou CPF</Label>
|
||||
<Input id="email" {...register('username')} />
|
||||
</div>
|
||||
|
||||
<div className="grid gap-3">
|
||||
<div className="flex items-center">
|
||||
<Label htmlFor="password">Senha</Label>
|
||||
<a
|
||||
href="#"
|
||||
tabIndex={-1}
|
||||
className="ml-auto text-sm underline-offset-4 hover:underline"
|
||||
>
|
||||
Esqueceu sua senha?
|
||||
</a>
|
||||
<Form {...form}>
|
||||
<form onSubmit={handleSubmit(onSubmit)}>
|
||||
<div className="grid gap-6">
|
||||
<div className="text-center space-y-1.5">
|
||||
<h1 className="text-2xl font-semibold font-display text-balance">
|
||||
Faça login
|
||||
</h1>
|
||||
<p className="text-white/50 text-sm">
|
||||
Não tem uma conta?{' '}
|
||||
<a href="#" className="font-medium text-white">
|
||||
Cadastre-se
|
||||
</a>
|
||||
.
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<Input
|
||||
id="password"
|
||||
type={show ? 'text' : 'password'}
|
||||
{...register('password')}
|
||||
<FormField
|
||||
control={control}
|
||||
name="username"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>Email ou CPF</FormLabel>
|
||||
<FormControl>
|
||||
<Input {...field} />
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
|
||||
<div className="flex items-center gap-3">
|
||||
<Checkbox
|
||||
id="showPassword"
|
||||
onClick={() => setShow((x) => !x)}
|
||||
tabIndex={-1}
|
||||
/>
|
||||
<Label htmlFor="showPassword">Mostrar senha</Label>
|
||||
</div>
|
||||
</div>
|
||||
<FormField
|
||||
control={control}
|
||||
name="password"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<div className="flex">
|
||||
<FormLabel>Senha</FormLabel>
|
||||
<a
|
||||
href="#"
|
||||
tabIndex={-1}
|
||||
className="ml-auto text-sm underline-offset-4 hover:underline"
|
||||
>
|
||||
Esqueceu sua senha?
|
||||
</a>
|
||||
</div>
|
||||
<FormControl>
|
||||
<Input type={show ? 'text' : 'password'} {...field} />
|
||||
</FormControl>
|
||||
<div className="flex items-center gap-3">
|
||||
<Checkbox
|
||||
id="showPassword"
|
||||
onClick={() => setShow((x) => !x)}
|
||||
tabIndex={-1}
|
||||
/>
|
||||
<Label htmlFor="showPassword">Mostrar senha</Label>
|
||||
</div>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
|
||||
<Button
|
||||
type="submit"
|
||||
className="w-full bg-lime-400 cursor-pointer"
|
||||
disabled={formState.isSubmitting}
|
||||
>
|
||||
{formState.isSubmitting && (
|
||||
<Loader2Icon className="animate-spin" />
|
||||
)}
|
||||
Entrar
|
||||
</Button>
|
||||
</div>
|
||||
</form>
|
||||
<Button
|
||||
type="submit"
|
||||
className="w-full bg-lime-400 cursor-pointer"
|
||||
disabled={formState.isSubmitting}
|
||||
>
|
||||
{formState.isSubmitting && (
|
||||
<Loader2Icon className="animate-spin" />
|
||||
)}
|
||||
Entrar
|
||||
</Button>
|
||||
</div>
|
||||
</form>
|
||||
</Form>
|
||||
|
||||
<p className="text-white/50 text-xs text-center">
|
||||
Ao fazer login, você concorda com nossa{' '}
|
||||
|
||||
Reference in New Issue
Block a user