typo refactor hooks

This commit is contained in:
vchikalkin 2025-05-16 19:30:01 +03:00
parent 30001b993e
commit c91a188761
5 changed files with 23 additions and 21 deletions

View File

@ -1,6 +1,6 @@
/* eslint-disable canonical/id-match */
'use client';
import { useSlots } from '@/hooks/api/slots';
import { useSlotsQuery } from '@/hooks/api/slots';
import { useOrderStore } from '@/stores/order';
import { Enum_Slot_State, type SlotFieldsFragment } from '@repo/graphql/types';
import { Button } from '@repo/ui/components/ui/button';
@ -25,7 +25,7 @@ const generateTimeSlots = (slots: SlotFieldsFragment[]): Array<{ slotId: string;
export function TimeSelect() {
const masterId = useOrderStore((store) => store.masterId);
const date = useOrderStore((store) => store.date);
const { data: { slots } = {} } = useSlots({
const { data: { slots } = {} } = useSlotsQuery({
filters: {
date: {
eq: date,

View File

@ -2,7 +2,6 @@
'use client';
import { EditableTimeRangeForm } from './components/time-range';
import { ScheduleTimeContext, ScheduleTimeContextProvider } from './context';
import { ScheduleContext } from '@/context/schedule';
import { useSlotCreate } from '@/hooks/api/slots';
import { withContext } from '@/utils/context';
import { Enum_Slot_State } from '@repo/graphql/types';
@ -13,9 +12,7 @@ import { type FormEvent, use } from 'react';
export const DaySlotAddForm = withContext(ScheduleTimeContextProvider)(function () {
const { endTime, resetTime, startTime } = use(ScheduleTimeContext);
const { selectedDate } = use(ScheduleContext);
const { isPending, mutate: addSlot } = useSlotCreate({ date: selectedDate });
const { isPending, mutate: addSlot } = useSlotCreate();
const handleSubmit = (event: FormEvent) => {
event.preventDefault();

View File

@ -3,12 +3,14 @@ import { SlotCard } from './components/slot-card';
import { DaySlotAddForm } from './day-slot-add-form';
import { LoadingSpinner } from '@/components/common/spinner';
import { ScheduleContext } from '@/context/schedule';
import { useSlots } from '@/hooks/api/slots';
import { useSlotsQuery } from '@/hooks/api/slots';
import { use } from 'react';
export function DaySlotsList() {
const { selectedDate } = use(ScheduleContext);
const { data: { slots } = {}, isLoading } = useSlots({ filters: { date: { eq: selectedDate } } });
const { data: { slots } = {}, isLoading } = useSlotsQuery({
filters: { date: { eq: selectedDate } },
});
if (isLoading) return <LoadingSpinner />;

View File

@ -3,9 +3,9 @@ import { getCustomer, updateCustomer } from '@/actions/api/customers';
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import { useSession } from 'next-auth/react';
export const useCustomerQuery = (props?: Parameters<typeof getCustomer>[0]) => {
export const useCustomerQuery = (variables?: Parameters<typeof getCustomer>[0]) => {
const { data: session } = useSession();
const telegramId = props?.telegramId || session?.user?.telegramId;
const telegramId = variables?.telegramId || session?.user?.telegramId;
return useQuery({
enabled: Boolean(telegramId),

View File

@ -3,7 +3,7 @@ import { useCustomerQuery } from './customers';
import { createSlot, deleteSlot, getSlot, getSlots, updateSlot } from '@/actions/api/slots';
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
export const useSlots = (variables: Parameters<typeof getSlots>[0]) => {
export const useSlotsQuery = (variables: Parameters<typeof getSlots>[0]) => {
const { data: { customer } = {} } = useCustomerQuery();
const masterId = variables.filters?.master?.documentId?.eq || customer?.documentId;
@ -41,23 +41,22 @@ export const useSlotMutation = ({
});
};
export const useSlotCreate = ({ date }: { date: Date }) => {
export const useSlotCreate = () => {
const { data: { customer } = {} } = useCustomerQuery();
const masterId = customer?.documentId;
const queryClient = useQueryClient();
return useMutation({
mutationFn: ({ input }: { input: Omit<Parameters<typeof createSlot>[0]['input'], 'date'> }) =>
createSlot({ input: { ...input, date } }),
mutationFn: createSlot,
mutationKey: ['slot', 'create'],
onSuccess: () => {
if (masterId && date) {
queryClient.invalidateQueries({
queryKey: ['slots', { date: date?.toISOString(), masterId }],
});
}
},
onSuccess: masterId
? (data) => {
queryClient.invalidateQueries({
queryKey: ['slots', { date: data?.createSlot?.date?.toISOString(), masterId }],
});
}
: undefined,
});
};
@ -77,6 +76,10 @@ export const useSlotDelete = ({ documentId }: Parameters<typeof deleteSlot>[0])
queryClient.invalidateQueries({
queryKey: ['slots', { date: date?.toISOString(), masterId }],
});
queryClient.invalidateQueries({
queryKey: ['slot', documentId],
});
}
},
});