218 lines
6.3 KiB
TypeScript
218 lines
6.3 KiB
TypeScript
import type { Route } from './+types/route'
|
|
|
|
import { useState } from 'react'
|
|
import { Link } from 'react-router'
|
|
import { useLocalStorageState, useMount } from 'ahooks'
|
|
import { BookSearchIcon, CircleCheckBigIcon, WalletIcon } from 'lucide-react'
|
|
|
|
import {
|
|
Card,
|
|
CardContent,
|
|
CardHeader,
|
|
CardDescription,
|
|
CardTitle
|
|
} from '@repo/ui/components/ui/card'
|
|
import {
|
|
Breadcrumb,
|
|
BreadcrumbItem,
|
|
BreadcrumbLink,
|
|
BreadcrumbList,
|
|
BreadcrumbPage,
|
|
BreadcrumbSeparator
|
|
} from '@repo/ui/components/ui/breadcrumb'
|
|
import { Switch } from '@repo/ui/components/ui/switch'
|
|
import { createSearch } from '@repo/util/meili'
|
|
import { cloudflareContext } from '@repo/auth/context'
|
|
import { Label } from '@repo/ui/components/ui/label'
|
|
import { Skeleton } from '@repo/ui/components/skeleton'
|
|
|
|
import { Wizard, WizardStep } from '@/components/wizard'
|
|
import { Step, StepItem, StepSeparator } from '@/components/step'
|
|
import type { Course, Enrollment } from '../_.$orgid.enrollments.add/data'
|
|
import { Assigned } from './assigned'
|
|
import { Bulk, type Item } from './bulk'
|
|
import { Payment, type CreditCard } from './payment'
|
|
import { Review } from './review'
|
|
import type { Coupon } from './discount'
|
|
|
|
export type WizardState = {
|
|
index: number
|
|
kind: 'bulk' | 'assigned'
|
|
items: Item[]
|
|
enrollments: Enrollment[]
|
|
coupon?: Coupon
|
|
payment_method?: 'PIX' | 'BANK_SLIP' | 'CREDIT_CARD'
|
|
credit_card?: CreditCard
|
|
}
|
|
|
|
const emptyWizard: WizardState = {
|
|
index: 0,
|
|
kind: 'bulk',
|
|
items: [],
|
|
enrollments: [],
|
|
coupon: undefined,
|
|
payment_method: undefined,
|
|
credit_card: undefined
|
|
}
|
|
|
|
export function meta({}: Route.MetaArgs) {
|
|
return [{ title: 'Comprar matrículas' }]
|
|
}
|
|
|
|
export async function loader({ context }: Route.LoaderArgs) {
|
|
const cloudflare = context.get(cloudflareContext)
|
|
const courses = createSearch<Course>({
|
|
index: 'saladeaula_courses',
|
|
sort: ['created_at:desc'],
|
|
filter: 'unlisted = false',
|
|
hitsPerPage: 100,
|
|
env: cloudflare.env
|
|
})
|
|
|
|
return { courses }
|
|
}
|
|
|
|
export async function action({ request }: Route.ActionArgs) {
|
|
const body = (await request.json()) as object
|
|
console.log(body)
|
|
}
|
|
|
|
export default function Route({
|
|
loaderData: { courses }
|
|
}: Route.ComponentProps) {
|
|
const [mounted, setMounted] = useState(false)
|
|
const [state, setState] = useLocalStorageState<WizardState>('wizard_cart', {
|
|
defaultValue: emptyWizard
|
|
})
|
|
const index = state.index
|
|
const kind = state.kind
|
|
const props = {
|
|
...state,
|
|
courses,
|
|
onSubmit: async (data: any) =>
|
|
setState((prev) => ({
|
|
...(prev ?? emptyWizard),
|
|
...data
|
|
}))
|
|
}
|
|
|
|
useMount(() => {
|
|
setMounted(true)
|
|
})
|
|
|
|
if (!mounted) {
|
|
return <Skeleton />
|
|
}
|
|
|
|
return (
|
|
<div className="space-y-2.5">
|
|
<Breadcrumb>
|
|
<BreadcrumbList>
|
|
<BreadcrumbItem>
|
|
<BreadcrumbLink asChild>
|
|
<Link to="../enrollments">Matrículas</Link>
|
|
</BreadcrumbLink>
|
|
</BreadcrumbItem>
|
|
<BreadcrumbSeparator />
|
|
<BreadcrumbItem>
|
|
<BreadcrumbPage>Comprar matrículas</BreadcrumbPage>
|
|
</BreadcrumbItem>
|
|
</BreadcrumbList>
|
|
</Breadcrumb>
|
|
|
|
<div className="lg:max-w-4xl mx-auto space-y-2.5">
|
|
<Card>
|
|
<CardHeader>
|
|
<CardTitle className="text-2xl">Comprar matrículas</CardTitle>
|
|
<CardDescription>
|
|
Siga os passos abaixo para comprar novas matrículas.
|
|
</CardDescription>
|
|
</CardHeader>
|
|
|
|
<CardContent className="space-y-4">
|
|
<Step className="mb-6" activeIndex={index}>
|
|
<StepItem index={0} icon={BookSearchIcon}>
|
|
Escolher cursos
|
|
</StepItem>
|
|
<StepSeparator />
|
|
<StepItem index={1} icon={WalletIcon}>
|
|
Pagamento
|
|
</StepItem>
|
|
<StepSeparator />
|
|
<StepItem index={2} icon={CircleCheckBigIcon}>
|
|
Revisão & confirmação
|
|
</StepItem>
|
|
</Step>
|
|
|
|
<Wizard
|
|
index={index}
|
|
onChange={(nextIndex) =>
|
|
setState((prev) => ({
|
|
...(prev ?? emptyWizard),
|
|
index: nextIndex
|
|
}))
|
|
}
|
|
>
|
|
{/* Cart */}
|
|
<WizardStep name="cart">
|
|
<Label
|
|
className="flex flex-row items-center justify-between cursor-pointer
|
|
bg-accent/50 hover:bg-accent rounded-lg border p-4
|
|
dark:has-aria-checked:border-blue-900
|
|
dark:has-aria-checked:bg-blue-950"
|
|
>
|
|
<div className="grid gap-1.5 font-normal">
|
|
<p className="text-sm leading-none font-medium">
|
|
Adicionar colaboradores
|
|
</p>
|
|
<p className="text-muted-foreground text-sm">
|
|
Você pode adicionar agora os colaboradores que irão fazer
|
|
o curso ou deixar isso para depois.
|
|
</p>
|
|
</div>
|
|
{/* Toggle button x*/}
|
|
<Switch
|
|
checked={kind === 'assigned'}
|
|
onCheckedChange={(checked) =>
|
|
setState((prev) => ({
|
|
...(prev ?? emptyWizard),
|
|
kind: checked ? 'assigned' : 'bulk'
|
|
}))
|
|
}
|
|
className="cursor-pointer"
|
|
/>
|
|
</Label>
|
|
|
|
{kind == 'assigned' ? (
|
|
<Assigned {...props} />
|
|
) : (
|
|
<Bulk {...props} />
|
|
)}
|
|
</WizardStep>
|
|
|
|
{/* Payment */}
|
|
<WizardStep name="payment">
|
|
<Payment
|
|
payment_method={state.payment_method}
|
|
credit_card={state.credit_card}
|
|
onSubmit={(data: any) => {
|
|
setState((prev) => ({
|
|
...(prev ?? emptyWizard),
|
|
...data
|
|
}))
|
|
}}
|
|
/>
|
|
</WizardStep>
|
|
|
|
{/* Review */}
|
|
<WizardStep name="review">
|
|
<Review state={state} />
|
|
</WizardStep>
|
|
</Wizard>
|
|
</CardContent>
|
|
</Card>
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|