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

@@ -33,7 +33,7 @@ import { Abbr } from '@repo/ui/components/abbr'
import { Cell } from '../_.$orgid.enrollments.add/route'
import { CoursePicker } from '../_.$orgid.enrollments.add/course-picker'
import { MAX_ITEMS, type Course } from '../_.$orgid.enrollments.add/data'
import { Discount, applyDiscount } from './discount'
import { Discount, applyDiscount, type Coupon } from './discount'
import { currency } from './utils'
import { useWizard } from '@/components/wizard'
@@ -41,13 +41,6 @@ const emptyRow = {
course: undefined
}
type BulkProps = {
onSubmit: (value: any) => void | Promise<void>
courses: Promise<{ hits: Course[] }>
items?: object[]
coupon?: object
}
const item = z.object({
course: z
.object(
@@ -76,17 +69,26 @@ const formSchema = z.object({
type Schema = z.infer<typeof formSchema>
export type Item = z.infer<typeof item>
type BulkProps = {
onSubmit: (value: any) => void | Promise<void>
courses: Promise<{ hits: Course[] }>
items: Item[]
coupon?: Coupon
}
export function Bulk({
courses,
onSubmit,
items: itemInit,
items: itemsInit,
coupon: couponInit
}: BulkProps) {
const wizard = useWizard()
const form = useForm({
resolver: zodResolver(formSchema),
defaultValues: {
items: itemInit || [emptyRow],
items: itemsInit?.length ? itemsInit : [emptyRow],
coupon: couponInit
}
})
@@ -117,8 +119,22 @@ export function Bulk({
0
)
const onSubmit_ = async (data: Schema) => {
await onSubmit(data)
const onSubmit_ = async ({ coupon, ...data }: Schema) => {
const items = Object.values(
data.items.reduce<Record<string, Item>>((acc, item) => {
const id = item.course.id
if (!acc[id]) {
acc[id] = { ...item }
} else {
acc[id].quantity += item.quantity
}
return acc
}, {})
)
await onSubmit({ coupon, items, enrollments: [] })
wizard('payment')
}
@@ -403,11 +419,11 @@ export function Summary({ subtotal, coupon, setValue }: SummaryProps) {
) : (
<Discount
disabled={subtotal === 0}
onChange={({ sk, discount_amount, discount_type }) => {
onChange={(coupon) => {
setValue('coupon', {
code: sk,
amount: discount_amount,
type: discount_type
// code: sk,
// amount: discount_amount,
// type: discount_type
})
}}
/>