add checkout
This commit is contained in:
@@ -1,41 +1,24 @@
|
||||
import type { Route } from './+types/route'
|
||||
|
||||
import Fuse from 'fuse.js'
|
||||
import { Fragment, use, useEffect, type ReactNode } from 'react'
|
||||
import { useRequest, useToggle } from 'ahooks'
|
||||
import { ErrorMessage } from '@hookform/error-message'
|
||||
import {
|
||||
CalendarIcon,
|
||||
CopyIcon,
|
||||
CopyPlusIcon,
|
||||
Trash2Icon,
|
||||
PlusIcon,
|
||||
XIcon,
|
||||
ChevronsUpDownIcon,
|
||||
CheckIcon,
|
||||
BookIcon,
|
||||
ArrowDownAZIcon,
|
||||
ArrowUpAZIcon,
|
||||
AlertTriangleIcon,
|
||||
UserIcon,
|
||||
EllipsisIcon
|
||||
} from 'lucide-react'
|
||||
import { redirect, Link, useParams, useFetcher } from 'react-router'
|
||||
import { Controller, useFieldArray, useForm } from 'react-hook-form'
|
||||
import { Fragment, use, useEffect, useMemo, useState } from 'react'
|
||||
import { format } from 'date-fns'
|
||||
import { ptBR } from 'react-day-picker/locale'
|
||||
import { zodResolver } from '@hookform/resolvers/zod'
|
||||
import { formatCPF } from '@brazilian-utils/brazilian-utils'
|
||||
import { pick } from 'ramda'
|
||||
|
||||
import { DateTime } from '@repo/ui/components/datetime'
|
||||
import { Avatar, AvatarFallback } from '@repo/ui/components/ui/avatar'
|
||||
import { Abbr } from '@repo/ui/components/abbr'
|
||||
import {
|
||||
Command,
|
||||
CommandEmpty,
|
||||
CommandGroup,
|
||||
CommandInput,
|
||||
CommandItem,
|
||||
CommandList
|
||||
} from '@repo/ui/components/ui/command'
|
||||
@@ -47,11 +30,6 @@ import {
|
||||
BreadcrumbPage,
|
||||
BreadcrumbSeparator
|
||||
} from '@repo/ui/components/ui/breadcrumb'
|
||||
import {
|
||||
InputGroup,
|
||||
InputGroupAddon,
|
||||
InputGroupInput
|
||||
} from '@repo/ui/components/ui/input-group'
|
||||
import {
|
||||
Card,
|
||||
CardAction,
|
||||
@@ -60,6 +38,7 @@ import {
|
||||
CardHeader,
|
||||
CardTitle
|
||||
} from '@repo/ui/components/ui/card'
|
||||
import { DateTime } from '@repo/ui/components/datetime'
|
||||
import { Spinner } from '@repo/ui/components/ui/spinner'
|
||||
import { Input } from '@repo/ui/components/ui/input'
|
||||
import { Button } from '@repo/ui/components/ui/button'
|
||||
@@ -70,25 +49,33 @@ import {
|
||||
PopoverTrigger
|
||||
} from '@repo/ui/components/ui/popover'
|
||||
import { Label } from '@repo/ui/components/ui/label'
|
||||
import { Calendar } from '@repo/ui/components/ui/calendar'
|
||||
import { createSearch } from '@repo/util/meili'
|
||||
import { initials, cn } from '@repo/ui/lib/utils'
|
||||
import { HttpMethod, request as req } from '@repo/util/request'
|
||||
import { useIsMobile } from '@repo/ui/hooks/use-mobile'
|
||||
import { cloudflareContext } from '@repo/auth/context'
|
||||
import { SearchFilter } from '@repo/ui/components/search-filter'
|
||||
|
||||
import { formSchema, type Schema, MAX_ITEMS } from './data'
|
||||
import {
|
||||
MAX_ITEMS,
|
||||
formSchema,
|
||||
type Schema,
|
||||
type Course,
|
||||
type User,
|
||||
type Enrolled
|
||||
} from './data'
|
||||
import { ScheduledForInput } from './scheduled-for'
|
||||
import { CoursePicker } from './course-picker'
|
||||
import { UserPicker } from './user-picker'
|
||||
|
||||
const emptyRow = {
|
||||
user: undefined,
|
||||
course: undefined,
|
||||
scheduled_for: undefined
|
||||
}
|
||||
|
||||
export function meta({}: Route.MetaArgs) {
|
||||
return [{ title: 'Adicionar matrícula' }]
|
||||
}
|
||||
|
||||
type Enrolled = {
|
||||
status: 'fail' | 'success'
|
||||
input_record: { user: any; course: any }
|
||||
}
|
||||
|
||||
export async function loader({ params, context, request }: Route.LoaderArgs) {
|
||||
const url = new URL(request.url)
|
||||
const submissionId = url.searchParams.get('submission')
|
||||
@@ -129,12 +116,6 @@ export async function action({ params, request, context }: Route.ActionArgs) {
|
||||
return redirect(`/${params.orgid}/enrollments/${data.sk}/submitted`)
|
||||
}
|
||||
|
||||
const emptyRow = {
|
||||
user: undefined,
|
||||
course: undefined,
|
||||
scheduled_for: undefined
|
||||
}
|
||||
|
||||
export default function Route({
|
||||
loaderData: { courses, submission }
|
||||
}: Route.ComponentProps) {
|
||||
@@ -161,7 +142,7 @@ export default function Route({
|
||||
const onSearch = async (search: string) => {
|
||||
const params = new URLSearchParams({ q: search })
|
||||
const r = await fetch(`/${orgid}/users.json?${params.toString()}`)
|
||||
const { hits } = (await r.json()) as { hits: any[] }
|
||||
const { hits } = (await r.json()) as { hits: User[] }
|
||||
return hits
|
||||
}
|
||||
|
||||
@@ -228,16 +209,10 @@ export default function Route({
|
||||
<div className="grid lg:grid-cols-[repeat(3,1fr)_auto] w-full gap-1.5">
|
||||
{/* Header */}
|
||||
<>
|
||||
<div className="max-lg:hidden text-foreground font-medium text-sm">
|
||||
Colaborador
|
||||
</div>
|
||||
<div className="max-lg:hidden text-foreground font-medium text-sm">
|
||||
Curso
|
||||
</div>
|
||||
<div className="max-lg:hidden text-foreground font-medium text-sm">
|
||||
Matriculado em
|
||||
</div>
|
||||
<div>{/**/}</div>
|
||||
<Cell>Colaborador</Cell>
|
||||
<Cell>Curso</Cell>
|
||||
<Cell>Matricular em</Cell>
|
||||
<Cell>{/**/}</Cell>
|
||||
</>
|
||||
|
||||
{/* Rows */}
|
||||
@@ -255,109 +230,13 @@ export default function Route({
|
||||
fieldState
|
||||
}) => (
|
||||
<div className="grid gap-1">
|
||||
<SearchFilter
|
||||
align="start"
|
||||
<UserPicker
|
||||
value={value}
|
||||
onChange={onChange}
|
||||
onSearch={onSearch}
|
||||
render={({
|
||||
id,
|
||||
name,
|
||||
email,
|
||||
cpf,
|
||||
onSelect,
|
||||
onClose
|
||||
}) => (
|
||||
<CommandItem
|
||||
key={id}
|
||||
value={id}
|
||||
className="cursor-pointer"
|
||||
disabled={!cpf}
|
||||
onSelect={() => {
|
||||
onSelect()
|
||||
onClose()
|
||||
}}
|
||||
>
|
||||
<div className="flex gap-2.5 items-start">
|
||||
<Avatar className="size-10 hidden lg:block">
|
||||
<AvatarFallback className="border">
|
||||
{initials(name)}
|
||||
</AvatarFallback>
|
||||
</Avatar>
|
||||
fieldState={fieldState}
|
||||
/>
|
||||
|
||||
<ul>
|
||||
<li className="font-bold">
|
||||
<Abbr>{name}</Abbr>
|
||||
</li>
|
||||
<li className="text-muted-foreground text-sm">
|
||||
<Abbr>{email}</Abbr>
|
||||
</li>
|
||||
|
||||
{cpf ? (
|
||||
<li className="text-muted-foreground text-sm">
|
||||
{formatCPF(cpf)}
|
||||
</li>
|
||||
) : (
|
||||
<li className="flex gap-1 items-center text-red-400">
|
||||
<AlertTriangleIcon className="text-red-400" />
|
||||
Inelegível
|
||||
</li>
|
||||
)}
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<CheckIcon
|
||||
className={cn(
|
||||
'ml-auto',
|
||||
value?.id === id
|
||||
? 'opacity-100'
|
||||
: 'opacity-0'
|
||||
)}
|
||||
/>
|
||||
</CommandItem>
|
||||
)}
|
||||
>
|
||||
{({ loading }) => (
|
||||
<InputGroup>
|
||||
<InputGroupInput
|
||||
readOnly
|
||||
value={value?.name || ''}
|
||||
autoFocus={true}
|
||||
placeholder="Colaborador"
|
||||
className="cursor-pointer"
|
||||
autoComplete="off"
|
||||
aria-invalid={!!fieldState.error}
|
||||
/>
|
||||
<InputGroupAddon>
|
||||
<UserIcon />
|
||||
</InputGroupAddon>
|
||||
|
||||
{value && (
|
||||
<InputGroupAddon
|
||||
align="inline-end"
|
||||
className="mr-0"
|
||||
>
|
||||
<Button
|
||||
variant="link"
|
||||
size="icon-sm"
|
||||
className="cursor-pointer text-muted-foreground"
|
||||
onClick={(e) => {
|
||||
e.preventDefault()
|
||||
onChange?.(null)
|
||||
}}
|
||||
>
|
||||
<XIcon />
|
||||
</Button>
|
||||
</InputGroupAddon>
|
||||
)}
|
||||
|
||||
{loading && (
|
||||
<InputGroupAddon align="inline-end">
|
||||
<Spinner />
|
||||
</InputGroupAddon>
|
||||
)}
|
||||
</InputGroup>
|
||||
)}
|
||||
</SearchFilter>
|
||||
<ErrorMessage
|
||||
errors={formState.errors}
|
||||
name={name}
|
||||
@@ -379,12 +258,13 @@ export default function Route({
|
||||
fieldState
|
||||
}) => (
|
||||
<div className="grid gap-1">
|
||||
<FacetedFilter
|
||||
<CoursePicker
|
||||
value={value}
|
||||
onChange={onChange}
|
||||
options={courses}
|
||||
error={fieldState.error}
|
||||
/>
|
||||
|
||||
<ErrorMessage
|
||||
errors={formState.errors}
|
||||
name={name}
|
||||
@@ -468,202 +348,6 @@ export default function Route({
|
||||
)
|
||||
}
|
||||
|
||||
type Course = {
|
||||
id: string
|
||||
name: string
|
||||
access_period: number
|
||||
metadata__unit_price?: number
|
||||
}
|
||||
|
||||
interface FacetedFilterProps {
|
||||
value?: Course
|
||||
options: Promise<{ hits: any[] }>
|
||||
onChange?: (value: any) => void
|
||||
error?: any
|
||||
}
|
||||
|
||||
function FacetedFilter({
|
||||
value,
|
||||
onChange,
|
||||
options,
|
||||
error
|
||||
}: FacetedFilterProps) {
|
||||
const [search, setSearch] = useState<string>('')
|
||||
const [open, { set }] = useToggle()
|
||||
const [sort, { toggle }] = useToggle('a-z', 'z-a')
|
||||
const { hits } = use(options)
|
||||
const fuse = useMemo(() => {
|
||||
return new Fuse(hits, {
|
||||
keys: ['name'],
|
||||
threshold: 0.3,
|
||||
includeMatches: true
|
||||
})
|
||||
}, [hits])
|
||||
|
||||
const filtered = useMemo(() => {
|
||||
const results = !search ? hits : fuse.search(search).map(({ item }) => item)
|
||||
|
||||
return results.sort((a, b) => {
|
||||
const comparison = a.name.localeCompare(b.name)
|
||||
return sort === 'a-z' ? comparison : -comparison
|
||||
})
|
||||
}, [search, fuse, hits, sort])
|
||||
|
||||
return (
|
||||
<Popover open={open} onOpenChange={set}>
|
||||
<PopoverTrigger asChild>
|
||||
<InputGroup>
|
||||
<InputGroupInput
|
||||
readOnly
|
||||
placeholder="Curso"
|
||||
value={value?.name || ''}
|
||||
aria-invalid={!!error}
|
||||
/>
|
||||
<InputGroupAddon>
|
||||
<BookIcon />
|
||||
</InputGroupAddon>
|
||||
<InputGroupAddon align="inline-end">
|
||||
<ChevronsUpDownIcon />
|
||||
</InputGroupAddon>
|
||||
</InputGroup>
|
||||
</PopoverTrigger>
|
||||
|
||||
<PopoverContent className="lg:w-84 p-0" align="start">
|
||||
<Command shouldFilter={false}>
|
||||
<div className="flex">
|
||||
<div className="flex-1">
|
||||
<CommandInput
|
||||
placeholder="Curso"
|
||||
autoComplete="off"
|
||||
onValueChange={setSearch}
|
||||
/>
|
||||
</div>
|
||||
<div className="border-b flex items-center justify-end">
|
||||
<Button
|
||||
variant="link"
|
||||
size="icon-sm"
|
||||
tabIndex={-1}
|
||||
className="cursor-pointer text-muted-foreground"
|
||||
onClick={toggle}
|
||||
>
|
||||
{sort == 'a-z' ? <ArrowDownAZIcon /> : <ArrowUpAZIcon />}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
{/* Force rerender to reset the scroll position */}
|
||||
<CommandList key={sort}>
|
||||
<CommandEmpty>Nenhum resultado encontrado.</CommandEmpty>
|
||||
<CommandGroup>
|
||||
{filtered
|
||||
.filter(
|
||||
({ metadata__unit_price = 0 }) => metadata__unit_price > 0
|
||||
)
|
||||
.map(
|
||||
({
|
||||
id,
|
||||
name,
|
||||
access_period,
|
||||
metadata__unit_price: unit_price
|
||||
}) => (
|
||||
<CommandItem
|
||||
key={id}
|
||||
value={id}
|
||||
className="cursor-pointer"
|
||||
onSelect={() => {
|
||||
onChange?.({
|
||||
id,
|
||||
name,
|
||||
access_period: Number(access_period),
|
||||
unit_price: Number(unit_price)
|
||||
})
|
||||
set(false)
|
||||
}}
|
||||
>
|
||||
{name}
|
||||
<CheckIcon
|
||||
className={cn(
|
||||
'ml-auto',
|
||||
value?.id === id ? 'opacity-100' : 'opacity-0'
|
||||
)}
|
||||
/>
|
||||
</CommandItem>
|
||||
)
|
||||
)}
|
||||
</CommandGroup>
|
||||
</CommandList>
|
||||
</Command>
|
||||
</PopoverContent>
|
||||
</Popover>
|
||||
)
|
||||
}
|
||||
|
||||
interface ScheduledForInputProps {
|
||||
value?: Date
|
||||
onChange?: (value: Date | undefined) => void
|
||||
}
|
||||
|
||||
function ScheduledForInput({ value, onChange }: ScheduledForInputProps) {
|
||||
const today = new Date()
|
||||
const tomorrow = new Date()
|
||||
tomorrow.setDate(today.getDate() + 1)
|
||||
const [open, { set }] = useToggle()
|
||||
const [selected, setDate] = useState<Date | undefined>(value)
|
||||
const display = selected ? format(selected, 'dd/MM/yyyy') : ''
|
||||
|
||||
return (
|
||||
<Popover open={open} onOpenChange={set}>
|
||||
<PopoverTrigger asChild>
|
||||
<InputGroup>
|
||||
<InputGroupInput
|
||||
readOnly
|
||||
placeholder="Imediatamente"
|
||||
value={display}
|
||||
/>
|
||||
|
||||
<InputGroupAddon>
|
||||
<CalendarIcon />
|
||||
</InputGroupAddon>
|
||||
|
||||
{selected && (
|
||||
<InputGroupAddon align="inline-end" className="mr-0">
|
||||
<Button
|
||||
variant="link"
|
||||
size="icon-sm"
|
||||
className="cursor-pointer text-muted-foreground"
|
||||
onClick={(e) => {
|
||||
e.preventDefault()
|
||||
setDate(undefined)
|
||||
onChange?.(undefined)
|
||||
set(false)
|
||||
}}
|
||||
>
|
||||
<XIcon />
|
||||
</Button>
|
||||
</InputGroupAddon>
|
||||
)}
|
||||
</InputGroup>
|
||||
</PopoverTrigger>
|
||||
|
||||
<PopoverContent className="w-full p-0" align="start">
|
||||
<Calendar
|
||||
mode="single"
|
||||
selected={selected}
|
||||
onSelect={(date: Date | undefined) => {
|
||||
setDate(date)
|
||||
onChange?.(date)
|
||||
set(false)
|
||||
}}
|
||||
disabled={{ before: tomorrow }}
|
||||
startMonth={new Date(today.getFullYear(), 0)}
|
||||
endMonth={new Date(today.getFullYear() + 3, 11)}
|
||||
captionLayout="dropdown"
|
||||
locale={ptBR}
|
||||
/>
|
||||
</PopoverContent>
|
||||
</Popover>
|
||||
)
|
||||
}
|
||||
|
||||
function DuplicateRowMultipleTimes({
|
||||
index,
|
||||
duplicateRow
|
||||
@@ -747,7 +431,7 @@ function ActionMenu() {
|
||||
const r = await fetch(`/~/api/orgs/${orgid}/enrollments/submissions`, {
|
||||
method: 'GET'
|
||||
})
|
||||
return await r.json()
|
||||
return (await r.json()) as { items: { sk: string }[] }
|
||||
},
|
||||
{ manual: true }
|
||||
)
|
||||
@@ -810,3 +494,11 @@ function ActionMenu() {
|
||||
</Popover>
|
||||
)
|
||||
}
|
||||
|
||||
export function Cell({ children }: { children?: ReactNode }) {
|
||||
return (
|
||||
<div className="max-lg:hidden text-foreground font-medium text-sm">
|
||||
{children}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user