add zustard

This commit is contained in:
2025-12-30 14:37:53 -03:00
parent 58068cd463
commit bad7e15f46
15 changed files with 349 additions and 280 deletions

View File

@@ -37,11 +37,11 @@ import {
DialogTitle,
DialogTrigger
} from '@repo/ui/components/ui/dialog'
import { paymentMethods } from '@repo/ui/routes/orders/data'
import { useWizard } from '@/components/wizard'
import { type WizardState } from './route'
import { type WizardState } from './store'
import { applyDiscount } from './discount'
import { paymentMethods } from '@repo/ui/routes/orders/data'
import {
Field,
FieldDescription,
@@ -57,46 +57,28 @@ import {
InputGroupButton,
InputGroupInput
} from '@repo/ui/components/ui/input-group'
import { calcInterest } from './payment'
import { useWizardStore } from './store'
type ReviewProps = {
state: WizardState
onSubmit: (value: WizardState) => void | Promise<void>
}
export function Review({ state, onSubmit }: ReviewProps) {
export function Review({ onSubmit }: ReviewProps) {
const wizard = useWizard()
const { items, summary } = useWizardStore()
const { subtotal, discount, interest_amount, total } = summary()
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
const installments = state.installments
const interest_amount =
state.payment_method === 'CREDIT_CARD' &&
typeof installments === 'number' &&
installments > 1
? calcInterest(total, installments) - total
: 0
return (
<>
<Address total={total} {...state} />
<Summary />
<form
onSubmit={async (e) => {
e.preventDefault()
set(true)
await onSubmit(state)
// await onSubmit(state)
}}
className="space-y-4"
>
@@ -164,7 +146,7 @@ export function Review({ state, onSubmit }: ReviewProps) {
Total
</TableCell>
<TableCell>
<Currency>{total + interest_amount}</Currency>
<Currency>{total}</Currency>
</TableCell>
</TableRow>
</TableFooter>
@@ -193,11 +175,10 @@ export function Review({ state, onSubmit }: ReviewProps) {
)
}
export function Address({
total,
payment_method,
credit_card
}: WizardState & { total: number }) {
export function Summary() {
const { summary, credit_card, payment_method, installments } =
useWizardStore()
const { total } = summary()
const numberValidation = valid.number(credit_card?.number)
return (
@@ -229,7 +210,8 @@ export function Address({
{numberValidation.card?.niceType} (Crédito) ****{' '}
{credit_card.number.slice(-4)}
<br />
1x <Currency>{total}</Currency>
{installments}x{' '}
<Currency>{total / Number(installments)}</Currency>
</>
) : (
<>