context/order: skip client-select in client steps

This commit is contained in:
vchikalkin 2025-04-10 14:36:27 +03:00
parent ec32f56f8b
commit dd99e7d984
2 changed files with 41 additions and 25 deletions

View File

@ -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' });

View File

@ -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;
}
};
}