update menu
This commit is contained in:
@@ -64,7 +64,8 @@ def put_course(course_id: str):
|
|||||||
event = router.current_event
|
event = router.current_event
|
||||||
|
|
||||||
if not event.decoded_body:
|
if not event.decoded_body:
|
||||||
raise BadRequestError('Invalid request body') now_ = now()
|
raise BadRequestError('Invalid request body')
|
||||||
|
|
||||||
body = parse(
|
body = parse(
|
||||||
event.headers,
|
event.headers,
|
||||||
BytesIO(event.decoded_body.encode()),
|
BytesIO(event.decoded_body.encode()),
|
||||||
@@ -98,7 +99,7 @@ def put_course(course_id: str):
|
|||||||
':cert': course.cert.model_dump(),
|
':cert': course.cert.model_dump(),
|
||||||
':access_period': course.access_period,
|
':access_period': course.access_period,
|
||||||
':draft': course.draft,
|
':draft': course.draft,
|
||||||
':updated_at': now_,
|
':updated_at': now(),
|
||||||
},
|
},
|
||||||
cond_expr='attribute_exists(sk)',
|
cond_expr='attribute_exists(sk)',
|
||||||
exc_cls=BadRequestError,
|
exc_cls=BadRequestError,
|
||||||
|
|||||||
@@ -21,5 +21,9 @@ dyn = DynamoDBPersistenceLayer(ENROLLMENT_TABLE, dynamodb_client)
|
|||||||
@router.get('/<enrollment_id>')
|
@router.get('/<enrollment_id>')
|
||||||
def get_enrollment(enrollment_id: str):
|
def get_enrollment(enrollment_id: str):
|
||||||
return dyn.collection.get_items(
|
return dyn.collection.get_items(
|
||||||
TransactKey(enrollment_id) + SortKey('0') + SortKey('ORG') + SortKey('LOCK')
|
TransactKey(enrollment_id)
|
||||||
|
+ SortKey('0')
|
||||||
|
+ SortKey('ORG', rename_key='org')
|
||||||
|
+ SortKey('CANCEL_POLICY', rename_key='cancel_policy')
|
||||||
|
+ SortKey('LOCK', rename_key='lock')
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
'use client'
|
'use client'
|
||||||
|
|
||||||
import type { CellContext, ColumnDef } from '@tanstack/react-table'
|
import type { CellContext, ColumnDef } from '@tanstack/react-table'
|
||||||
import { useToggle } from 'ahooks'
|
import { useRequest, useToggle } from 'ahooks'
|
||||||
import {
|
import {
|
||||||
CircleXIcon,
|
CircleXIcon,
|
||||||
EllipsisVerticalIcon,
|
EllipsisVerticalIcon,
|
||||||
@@ -9,7 +9,7 @@ import {
|
|||||||
HelpCircleIcon,
|
HelpCircleIcon,
|
||||||
LockOpenIcon
|
LockOpenIcon
|
||||||
} from 'lucide-react'
|
} from 'lucide-react'
|
||||||
import { NavLink, useParams } from 'react-router'
|
import type { ComponentProps, MouseEvent } from 'react'
|
||||||
import { toast } from 'sonner'
|
import { toast } from 'sonner'
|
||||||
|
|
||||||
import {
|
import {
|
||||||
@@ -40,7 +40,6 @@ import { cn, initials } from '@repo/ui/lib/utils'
|
|||||||
|
|
||||||
import { Abbr } from '@/components/abbr'
|
import { Abbr } from '@/components/abbr'
|
||||||
import { DataTableColumnHeader } from '@/components/data-table/column-header'
|
import { DataTableColumnHeader } from '@/components/data-table/column-header'
|
||||||
import { useDataTable } from '@/components/data-table/data-table'
|
|
||||||
import { labels, statuses } from './data'
|
import { labels, statuses } from './data'
|
||||||
|
|
||||||
// This type is used to define the shape of our data.
|
// This type is used to define the shape of our data.
|
||||||
@@ -218,13 +217,40 @@ function cellDate<TData>({
|
|||||||
return <></>
|
return <></>
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function getEnrollment(id: string) {
|
||||||
|
const r = await fetch(`/~/api/enrollments/${id}`, {
|
||||||
|
method: 'GET'
|
||||||
|
})
|
||||||
|
|
||||||
|
return (await r.json()) as {
|
||||||
|
cancel_policy?: any
|
||||||
|
lock?: { hash: string }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function ActionMenu({ row }: { row: any }) {
|
function ActionMenu({ row }: { row: any }) {
|
||||||
|
const [open, { set: setOpen }] = useToggle(false)
|
||||||
const cert = row.original?.cert
|
const cert = row.original?.cert
|
||||||
const progress = row.getValue('progress')
|
const { data, loading, run, refresh } = useRequest(getEnrollment, {
|
||||||
|
manual: true
|
||||||
|
})
|
||||||
|
|
||||||
|
const onSuccess = () => {
|
||||||
|
refresh()
|
||||||
|
setOpen(false)
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex justify-end items-center">
|
<div className="flex justify-end items-center">
|
||||||
<DropdownMenu>
|
<DropdownMenu
|
||||||
|
modal={false}
|
||||||
|
open={open}
|
||||||
|
onOpenChange={(open) => {
|
||||||
|
setOpen(open)
|
||||||
|
if (data) return
|
||||||
|
run(row.id)
|
||||||
|
}}
|
||||||
|
>
|
||||||
<DropdownMenuTrigger asChild>
|
<DropdownMenuTrigger asChild>
|
||||||
<Button
|
<Button
|
||||||
variant="ghost"
|
variant="ghost"
|
||||||
@@ -236,20 +262,45 @@ function ActionMenu({ row }: { row: any }) {
|
|||||||
</Button>
|
</Button>
|
||||||
</DropdownMenuTrigger>
|
</DropdownMenuTrigger>
|
||||||
<DropdownMenuContent align="end" className="w-46 *:cursor-pointer">
|
<DropdownMenuContent align="end" className="w-46 *:cursor-pointer">
|
||||||
<DownloadItem id={row.id} disabled={!cert} />
|
<DownloadItem
|
||||||
|
id={row.id}
|
||||||
|
disabled={!cert}
|
||||||
|
onSuccess={() => setOpen(false)}
|
||||||
|
/>
|
||||||
<DropdownMenuSeparator />
|
<DropdownMenuSeparator />
|
||||||
<RemoveDedupItem id={row.id} disabled={progress > 0} />
|
{loading ? (
|
||||||
<CancelItem id={row.id} disabled={progress > 0} />
|
<DropdownMenuItem disabled>
|
||||||
|
<Spinner />
|
||||||
|
</DropdownMenuItem>
|
||||||
|
) : (
|
||||||
|
<>
|
||||||
|
<RemoveDedupItem
|
||||||
|
id={row.id}
|
||||||
|
lock={data?.lock}
|
||||||
|
onSuccess={onSuccess}
|
||||||
|
/>
|
||||||
|
<CancelItem
|
||||||
|
id={row.id}
|
||||||
|
cancelPolicy={data?.cancel_policy}
|
||||||
|
onSuccess={onSuccess}
|
||||||
|
/>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
</DropdownMenuContent>
|
</DropdownMenuContent>
|
||||||
</DropdownMenu>
|
</DropdownMenu>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
function DownloadItem({ id, ...props }: { id: string }) {
|
type ItemProps = ComponentProps<typeof DropdownMenuItem> & {
|
||||||
|
id: string
|
||||||
|
onSuccess?: () => void
|
||||||
|
}
|
||||||
|
|
||||||
|
function DownloadItem({ id, onSuccess, ...props }: ItemProps) {
|
||||||
const [loading, { set }] = useToggle(false)
|
const [loading, { set }] = useToggle(false)
|
||||||
|
|
||||||
const download = async (e) => {
|
const download = async (e: Event) => {
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
set(true)
|
set(true)
|
||||||
const r = await fetch(`/~/api/enrollments/${id}/download`, {
|
const r = await fetch(`/~/api/enrollments/${id}/download`, {
|
||||||
@@ -259,8 +310,10 @@ function DownloadItem({ id, ...props }: { id: string }) {
|
|||||||
if (r.ok) {
|
if (r.ok) {
|
||||||
const { presigned_url } = (await r.json()) as { presigned_url: string }
|
const { presigned_url } = (await r.json()) as { presigned_url: string }
|
||||||
window.open(presigned_url, '_blank')
|
window.open(presigned_url, '_blank')
|
||||||
}
|
|
||||||
set(false)
|
set(false)
|
||||||
|
onSuccess?.()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -270,32 +323,43 @@ function DownloadItem({ id, ...props }: { id: string }) {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
function RemoveDedupItem({ id, ...props }: { id: string }) {
|
function RemoveDedupItem({
|
||||||
|
id,
|
||||||
|
lock,
|
||||||
|
onSuccess,
|
||||||
|
...props
|
||||||
|
}: ItemProps & {
|
||||||
|
lock?: { hash: string }
|
||||||
|
}) {
|
||||||
const [loading, { set }] = useToggle(false)
|
const [loading, { set }] = useToggle(false)
|
||||||
const { orgid } = useParams()
|
const [open, { set: setOpen }] = useToggle(false)
|
||||||
const { table } = useDataTable<Enrollment>()
|
|
||||||
|
|
||||||
const cancel = async (e) => {
|
const cancel = async (e: MouseEvent<HTMLButtonElement>) => {
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
set(true)
|
set(true)
|
||||||
|
|
||||||
const r = await fetch(`/~/api/enrollments/${orgid}/dedupwindow`, {
|
if (!lock) return
|
||||||
method: 'DELETE'
|
|
||||||
|
const r = await fetch(`/~/api/enrollments/${id}/dedupwindow`, {
|
||||||
|
method: 'DELETE',
|
||||||
|
body: JSON.stringify({ lock_hash: lock.hash }),
|
||||||
|
headers: new Headers({ 'Content-Type': 'application/json' })
|
||||||
})
|
})
|
||||||
|
|
||||||
if (r.ok) {
|
if (r.ok) {
|
||||||
toast.info('A proteção contra duplicação foi removida')
|
toast.info('A proteção contra duplicação foi removida')
|
||||||
// @ts-ignore
|
setOpen(false)
|
||||||
table.options.meta?.removeRow?.(id)
|
onSuccess?.()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<AlertDialog>
|
<AlertDialog open={open} onOpenChange={setOpen}>
|
||||||
<AlertDialogTrigger asChild>
|
<AlertDialogTrigger asChild>
|
||||||
<DropdownMenuItem
|
<DropdownMenuItem
|
||||||
variant="destructive"
|
variant="destructive"
|
||||||
onSelect={(e) => e.preventDefault()}
|
onSelect={(e) => e.preventDefault()}
|
||||||
|
disabled={!lock}
|
||||||
{...props}
|
{...props}
|
||||||
>
|
>
|
||||||
<LockOpenIcon /> Remover proteção
|
<LockOpenIcon /> Remover proteção
|
||||||
@@ -322,32 +386,43 @@ function RemoveDedupItem({ id, ...props }: { id: string }) {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
function CancelItem({ id, ...props }: { id: string }) {
|
function CancelItem({
|
||||||
|
id,
|
||||||
|
cancelPolicy,
|
||||||
|
lock,
|
||||||
|
...props
|
||||||
|
}: ItemProps & {
|
||||||
|
cancelPolicy?: object
|
||||||
|
lock?: {
|
||||||
|
hash: string
|
||||||
|
}
|
||||||
|
}) {
|
||||||
const [loading, { set }] = useToggle(false)
|
const [loading, { set }] = useToggle(false)
|
||||||
const { orgid } = useParams()
|
const [open, { set: setOpen }] = useToggle(false)
|
||||||
const { table } = useDataTable<Enrollment>()
|
|
||||||
|
|
||||||
const cancel = async (e) => {
|
const cancel = async (e: MouseEvent<HTMLButtonElement>) => {
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
set(true)
|
set(true)
|
||||||
|
|
||||||
const r = await fetch(`/~/api/enrollments/${orgid}/cancel`, {
|
const r = await fetch(`/~/api/enrollments/${id}/cancel`, {
|
||||||
method: 'PATCH'
|
method: 'POST',
|
||||||
|
headers: new Headers({ 'Content-Type': 'application/json' })
|
||||||
})
|
})
|
||||||
|
|
||||||
if (r.ok) {
|
if (r.ok) {
|
||||||
toast.info('A matrícula foi cancelada')
|
toast.info('A matrícula foi cancelada')
|
||||||
// @ts-ignore
|
|
||||||
table.options.meta?.removeRow?.(id)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setOpen(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<AlertDialog>
|
<AlertDialog open={open} onOpenChange={setOpen}>
|
||||||
<AlertDialogTrigger asChild>
|
<AlertDialogTrigger asChild>
|
||||||
<DropdownMenuItem
|
<DropdownMenuItem
|
||||||
variant="destructive"
|
variant="destructive"
|
||||||
onSelect={(e) => e.preventDefault()}
|
onSelect={(e) => e.preventDefault()}
|
||||||
|
disabled={!cancelPolicy}
|
||||||
{...props}
|
{...props}
|
||||||
>
|
>
|
||||||
<CircleXIcon /> Cancelar
|
<CircleXIcon /> Cancelar
|
||||||
|
|||||||
Reference in New Issue
Block a user