51 lines
1.5 KiB
TypeScript

/* eslint-disable canonical/id-match */
'use client';
import FloatingActionPanel from '../shared/action-panel';
import { type SlotComponentProps } from './types';
import { useSlotDelete, useSlotMutation, useSlotQuery } from '@/hooks/api/slots';
import { Enum_Slot_State } from '@repo/graphql/types';
import { useRouter } from 'next/navigation';
export function SlotButtons({ documentId }: Readonly<SlotComponentProps>) {
const { data: { slot } = {} } = useSlotQuery({ documentId });
const { isPending: isPendingUpdate, mutate: updateSlot } = useSlotMutation({ documentId });
const { isPending: isPendingDelete, mutate: deleteSlot } = useSlotDelete({ documentId });
const router = useRouter();
if (!slot) return null;
const isOpened = slot?.state === Enum_Slot_State.Open;
const isClosed = slot?.state === Enum_Slot_State.Closed;
function handleOpenSlot() {
return updateSlot({ data: { state: Enum_Slot_State.Open } });
}
function handleCloseSlot() {
return updateSlot({ data: { state: Enum_Slot_State.Closed } });
}
function handleDeleteSlot() {
router.back();
return deleteSlot();
}
const hasOrders = Boolean(slot?.orders.length);
return (
<FloatingActionPanel
isLoading={isPendingUpdate || isPendingDelete}
isOpen={isOpened}
onDelete={hasOrders ? undefined : () => handleDeleteSlot()}
onToggle={() => {
if (isOpened) handleCloseSlot();
if (isClosed) handleOpenSlot();
}}
/>
);
}