add download cert
This commit is contained in:
@@ -1,16 +1,46 @@
|
||||
'use client'
|
||||
|
||||
import type { CellContext, ColumnDef } from '@tanstack/react-table'
|
||||
import { HelpCircleIcon } from 'lucide-react'
|
||||
import { useToggle } from 'ahooks'
|
||||
import {
|
||||
CircleXIcon,
|
||||
EllipsisVerticalIcon,
|
||||
FileBadgeIcon,
|
||||
HelpCircleIcon,
|
||||
LockOpenIcon
|
||||
} from 'lucide-react'
|
||||
import { NavLink, useParams } from 'react-router'
|
||||
import { toast } from 'sonner'
|
||||
|
||||
import {
|
||||
AlertDialog,
|
||||
AlertDialogAction,
|
||||
AlertDialogCancel,
|
||||
AlertDialogContent,
|
||||
AlertDialogDescription,
|
||||
AlertDialogFooter,
|
||||
AlertDialogHeader,
|
||||
AlertDialogTitle,
|
||||
AlertDialogTrigger
|
||||
} from '@repo/ui/components/ui/alert-dialog'
|
||||
import { Avatar, AvatarFallback } from '@repo/ui/components/ui/avatar'
|
||||
import { Badge } from '@repo/ui/components/ui/badge'
|
||||
import { Button } from '@repo/ui/components/ui/button'
|
||||
import { Checkbox } from '@repo/ui/components/ui/checkbox'
|
||||
import {
|
||||
DropdownMenu,
|
||||
DropdownMenuContent,
|
||||
DropdownMenuItem,
|
||||
DropdownMenuSeparator,
|
||||
DropdownMenuTrigger
|
||||
} from '@repo/ui/components/ui/dropdown-menu'
|
||||
import { Progress } from '@repo/ui/components/ui/progress'
|
||||
import { Spinner } from '@repo/ui/components/ui/spinner'
|
||||
import { cn, initials } from '@repo/ui/lib/utils'
|
||||
|
||||
import { Abbr } from '@/components/abbr'
|
||||
import { DataTableColumnHeader } from '@/components/data-table/column-header'
|
||||
import { useDataTable } from '@/components/data-table/data-table'
|
||||
import { labels, statuses } from './data'
|
||||
|
||||
// This type is used to define the shape of our data.
|
||||
@@ -167,6 +197,10 @@ export const columns: ColumnDef<Enrollment>[] = [
|
||||
enableSorting: true,
|
||||
enableHiding: true,
|
||||
cell: cellDate
|
||||
},
|
||||
{
|
||||
id: 'actions',
|
||||
cell: ({ row }) => <ActionMenu row={row} />
|
||||
}
|
||||
]
|
||||
|
||||
@@ -183,3 +217,159 @@ function cellDate<TData>({
|
||||
|
||||
return <></>
|
||||
}
|
||||
|
||||
function ActionMenu({ row }: { row: any }) {
|
||||
const cert = row.original?.cert
|
||||
const progress = row.getValue('progress')
|
||||
|
||||
return (
|
||||
<div className="flex justify-end items-center">
|
||||
<DropdownMenu>
|
||||
<DropdownMenuTrigger asChild>
|
||||
<Button
|
||||
variant="ghost"
|
||||
className="data-[state=open]:bg-muted text-muted-foreground cursor-pointer"
|
||||
size="icon-sm"
|
||||
>
|
||||
<EllipsisVerticalIcon />
|
||||
<span className="sr-only">Abrir menu</span>
|
||||
</Button>
|
||||
</DropdownMenuTrigger>
|
||||
<DropdownMenuContent align="end" className="w-46 *:cursor-pointer">
|
||||
<DownloadItem id={row.id} disabled={!cert} />
|
||||
<DropdownMenuSeparator />
|
||||
<RemoveDedupItem id={row.id} disabled={progress > 0} />
|
||||
<CancelItem id={row.id} disabled={progress > 0} />
|
||||
</DropdownMenuContent>
|
||||
</DropdownMenu>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function DownloadItem({ id, ...props }: { id: string }) {
|
||||
const [loading, { set }] = useToggle(false)
|
||||
|
||||
const download = async (e) => {
|
||||
e.preventDefault()
|
||||
set(true)
|
||||
const r = await fetch(`/~/api/enrollments/${id}/download`, {
|
||||
method: 'GET'
|
||||
})
|
||||
|
||||
if (r.ok) {
|
||||
const { presigned_url } = (await r.json()) as { presigned_url: string }
|
||||
window.open(presigned_url, '_blank')
|
||||
}
|
||||
set(false)
|
||||
}
|
||||
|
||||
return (
|
||||
<DropdownMenuItem onSelect={download} {...props}>
|
||||
{loading ? <Spinner /> : <FileBadgeIcon />} Baixar certificado
|
||||
</DropdownMenuItem>
|
||||
)
|
||||
}
|
||||
|
||||
function RemoveDedupItem({ id, ...props }: { id: string }) {
|
||||
const [loading, { set }] = useToggle(false)
|
||||
const { orgid } = useParams()
|
||||
const { table } = useDataTable<Enrollment>()
|
||||
|
||||
const cancel = async (e) => {
|
||||
e.preventDefault()
|
||||
set(true)
|
||||
|
||||
const r = await fetch(`/~/api/enrollments/${orgid}/dedupwindow`, {
|
||||
method: 'DELETE'
|
||||
})
|
||||
|
||||
if (r.ok) {
|
||||
toast.info('A proteção contra duplicação foi removida')
|
||||
// @ts-ignore
|
||||
table.options.meta?.removeRow?.(id)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<AlertDialog>
|
||||
<AlertDialogTrigger asChild>
|
||||
<DropdownMenuItem
|
||||
variant="destructive"
|
||||
onSelect={(e) => e.preventDefault()}
|
||||
{...props}
|
||||
>
|
||||
<LockOpenIcon /> Remover proteção
|
||||
</DropdownMenuItem>
|
||||
</AlertDialogTrigger>
|
||||
<AlertDialogContent>
|
||||
<AlertDialogHeader>
|
||||
<AlertDialogTitle>Tem certeza absoluta?</AlertDialogTitle>
|
||||
<AlertDialogDescription>
|
||||
Esta ação não pode ser desfeita. Isso remove a proteção contra
|
||||
duplicação permanentemente desta matrícula.
|
||||
</AlertDialogDescription>
|
||||
</AlertDialogHeader>
|
||||
<AlertDialogFooter className="*:cursor-pointer">
|
||||
<AlertDialogCancel>Cancelar</AlertDialogCancel>
|
||||
<AlertDialogAction asChild>
|
||||
<Button onClick={cancel} disabled={loading} variant="destructive">
|
||||
{loading ? <Spinner /> : null} Continuar
|
||||
</Button>
|
||||
</AlertDialogAction>
|
||||
</AlertDialogFooter>
|
||||
</AlertDialogContent>
|
||||
</AlertDialog>
|
||||
)
|
||||
}
|
||||
|
||||
function CancelItem({ id, ...props }: { id: string }) {
|
||||
const [loading, { set }] = useToggle(false)
|
||||
const { orgid } = useParams()
|
||||
const { table } = useDataTable<Enrollment>()
|
||||
|
||||
const cancel = async (e) => {
|
||||
e.preventDefault()
|
||||
set(true)
|
||||
|
||||
const r = await fetch(`/~/api/enrollments/${orgid}/cancel`, {
|
||||
method: 'PATCH'
|
||||
})
|
||||
|
||||
if (r.ok) {
|
||||
toast.info('A matrícula foi cancelada')
|
||||
// @ts-ignore
|
||||
table.options.meta?.removeRow?.(id)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<AlertDialog>
|
||||
<AlertDialogTrigger asChild>
|
||||
<DropdownMenuItem
|
||||
variant="destructive"
|
||||
onSelect={(e) => e.preventDefault()}
|
||||
{...props}
|
||||
>
|
||||
<CircleXIcon /> Cancelar
|
||||
</DropdownMenuItem>
|
||||
</AlertDialogTrigger>
|
||||
<AlertDialogContent>
|
||||
<AlertDialogHeader>
|
||||
<AlertDialogTitle>Tem certeza absoluta?</AlertDialogTitle>
|
||||
<AlertDialogDescription>
|
||||
Esta ação não pode ser desfeita. Isso cancelar permanentemente a
|
||||
matrícula deste colaborador.
|
||||
</AlertDialogDescription>
|
||||
</AlertDialogHeader>
|
||||
<AlertDialogFooter className="*:cursor-pointer">
|
||||
<AlertDialogCancel>Cancelar</AlertDialogCancel>
|
||||
<AlertDialogAction asChild>
|
||||
<Button onClick={cancel} disabled={loading} variant="destructive">
|
||||
{loading ? <Spinner /> : null} Continuar
|
||||
</Button>
|
||||
</AlertDialogAction>
|
||||
</AlertDialogFooter>
|
||||
</AlertDialogContent>
|
||||
</AlertDialog>
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user