From f9f485728c001b077fedbc8a193e5553c62d8b24 Mon Sep 17 00:00:00 2001 From: vchikalkin Date: Sat, 8 Feb 2025 16:04:25 +0300 Subject: [PATCH] use slots hooks --- .../schedule/time-slots/add-slot-form.tsx | 13 ++--- .../schedule/time-slots/edit-slot-form.tsx | 37 +++++--------- apps/web/hooks/slots/index.ts | 48 +++++++++++++++---- 3 files changed, 53 insertions(+), 45 deletions(-) diff --git a/apps/web/components/schedule/time-slots/add-slot-form.tsx b/apps/web/components/schedule/time-slots/add-slot-form.tsx index 9014f05..d26c2ab 100644 --- a/apps/web/components/schedule/time-slots/add-slot-form.tsx +++ b/apps/web/components/schedule/time-slots/add-slot-form.tsx @@ -1,13 +1,11 @@ /* eslint-disable canonical/id-match */ 'use client'; -import { addSlot } from '@/actions/slots'; import { ScheduleSlotsContext } from '@/context/schedule-slots'; -import { useSlots } from '@/hooks/slots'; +import { useSlotAdd } from '@/hooks/slots'; import { combineDateTime } from '@/utils/date'; import { Enum_Slot_State } from '@repo/graphql/types'; import { Button } from '@repo/ui/components/ui/button'; import { Input } from '@repo/ui/components/ui/input'; -import { useMutation } from '@tanstack/react-query'; import { PlusSquare } from 'lucide-react'; import { type FormEvent, use, useState } from 'react'; @@ -16,13 +14,8 @@ export function AddSlotForm() { const [endTime, setEndTime] = useState(''); const { selectedDate } = use(ScheduleSlotsContext); - const { refetch: refetchSlots } = useSlots(); - const { mutate } = useMutation({ - mutationFn: addSlot, - mutationKey: ['schedule', 'slots', 'add'], - onSuccess: () => refetchSlots(), - }); + const { isPending, mutate } = useSlotAdd(); const handleSubmit = (event: FormEvent) => { event.preventDefault(); @@ -40,6 +33,7 @@ export function AddSlotForm() {
setStartTime(event.target.value)} required @@ -49,6 +43,7 @@ export function AddSlotForm() {
setEndTime(event.target.value)} required diff --git a/apps/web/components/schedule/time-slots/edit-slot-form.tsx b/apps/web/components/schedule/time-slots/edit-slot-form.tsx index bb97545..6338123 100644 --- a/apps/web/components/schedule/time-slots/edit-slot-form.tsx +++ b/apps/web/components/schedule/time-slots/edit-slot-form.tsx @@ -1,11 +1,10 @@ 'use client'; -import { getSlot, updateSlot } from '@/actions/slots'; import { ScheduleSlotsContext } from '@/context/schedule-slots'; +import { useSlotMutation, useSlotQuery } from '@/hooks/slots'; import { combineDateTime, getTimeString } from '@/utils/date'; import { type SlotFieldsFragment } from '@repo/graphql/types'; import { Button } from '@repo/ui/components/ui/button'; import { Input } from '@repo/ui/components/ui/input'; -import { useMutation, useQuery } from '@tanstack/react-query'; import { Loader, Pencil, Save } from 'lucide-react'; import { type FormEvent, use, useEffect, useState } from 'react'; @@ -21,20 +20,8 @@ export function EditSlotForm({ documentId }: Readonly) { const { selectedDate } = use(ScheduleSlotsContext); - const { - data, - isLoading: isLoadingSlot, - refetch: refetchSlot, - } = useQuery({ - queryFn: () => getSlot({ documentId }), - queryKey: ['schedule', 'slots', documentId], - }); - - const { isPending: isPendingSlotUpdate, mutate } = useMutation({ - mutationFn: updateSlot, - mutationKey: ['schedule', 'slots', documentId], - onSuccess: () => refetchSlot(), - }); + const { data, isLoading: isLoadingSlotQuery } = useSlotQuery({ documentId }); + const { isPending: isPendingSlotMutation, mutate } = useSlotMutation({ documentId }); const handleSubmit = (event: FormEvent) => { event.preventDefault(); @@ -90,16 +77,14 @@ export function EditSlotForm({ documentId }: Readonly) { )} - {editMode ? ( - - ) : null} - {editMode ? null : ( - - )} + + ); } diff --git a/apps/web/hooks/slots/index.ts b/apps/web/hooks/slots/index.ts index bfe9267..05f5ebf 100644 --- a/apps/web/hooks/slots/index.ts +++ b/apps/web/hooks/slots/index.ts @@ -1,21 +1,49 @@ 'use client'; -import { getSlots } from '@/actions/slots'; +import { addSlot, getSlot, getSlots, updateSlot } from '@/actions/slots'; import { ScheduleSlotsContext } from '@/context/schedule-slots'; -import { useQuery } from '@tanstack/react-query'; +// 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, - }, - }, - }), + 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(), + }); +};