50 lines
1.4 KiB
TypeScript
50 lines
1.4 KiB
TypeScript
'use client';
|
|
import { addSlot, getSlot, getSlots, updateSlot } from '@/actions/slots';
|
|
import { ScheduleSlotsContext } from '@/context/schedule-slots';
|
|
// 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(ScheduleSlotsContext);
|
|
|
|
return useQuery({
|
|
queryFn: () => getSlots({ filters: { datestart: { gte: selectedDate } } }),
|
|
queryKey: ['schedule', 'slots', selectedDate],
|
|
});
|
|
};
|
|
|
|
type Props = {
|
|
documentId: string;
|
|
};
|
|
|
|
export const useSlotQuery = ({ documentId }: Props) =>
|
|
useQuery({
|
|
queryFn: () => getSlot({ documentId }),
|
|
queryKey: ['schedule', 'slots', documentId],
|
|
});
|
|
|
|
export const useSlotMutation = ({ documentId }: Props) => {
|
|
const { refetch } = useSlotQuery({ documentId });
|
|
|
|
return useMutation({
|
|
mutationFn: updateSlot,
|
|
mutationKey: ['schedule', 'slots', documentId],
|
|
onSuccess: () => refetch(),
|
|
});
|
|
};
|
|
|
|
export const useSlotAdd = () => {
|
|
const { refetch } = useSlots();
|
|
|
|
return useMutation({
|
|
mutationFn: addSlot,
|
|
mutationKey: ['schedule', 'slots', 'add'],
|
|
onSuccess: () => refetch(),
|
|
});
|
|
};
|