diff --git a/apps/web/components/orders/components/back-button.tsx b/apps/web/components/orders/components/back-button.tsx index 7cc1708..df5f0f7 100644 --- a/apps/web/components/orders/components/back-button.tsx +++ b/apps/web/components/orders/components/back-button.tsx @@ -1,4 +1,5 @@ 'use client'; +import { useOrderCreate } from '@/hooks/orders'; import { useOrderStore } from '@/stores/order'; import { Button } from '@repo/ui/components/ui/button'; @@ -6,14 +7,18 @@ export function BackButton() { const step = useOrderStore((store) => store.step); const previousStep = useOrderStore((store) => store.prevStep); - function handleOnClick() { - previousStep(); - } + const { isPending } = useOrderCreate(); if (['master-select', 'success'].includes(step)) return null; return ( - ); diff --git a/apps/web/components/orders/components/next-button.tsx b/apps/web/components/orders/components/next-button.tsx index 5c4e0df..081be1c 100644 --- a/apps/web/components/orders/components/next-button.tsx +++ b/apps/web/components/orders/components/next-button.tsx @@ -1,58 +1,21 @@ -/* eslint-disable sonarjs/no-duplicate-string */ 'use client'; -import { useOrderCreate } from '@/hooks/orders'; import { useOrderStore } from '@/stores/order'; import { Button } from '@repo/ui/components/ui/button'; -import { useEffect } from 'react'; export function NextButton() { - const { clientId, date, nextStep, serviceId, setStep, slotId, step, time } = useOrderStore( + const { clientId, date, masterId, nextStep, serviceId, step, time } = useOrderStore( (store) => store, ); - const disabled = useButtonDisabled(); - - const { isPending, isSuccess, mutate: createOrder } = useOrderCreate(); - - useEffect(() => { - if (isSuccess) setStep('success'); - }, [isSuccess, setStep]); - - function handleOnClick() { - if (step !== 'datetime-select') return nextStep(); - - if (!clientId || !date || !serviceId || !slotId || !time) return null; - - return createOrder({ clientId, date, serviceId, slotId, time }); - } - - if (isPending) { - return ( - - ); - } - - return ( - - ); -} - -function useButtonDisabled() { - const step = useOrderStore((state) => state.step); - const clientId = useOrderStore((state) => state.clientId); - const masterId = useOrderStore((state) => state.masterId); - const serviceId = useOrderStore((state) => state.serviceId); - const date = useOrderStore((state) => state.date); - const time = useOrderStore((state) => state.time); - - return ( + const isDisabled = (step === 'master-select' && !masterId) || (step === 'client-select' && !clientId) || (step === 'service-select' && !serviceId) || - (step === 'datetime-select' && (!date || !time)) + (step === 'datetime-select' && (!date || !time)); + + return ( + ); } diff --git a/apps/web/components/orders/components/submit-button.tsx b/apps/web/components/orders/components/submit-button.tsx new file mode 100644 index 0000000..35a660f --- /dev/null +++ b/apps/web/components/orders/components/submit-button.tsx @@ -0,0 +1,35 @@ +'use client'; +import { useOrderCreate } from '@/hooks/orders'; +import { useOrderStore } from '@/stores/order'; +import { Button } from '@repo/ui/components/ui/button'; +import { LoadingSpinner } from '@repo/ui/components/ui/spinner'; +import { useEffect } from 'react'; + +export function SubmitButton() { + const { clientId, date, serviceId, setStep, slotId, time } = useOrderStore((store) => store); + const isDisabled = !clientId || !serviceId || !date || !time || !slotId; + + const { isError, isPending, isSuccess, mutate: createOrder } = useOrderCreate(); + + const handleSubmit = () => { + if (isDisabled) return; + + createOrder({ clientId, date, serviceId, slotId, time }); + }; + + useEffect(() => { + if (isSuccess) setStep('success'); + if (isError) setStep('error'); + }, [isError, isSuccess, setStep]); + + return ( + + ); +} diff --git a/apps/web/components/orders/order-form.tsx b/apps/web/components/orders/order-form.tsx index 9e71f70..7b58364 100644 --- a/apps/web/components/orders/order-form.tsx +++ b/apps/web/components/orders/order-form.tsx @@ -8,6 +8,7 @@ import { NextButton, ServiceSelect, } from './components'; +import { SubmitButton } from './components/submit-button'; import { OrderStoreProvider, useOrderStore } from '@/stores/order'; import { useInitOrderStore } from '@/stores/order/hooks'; import { withContext } from '@/utils/context'; @@ -20,6 +21,19 @@ const STEP_COMPONENTS: Record = { 'service-select': , }; +function getStepComponent(step: string) { + return STEP_COMPONENTS[step] ?? null; +} + +const BUTTON_COMPONENTS: Record = { + '': , + 'datetime-select': , +}; + +function getButtonComponent(step: string) { + return BUTTON_COMPONENTS[step] ?? ; +} + export const OrderForm = withContext(OrderStoreProvider)(function () { useInitOrderStore(); @@ -30,13 +44,9 @@ export const OrderForm = withContext(OrderStoreProvider)(function () {
{getStepComponent(step)}
- + {getButtonComponent(step)}
); }); - -function getStepComponent(step: string) { - return STEP_COMPONENTS[step] ?? null; -} diff --git a/apps/web/hooks/orders/index.ts b/apps/web/hooks/orders/index.ts index 9b525ed..bdd0957 100644 --- a/apps/web/hooks/orders/index.ts +++ b/apps/web/hooks/orders/index.ts @@ -20,4 +20,5 @@ export const useOrderQuery = ({ documentId }: Props) => export const useOrderCreate = () => useMutation({ mutationFn: createOrder, + mutationKey: ['orders', 'create'], });