68 lines
1.7 KiB
TypeScript
68 lines
1.7 KiB
TypeScript
'use client';
|
|
import { addSlot, deleteSlot, getSlot, getSlots, updateSlot } from '@/actions/slots';
|
|
import { ScheduleContext } from '@/context/schedule';
|
|
import { formatDate } from '@/utils/date';
|
|
// eslint-disable-next-line sonarjs/no-internal-api-use
|
|
import type * as ApolloTypes from '@repo/graphql/node_modules/@apollo/client/core';
|
|
import { useMutation, useQuery } from '@tanstack/react-query';
|
|
import { use } from 'react';
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
type FixTypescriptCringe = ApolloTypes.FetchResult;
|
|
|
|
export const useSlots = () => {
|
|
const { selectedDate } = use(ScheduleContext);
|
|
|
|
return useQuery({
|
|
queryFn: () =>
|
|
getSlots({
|
|
filters: {
|
|
date: {
|
|
eq: formatDate(selectedDate).db(),
|
|
},
|
|
},
|
|
}),
|
|
queryKey: ['slots', 'list', selectedDate],
|
|
});
|
|
};
|
|
|
|
type Props = {
|
|
documentId: string;
|
|
};
|
|
|
|
export const useSlotQuery = ({ documentId }: Props) =>
|
|
useQuery({
|
|
queryFn: () => getSlot({ documentId }),
|
|
queryKey: ['slots', 'get', documentId],
|
|
});
|
|
|
|
export const useSlotMutation = ({ documentId }: Props) => {
|
|
const { refetch } = useSlotQuery({ documentId });
|
|
|
|
return useMutation({
|
|
mutationFn: updateSlot,
|
|
mutationKey: ['slots', 'update', documentId],
|
|
onSuccess: () => refetch(),
|
|
});
|
|
};
|
|
|
|
export const useSlotAdd = () => {
|
|
const { refetch } = useSlots();
|
|
|
|
return useMutation({
|
|
mutationFn: addSlot,
|
|
mutationKey: ['slots', 'add'],
|
|
onSuccess: () => refetch(),
|
|
});
|
|
};
|
|
|
|
export const useSlotDelete = ({ documentId }: Props) => {
|
|
const { refetch } = useSlots();
|
|
|
|
return useMutation({
|
|
mutationFn: () => deleteSlot({ documentId }),
|
|
mutationKey: ['slots', 'delete', documentId],
|
|
onSuccess: () => refetch(),
|
|
});
|
|
};
|