diff --git a/apps/web/components/orders/components/contacts-grid/index.tsx b/apps/web/components/orders/components/contacts-grid/index.tsx index 5ecc955..65e43ec 100644 --- a/apps/web/components/orders/components/contacts-grid/index.tsx +++ b/apps/web/components/orders/components/contacts-grid/index.tsx @@ -1,10 +1,13 @@ +/* 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 () { @@ -28,14 +31,28 @@ export const MastersGrid = withContext(ContactsFilterProvider)(function () { }); export const ClientsGrid = withContext(ContactsFilterProvider)(function () { - const { contacts, isLoading, setFilter } = useCustomerContacts(); - const { clientId, setClientId } = use(OrderContext); + 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; 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 e4c3c0a..2c20408 100644 --- a/apps/web/context/order/reducer.tsx +++ b/apps/web/context/order/reducer.tsx @@ -1,8 +1,6 @@ -/* 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 masterSteps: Steps[] = [ +const steps: Steps[] = [ 'master-select', 'client-select', 'service-select', @@ -10,39 +8,29 @@ const masterSteps: Steps[] = [ 'success', ]; -const clientSteps = masterSteps.filter((step) => step !== 'client-select'); +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]; -export function getStepsReducer(profile: CustomerFieldsFragment | undefined) { - if (profile?.role === Enum_Customer_Role.Master) return createStepsReducer(masterSteps); - - 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; + return nextStep ? { ...state, step: nextStep } : state; } - }; + + 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; + } }