update items

This commit is contained in:
2025-12-26 11:19:14 -03:00
parent e7aa6a6694
commit 3cdded360f
11 changed files with 392 additions and 26 deletions

View File

@@ -30,6 +30,7 @@ import {
import {
MAX_ITEMS,
formSchema,
type Enrollment,
type Course,
type User
} from '../_.$orgid.enrollments.add/data'
@@ -38,7 +39,6 @@ import { Cell } from '../_.$orgid.enrollments.add/route'
import { CoursePicker } from '../_.$orgid.enrollments.add/course-picker'
import { UserPicker } from '../_.$orgid.enrollments.add/user-picker'
import { Summary } from './bulk'
import { applyDiscount } from './discount'
import { currency } from './utils'
import { useWizard } from '@/components/wizard'
@@ -48,6 +48,11 @@ const emptyRow = {
scheduled_for: undefined
}
type Item = {
course: Enrollment['course']
quantity: number
}
const formSchemaAssigned = formSchema.extend({
coupon: z
.object({
@@ -101,8 +106,6 @@ export function Assigned({
0
)
console.log(coupon)
const onSearch = async (search: string) => {
const params = new URLSearchParams({ q: search })
const r = await fetch(`/${orgid}/users.json?${params.toString()}`)
@@ -110,15 +113,25 @@ export function Assigned({
return hits
}
const onSubmit_ = async (data: Schema) => {
await onSubmit(data)
const onSubmit_ = async ({ enrollments }: Schema) => {
const items = Object.values(
enrollments.reduce<Record<string, Item>>((acc, e) => {
const id = e.course.id
acc[id] ??= { course: e.course, quantity: 0 }
acc[id].quantity++
return acc
}, {})
)
await onSubmit({ enrollments, items })
wizard('payment')
}
return (
<Form {...form}>
<form onSubmit={handleSubmit(onSubmit_)} className="space-y-4">
<div className="grid w-full gap-1.5 lg:grid-cols-[3fr_3fr_2fr_2fr_auto]">
<div className="grid w-full gap-3 lg:grid-cols-[3fr_3fr_2fr_2fr_auto]">
{/* Header */}
<>
<Cell>Colaborador</Cell>