add persistence to cart
This commit is contained in:
@@ -58,15 +58,23 @@ type Schema = z.infer<typeof formSchemaAssigned>
|
|||||||
type AssignedProps = {
|
type AssignedProps = {
|
||||||
onSubmit: (value: any) => void | Promise<void>
|
onSubmit: (value: any) => void | Promise<void>
|
||||||
courses: Promise<{ hits: Course[] }>
|
courses: Promise<{ hits: Course[] }>
|
||||||
|
defaultValues?: { enrollments: object[] }
|
||||||
}
|
}
|
||||||
|
|
||||||
export function Assigned({ courses, onSubmit }: AssignedProps) {
|
export function Assigned({ courses, onSubmit, defaultValues }: AssignedProps) {
|
||||||
const wizard = useWizard()
|
const wizard = useWizard()
|
||||||
const { orgid } = useParams()
|
const { orgid } = useParams()
|
||||||
const form = useForm({
|
const form = useForm({
|
||||||
resolver: zodResolver(formSchemaAssigned),
|
resolver: zodResolver(formSchemaAssigned),
|
||||||
defaultValues: { enrollments: [emptyRow] }
|
defaultValues: {
|
||||||
|
enrollments: defaultValues?.enrollments?.map((e: any) => ({
|
||||||
|
...e,
|
||||||
|
scheduled_for: e.scheduled_for ? new Date(e.scheduled_for) : undefined
|
||||||
|
})) || [emptyRow]
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
console.log(defaultValues)
|
||||||
const { formState, control, handleSubmit, setValue } = form
|
const { formState, control, handleSubmit, setValue } = form
|
||||||
const { fields, remove, append } = useFieldArray({
|
const { fields, remove, append } = useFieldArray({
|
||||||
control,
|
control,
|
||||||
|
|||||||
@@ -38,6 +38,7 @@ const emptyRow = {
|
|||||||
type BulkProps = {
|
type BulkProps = {
|
||||||
onSubmit: (value: any) => void | Promise<void>
|
onSubmit: (value: any) => void | Promise<void>
|
||||||
courses: Promise<{ hits: Course[] }>
|
courses: Promise<{ hits: Course[] }>
|
||||||
|
defaultValues?: { items: object[] }
|
||||||
}
|
}
|
||||||
|
|
||||||
const item = z.object({
|
const item = z.object({
|
||||||
@@ -68,11 +69,11 @@ const formSchema = z.object({
|
|||||||
|
|
||||||
type Schema = z.infer<typeof formSchema>
|
type Schema = z.infer<typeof formSchema>
|
||||||
|
|
||||||
export function Bulk({ courses, onSubmit }: BulkProps) {
|
export function Bulk({ courses, onSubmit, defaultValues }: BulkProps) {
|
||||||
const wizard = useWizard()
|
const wizard = useWizard()
|
||||||
const form = useForm({
|
const form = useForm({
|
||||||
resolver: zodResolver(formSchema),
|
resolver: zodResolver(formSchema),
|
||||||
defaultValues: { items: [emptyRow] }
|
defaultValues: { items: defaultValues?.items || [emptyRow] }
|
||||||
})
|
})
|
||||||
const {
|
const {
|
||||||
formState,
|
formState,
|
||||||
|
|||||||
@@ -1,42 +1,99 @@
|
|||||||
|
import { useForm, Controller } from 'react-hook-form'
|
||||||
|
import { zodResolver } from '@hookform/resolvers/zod'
|
||||||
|
import { ErrorMessage } from '@hookform/error-message'
|
||||||
|
import z from 'zod'
|
||||||
|
|
||||||
import { Button } from '@repo/ui/components/ui/button'
|
import { Button } from '@repo/ui/components/ui/button'
|
||||||
import { Label } from '@repo/ui/components/ui/label'
|
import { Label } from '@repo/ui/components/ui/label'
|
||||||
import { RadioGroup, RadioGroupItem } from '@repo/ui/components/ui/radio-group'
|
import { RadioGroup, RadioGroupItem } from '@repo/ui/components/ui/radio-group'
|
||||||
import { Separator } from '@repo/ui/components/ui/separator'
|
import { Separator } from '@repo/ui/components/ui/separator'
|
||||||
|
|
||||||
export function Payment() {
|
import { useWizard } from '@/components/wizard'
|
||||||
|
|
||||||
|
const formSchema = z.object({
|
||||||
|
payment_method: z.enum(['PIX', 'BANK_SLIP', 'CREDIT_CARD'], {
|
||||||
|
error: 'Escolha uma forma de pagamento'
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
type Schema = z.infer<typeof formSchema>
|
||||||
|
|
||||||
|
type PaymentProps = {
|
||||||
|
onSubmit: (value: any) => void | Promise<void>
|
||||||
|
defaultValues?: object
|
||||||
|
}
|
||||||
|
|
||||||
|
export function Payment({ onSubmit, defaultValues }: PaymentProps) {
|
||||||
|
const wizard = useWizard()
|
||||||
|
const { control, handleSubmit } = useForm<Schema>({
|
||||||
|
defaultValues: {
|
||||||
|
payment_method: '' as any,
|
||||||
|
...defaultValues
|
||||||
|
},
|
||||||
|
resolver: zodResolver(formSchema)
|
||||||
|
})
|
||||||
|
|
||||||
|
const onSubmit_ = async (data: Schema) => {
|
||||||
|
await onSubmit(data)
|
||||||
|
wizard('review')
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<form onSubmit={handleSubmit(onSubmit_)} className="space-y-6">
|
||||||
|
<Controller
|
||||||
|
name="payment_method"
|
||||||
|
control={control}
|
||||||
|
render={({ field: { name, value, onChange }, formState }) => (
|
||||||
|
<div className="space-y-1.5">
|
||||||
<RadioGroup
|
<RadioGroup
|
||||||
defaultValue="comfortable"
|
value={value}
|
||||||
className="lg:flex *:p-5 *:border *:rounded-xl *:flex-1 *:cursor-pointer
|
onValueChange={onChange}
|
||||||
*:bg-accent/25 *:has-[button[data-state=checked]]:bg-accent"
|
className="lg:flex gap-3
|
||||||
|
*:p-5 *:border *:rounded-xl *:flex-1
|
||||||
|
*:cursor-pointer *:bg-accent/25
|
||||||
|
*:has-[button[data-state=checked]]:bg-accent
|
||||||
|
"
|
||||||
>
|
>
|
||||||
<Label>
|
<Label>
|
||||||
<RadioGroupItem value="default" id="r1" />
|
<RadioGroupItem value="PIX" />
|
||||||
<span>Pix</span>
|
<span>Pix</span>
|
||||||
</Label>
|
</Label>
|
||||||
|
|
||||||
<Label>
|
<Label>
|
||||||
<RadioGroupItem value="comfortable" id="r2" />
|
<RadioGroupItem value="BANK_SLIP" />
|
||||||
<span>Boleto bancário</span>
|
<span>Boleto bancário</span>
|
||||||
</Label>
|
</Label>
|
||||||
|
|
||||||
<Label>
|
<Label>
|
||||||
<RadioGroupItem value="compact" id="r3" />
|
<RadioGroupItem value="CREDIT_CARD" />
|
||||||
<span>Cartão de crédito</span>
|
<span>Cartão de crédito</span>
|
||||||
</Label>
|
</Label>
|
||||||
</RadioGroup>
|
</RadioGroup>
|
||||||
|
|
||||||
|
<ErrorMessage
|
||||||
|
errors={formState.errors}
|
||||||
|
name={name}
|
||||||
|
render={({ message }) => (
|
||||||
|
<p className="text-destructive text-sm">{message}</p>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
|
||||||
<Separator />
|
<Separator />
|
||||||
|
|
||||||
<div className="flex justify-end">
|
<div className="flex justify-end gap-4 *:cursor-pointer">
|
||||||
<Button
|
<Button
|
||||||
type="submit"
|
type="button"
|
||||||
className="cursor-pointer"
|
variant="link"
|
||||||
// disabled={formState.isSubmitting}
|
className="text-black dark:text-white"
|
||||||
|
onClick={() => wizard('cart')}
|
||||||
>
|
>
|
||||||
{/*{formState.isSubmitting && <Spinner />}*/}
|
Voltar
|
||||||
Continuar
|
|
||||||
</Button>
|
</Button>
|
||||||
|
<Button type="submit">Continuar</Button>
|
||||||
</div>
|
</div>
|
||||||
</>
|
</form>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,87 @@
|
|||||||
|
import { useWizard } from '@/components/wizard'
|
||||||
|
import { Currency } from '@repo/ui/components/currency'
|
||||||
|
import { Button } from '@repo/ui/components/ui/button'
|
||||||
|
import { Separator } from '@repo/ui/components/ui/separator'
|
||||||
|
import {
|
||||||
|
Table,
|
||||||
|
TableBody,
|
||||||
|
TableCell,
|
||||||
|
TableFooter,
|
||||||
|
TableHead,
|
||||||
|
TableHeader,
|
||||||
|
TableRow
|
||||||
|
} from '@repo/ui/components/ui/table'
|
||||||
|
|
||||||
|
type ReviewProps = {
|
||||||
|
state: any
|
||||||
|
}
|
||||||
|
|
||||||
|
export function Review({ state }: ReviewProps) {
|
||||||
|
const wizard = useWizard()
|
||||||
|
// console.log(state)
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<Table>
|
||||||
|
<TableHeader>
|
||||||
|
<TableRow className="pointer-events-none">
|
||||||
|
<TableHead>Curso</TableHead>
|
||||||
|
<TableHead>Quantidade</TableHead>
|
||||||
|
<TableHead>Valor unit.</TableHead>
|
||||||
|
<TableHead>Total</TableHead>
|
||||||
|
</TableRow>
|
||||||
|
</TableHeader>
|
||||||
|
<TableBody>
|
||||||
|
{state?.cart?.items?.map(({ course, quantity }, index) => {
|
||||||
|
return (
|
||||||
|
<TableRow key={index}>
|
||||||
|
<TableCell>{course.name}</TableCell>
|
||||||
|
<TableCell>{quantity}</TableCell>
|
||||||
|
<TableCell>
|
||||||
|
<Currency>{course.unit_price}</Currency>
|
||||||
|
</TableCell>
|
||||||
|
<TableCell>
|
||||||
|
<Currency>{course.unit_price * quantity}</Currency>
|
||||||
|
</TableCell>
|
||||||
|
</TableRow>
|
||||||
|
)
|
||||||
|
})}
|
||||||
|
</TableBody>
|
||||||
|
<TableFooter>
|
||||||
|
<TableRow className="pointer-events-none">
|
||||||
|
<TableCell className="text-right" colSpan={3}>
|
||||||
|
Subtotal
|
||||||
|
</TableCell>
|
||||||
|
<TableCell>..</TableCell>
|
||||||
|
</TableRow>
|
||||||
|
<TableRow className="pointer-events-none">
|
||||||
|
<TableCell className="text-right" colSpan={3}>
|
||||||
|
Descontos
|
||||||
|
</TableCell>
|
||||||
|
<TableCell>..</TableCell>
|
||||||
|
</TableRow>
|
||||||
|
<TableRow className="pointer-events-none">
|
||||||
|
<TableCell className="text-right" colSpan={3}>
|
||||||
|
Total
|
||||||
|
</TableCell>
|
||||||
|
<TableCell>..</TableCell>
|
||||||
|
</TableRow>
|
||||||
|
</TableFooter>
|
||||||
|
</Table>
|
||||||
|
|
||||||
|
<Separator />
|
||||||
|
|
||||||
|
<div className="flex justify-end gap-4 *:cursor-pointer">
|
||||||
|
<Button
|
||||||
|
type="button"
|
||||||
|
variant="link"
|
||||||
|
className="text-black dark:text-white"
|
||||||
|
onClick={() => wizard('payment')}
|
||||||
|
>
|
||||||
|
Voltar
|
||||||
|
</Button>
|
||||||
|
<Button type="submit">Confirmar</Button>
|
||||||
|
</div>
|
||||||
|
</>
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -1,7 +1,8 @@
|
|||||||
import type { Route } from './+types/route'
|
import type { Route } from './+types/route'
|
||||||
|
|
||||||
|
import { useState } from 'react'
|
||||||
import { Link } from 'react-router'
|
import { Link } from 'react-router'
|
||||||
import { useToggle } from 'ahooks'
|
import { useLocalStorageState, useMount, useToggle } from 'ahooks'
|
||||||
import { BookSearchIcon, CircleCheckBigIcon, WalletIcon } from 'lucide-react'
|
import { BookSearchIcon, CircleCheckBigIcon, WalletIcon } from 'lucide-react'
|
||||||
|
|
||||||
import {
|
import {
|
||||||
@@ -30,13 +31,28 @@ import type { Course } from '../_.$orgid.enrollments.add/data'
|
|||||||
import { Assigned } from './assigned'
|
import { Assigned } from './assigned'
|
||||||
import { Bulk } from './bulk'
|
import { Bulk } from './bulk'
|
||||||
import { Payment } from './payment'
|
import { Payment } from './payment'
|
||||||
import { useState } from 'react'
|
import { Review } from './review'
|
||||||
|
import { Skeleton } from '@repo/ui/components/skeleton'
|
||||||
|
|
||||||
|
type WizardState = {
|
||||||
|
index: number
|
||||||
|
kind: 'bulk' | 'assigned'
|
||||||
|
cart: any
|
||||||
|
payment: any
|
||||||
|
}
|
||||||
|
|
||||||
|
const emptyWizard: WizardState = {
|
||||||
|
index: 0,
|
||||||
|
kind: 'bulk',
|
||||||
|
cart: {},
|
||||||
|
payment: {}
|
||||||
|
}
|
||||||
|
|
||||||
export function meta({}: Route.MetaArgs) {
|
export function meta({}: Route.MetaArgs) {
|
||||||
return [{ title: 'Comprar matrículas' }]
|
return [{ title: 'Comprar matrículas' }]
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function loader({ params, context, request }: Route.LoaderArgs) {
|
export async function loader({ context }: Route.LoaderArgs) {
|
||||||
const cloudflare = context.get(cloudflareContext)
|
const cloudflare = context.get(cloudflareContext)
|
||||||
const courses = createSearch<Course>({
|
const courses = createSearch<Course>({
|
||||||
index: 'saladeaula_courses',
|
index: 'saladeaula_courses',
|
||||||
@@ -57,15 +73,29 @@ export async function action({ request }: Route.ActionArgs) {
|
|||||||
export default function Route({
|
export default function Route({
|
||||||
loaderData: { courses }
|
loaderData: { courses }
|
||||||
}: Route.ComponentProps) {
|
}: Route.ComponentProps) {
|
||||||
const [index, setIndex] = useState(0)
|
const [mounted, setMounted] = useState(false)
|
||||||
const [state, { toggle }] = useToggle('bulk', 'assigned')
|
const [state, setState] = useLocalStorageState<WizardState>('wizard_cart', {
|
||||||
|
defaultValue: emptyWizard
|
||||||
const onSubmit = async (data: any) => {
|
})
|
||||||
// await new Promise((r) => setTimeout(r, 2000))
|
const index = state.index
|
||||||
console.log(data)
|
const kind = state.kind
|
||||||
|
const props = {
|
||||||
|
courses,
|
||||||
|
defaultValues: state.cart,
|
||||||
|
onSubmit: async (data: any) =>
|
||||||
|
setState((prev) => ({
|
||||||
|
...(prev ?? emptyWizard),
|
||||||
|
cart: data
|
||||||
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
const props = { courses, onSubmit }
|
useMount(() => {
|
||||||
|
setMounted(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
if (!mounted) {
|
||||||
|
return <Skeleton />
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="space-y-2.5">
|
<div className="space-y-2.5">
|
||||||
@@ -107,8 +137,17 @@ export default function Route({
|
|||||||
</StepItem>
|
</StepItem>
|
||||||
</Step>
|
</Step>
|
||||||
|
|
||||||
<Wizard onChange={setIndex}>
|
<Wizard
|
||||||
<WizardStep name="courses">
|
index={index}
|
||||||
|
onChange={(nextIndex) =>
|
||||||
|
setState((prev) => ({
|
||||||
|
...(prev ?? emptyWizard),
|
||||||
|
index: nextIndex
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
>
|
||||||
|
{/* Cart */}
|
||||||
|
<WizardStep name="cart">
|
||||||
<Label
|
<Label
|
||||||
className="flex flex-row items-center justify-between cursor-pointer
|
className="flex flex-row items-center justify-between cursor-pointer
|
||||||
bg-accent/50 hover:bg-accent rounded-lg border p-4
|
bg-accent/50 hover:bg-accent rounded-lg border p-4
|
||||||
@@ -125,21 +164,40 @@ export default function Route({
|
|||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
<Switch
|
<Switch
|
||||||
checked={state === 'assigned'}
|
checked={kind === 'assigned'}
|
||||||
onCheckedChange={toggle}
|
onCheckedChange={() =>
|
||||||
|
setState((prev) => ({
|
||||||
|
...(prev ?? emptyWizard),
|
||||||
|
kind: kind === 'bulk' ? 'assigned' : 'bulk'
|
||||||
|
}))
|
||||||
|
}
|
||||||
className="cursor-pointer"
|
className="cursor-pointer"
|
||||||
/>
|
/>
|
||||||
</Label>
|
</Label>
|
||||||
|
|
||||||
{state == 'assigned' ? (
|
{kind == 'assigned' ? (
|
||||||
<Assigned {...props} />
|
<Assigned {...props} />
|
||||||
) : (
|
) : (
|
||||||
<Bulk {...props} />
|
<Bulk {...props} />
|
||||||
)}
|
)}
|
||||||
</WizardStep>
|
</WizardStep>
|
||||||
|
|
||||||
|
{/* Payment */}
|
||||||
<WizardStep name="payment">
|
<WizardStep name="payment">
|
||||||
<Payment />
|
<Payment
|
||||||
|
defaultValues={state.payment}
|
||||||
|
onSubmit={(data: any) => {
|
||||||
|
setState((prev) => ({
|
||||||
|
...(prev ?? emptyWizard),
|
||||||
|
payment: data
|
||||||
|
}))
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</WizardStep>
|
||||||
|
|
||||||
|
{/* Review */}
|
||||||
|
<WizardStep name="review">
|
||||||
|
<Review state={state} />
|
||||||
</WizardStep>
|
</WizardStep>
|
||||||
</Wizard>
|
</Wizard>
|
||||||
</CardContent>
|
</CardContent>
|
||||||
|
|||||||
Reference in New Issue
Block a user