import { useRequest, useToggle } from 'ahooks' import { PatternFormat } from 'react-number-format' import { ExternalLinkIcon, PencilIcon, SearchIcon } from 'lucide-react' import { Controller, useForm } from 'react-hook-form' import { zodResolver } from '@hookform/resolvers/zod' import { z } from 'zod' import valid from 'card-validator' import { Currency } from '@repo/ui/components/currency' import { Button } from '@repo/ui/components/ui/button' import { Separator } from '@repo/ui/components/ui/separator' import { Spinner } from '@repo/ui/components/ui/spinner' import { Table, TableBody, TableCell, TableFooter, TableHead, TableHeader, TableRow } from '@repo/ui/components/ui/table' import { Item, ItemActions, ItemContent, ItemDescription, ItemGroup, ItemTitle } from '@repo/ui/components/ui/item' import { Dialog, DialogClose, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger } from '@repo/ui/components/ui/dialog' import { useWizard } from '@/components/wizard' import { type WizardState } from './route' import { applyDiscount } from './discount' import { paymentMethods } from '@repo/ui/routes/orders/data' import { Field, FieldDescription, FieldError, FieldGroup, FieldLabel, FieldSet } from '@repo/ui/components/ui/field' import { Input } from '@repo/ui/components/ui/input' import { InputGroup, InputGroupAddon, InputGroupButton, InputGroupInput } from '@repo/ui/components/ui/input-group' type ReviewProps = { state: WizardState onSubmit: (value: WizardState) => void | Promise } export function Review({ state, onSubmit }: ReviewProps) { const wizard = useWizard() const [loading, { set }] = useToggle() const { coupon, items } = state || { items: [], coupon: {} } const subtotal = items?.reduce( (acc, { course, quantity }) => acc + (course?.unit_price || 0) * (Number.isFinite(quantity) && quantity > 0 ? quantity : 1), 0 ) || 0 const discount = coupon ? applyDiscount(subtotal, coupon.amount, coupon.type) * -1 : 0 const total = subtotal > 0 ? subtotal + discount : 0 return ( <>
{ e.preventDefault() set(true) await onSubmit(state) }} className="space-y-4" > Curso Quantidade Valor unit. Total {items?.map(({ course, quantity }, index) => { return ( {course.name} {quantity} {course.unit_price} {course.unit_price * quantity} ) })} Subtotal {subtotal} Descontos {discount} Total {total}
) } export function Address({ total, payment_method, credit_card }: WizardState & { total: number }) { const numberValidation = valid.number(credit_card?.number) return ( Endereço de cobrança
    Rua Monsenhor Ivo Zanlorenzi, nº 5190, ap 1802
    Cidade Industrial
    Curitiba, Paraná
    81280-350
Forma de pagamento {credit_card ? ( <> {numberValidation.card?.niceType} (Crédito) ****{' '} {credit_card.number.slice(-4)}
1x {total} ) : ( <> {payment_method ? paymentMethods[payment_method] : payment_method} )}
) } const formSchema = z.object({ postcode: z.string().min(1, 'Digite um CEP'), address1: z.string().min(1, 'Digite um endereço'), address2: z.string().optional(), neighborhood: z.string().min(1, 'Digite o bairro'), city: z.string().min(1, 'Digite a cidade'), state: z.string().min(1, 'Digite o estado') }) type Address = z.infer export function AddressDialog() { const [open, { toggle, set }] = useToggle() const { handleSubmit, control, reset } = useForm({ resolver: zodResolver(formSchema) }) const onSubmit = async (data: Address) => { set(false) console.log(data) } return ( Editar endereço Este endereço será utilizado para a emissão da NFS-e.
( Endereço {fieldState.invalid && ( )} )} /> ( Complemento{' '} (opcional) {fieldState.invalid && ( )} )} />
{/* Neighborhood */} ( Bairro {fieldState.invalid && ( )} )} /> {/* City */} ( Cidade {fieldState.invalid && ( )} )} /> {/* State */} ( Estado {fieldState.invalid && ( )} )} />
) } type PostcodeFormProps = { onChange: (value: Address) => void } function PostcodeForm({ onChange }: PostcodeFormProps) { const formSchema = z.object({ postcode: z.string().min(1, 'Digite um CEP') }) type Schema = z.infer const { control, handleSubmit, setError, formState } = useForm({ resolver: zodResolver(formSchema) }) const { runAsync, loading } = useRequest( async (cep: string) => { return await fetch(`/~/api/cep/${cep}.json`) }, { manual: true } ) const onSubmit = async ({ postcode }: Schema) => { const r = await runAsync(postcode) if (r.ok) { onChange?.((await r.json()) as Address) return } setError('postcode', { message: 'CEP não encontrado' }) } return (
( {/* CEP */} CEP { onChange(value) }} {...field} /> {loading ? : } {fieldState.invalid && } Não sei o CEP )} />
) }