This commit is contained in:
2025-12-26 18:26:09 -03:00
parent 3cdded360f
commit d0dcc0a953
11 changed files with 574 additions and 460 deletions

View File

@@ -21,17 +21,23 @@ import {
FormMessage
} from '@repo/ui/components/ui/form'
export const formSchema = z.object({
const formSchema = z.object({
coupon: z.string().min(3, { error: 'Digite um cupom válido' }).trim()
})
export type Schema = z.infer<typeof formSchema>
type Schema = z.infer<typeof formSchema>
export type Coupon = {
code: string
amount: number
type: 'FIXED' | 'PERCENT'
}
interface DiscountProps extends Omit<
ButtonHTMLAttributes<HTMLButtonElement>,
'onChange'
> {
onChange?: (value: any) => void
onChange?: (value: Coupon) => void
disabled?: boolean
}
@@ -56,7 +62,17 @@ export function Discount({ onChange, ...props }: DiscountProps) {
if (!r.ok) {
return setError('coupon', { message: 'Cupom inválido' })
}
onChange?.(await r.json())
const {
sk: code,
discount_amount: amount,
discount_type: type
} = (await r.json()) as {
sk: string
discount_amount: number
discount_type: 'FIXED' | 'PERCENT'
}
onChange?.({ code, amount, type })
reset()
set(false)