diff --git a/apps/web/components/orders/components/contacts-grid/index.tsx b/apps/web/components/orders/components/contacts-grid/index.tsx index 65e43ec..5ecc955 100644 --- a/apps/web/components/orders/components/contacts-grid/index.tsx +++ b/apps/web/components/orders/components/contacts-grid/index.tsx @@ -1,13 +1,10 @@ -/* eslint-disable canonical/id-match */ 'use client'; import { ContactsGridBase } from './components'; import { LoadingSpinner } from '@/components/common/spinner'; import { ContactsFilterProvider } from '@/context/contacts-filter'; import { OrderContext } from '@/context/order'; import { useCustomerContacts } from '@/hooks/contacts'; -import { useProfileQuery } from '@/hooks/profile'; import { withContext } from '@/utils/context'; -import { Enum_Customer_Role } from '@repo/graphql/types'; import { use, useEffect } from 'react'; export const MastersGrid = withContext(ContactsFilterProvider)(function () { @@ -31,28 +28,14 @@ export const MastersGrid = withContext(ContactsFilterProvider)(function () { }); export const ClientsGrid = withContext(ContactsFilterProvider)(function () { - const { contacts, isLoading: isLoadingContacts, setFilter } = useCustomerContacts(); - const { clientId, nextStep, setClientId } = use(OrderContext); - const { data: profile, isLoading: isLoadingProfile } = useProfileQuery(); - - const isLoading = isLoadingContacts || isLoadingProfile; - - const isClient = profile?.role === Enum_Customer_Role.Client; + const { contacts, isLoading, setFilter } = useCustomerContacts(); + const { clientId, setClientId } = use(OrderContext); useEffect(() => { setFilter('clients'); }, [setFilter]); - useEffect(() => { - if (isClient && !clientId) { - setClientId(profile?.documentId); - } - - if (isClient && clientId) nextStep(); - }, [clientId, isClient, nextStep, profile, setClientId]); - if (isLoading) return ; - if (isClient) return null; return ( dispatch({ payload, type: 'SET_STEP' }); const nextStep = () => dispatch({ type: 'NEXT_STEP' }); diff --git a/apps/web/context/order/reducer.tsx b/apps/web/context/order/reducer.tsx index 2c20408..e4c3c0a 100644 --- a/apps/web/context/order/reducer.tsx +++ b/apps/web/context/order/reducer.tsx @@ -1,6 +1,8 @@ +/* eslint-disable canonical/id-match */ import { type Action, type State, type Steps } from './types'; +import { type CustomerFieldsFragment, Enum_Customer_Role } from '@repo/graphql/types'; -const steps: Steps[] = [ +const masterSteps: Steps[] = [ 'master-select', 'client-select', 'service-select', @@ -8,29 +10,39 @@ const steps: Steps[] = [ 'success', ]; -export function reducer(state: State, action: Action): State { - switch (action.type) { - case 'NEXT_STEP': { - const currentIndex = steps.indexOf(state.step); - const nextIndex = currentIndex + 1; - const nextStep = steps[nextIndex]; +const clientSteps = masterSteps.filter((step) => step !== 'client-select'); - return nextStep ? { ...state, step: nextStep } : state; - } +export function getStepsReducer(profile: CustomerFieldsFragment | undefined) { + if (profile?.role === Enum_Customer_Role.Master) return createStepsReducer(masterSteps); - case 'PREV_STEP': { - const currentIndex = steps.indexOf(state.step); - const previousIndex = currentIndex - 1; - const previousStep = steps[previousIndex]; - - return previousStep ? { ...state, step: previousStep } : state; - } - - case 'SET_STEP': { - return { ...state, step: action.payload }; - } - - default: - return state; - } + return createStepsReducer(clientSteps); +} + +function createStepsReducer(stepsSequence: Steps[]) { + return function (state: State, action: Action): State { + switch (action.type) { + case 'NEXT_STEP': { + const currentIndex = stepsSequence.indexOf(state.step); + const nextIndex = currentIndex + 1; + const nextStep = stepsSequence[nextIndex]; + + return nextStep ? { ...state, step: nextStep } : state; + } + + case 'PREV_STEP': { + const currentIndex = stepsSequence.indexOf(state.step); + const previousIndex = currentIndex - 1; + const previousStep = stepsSequence[previousIndex]; + + return previousStep ? { ...state, step: previousStep } : state; + } + + case 'SET_STEP': { + return { ...state, step: action.payload }; + } + + default: + return state; + } + }; }