diff --git a/apps/web/context/order/hooks.tsx b/apps/web/context/order/hooks.tsx index b98dbd8..2fd034e 100644 --- a/apps/web/context/order/hooks.tsx +++ b/apps/web/context/order/hooks.tsx @@ -1,6 +1,7 @@ 'use client'; -import { stepsReducer } from './reducer'; +import { getStepsReducer } from './reducer'; import { type Steps } from './types'; +import { useProfileQuery } from '@/hooks/profile'; import { useReducer, useState } from 'react'; export function useEntityState() { @@ -9,6 +10,9 @@ export function useEntityState() { } export function useStep() { + const { data: profile } = useProfileQuery(); + const stepsReducer = getStepsReducer(profile); + const [state, dispatch] = useReducer(stepsReducer, { step: 'master-select' }); const setStep = (payload: Steps) => dispatch({ payload, type: 'SET_STEP' }); diff --git a/apps/web/context/order/reducer.tsx b/apps/web/context/order/reducer.tsx index 096cf2e..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 stepsSequence: Steps[] = [ +const masterSteps: Steps[] = [ 'master-select', 'client-select', 'service-select', @@ -8,29 +10,39 @@ const stepsSequence: Steps[] = [ 'success', ]; -export function stepsReducer(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]; +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 = 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 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; + } + }; }