49 lines
1.4 KiB
TypeScript

/* 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[] = [
'master-select',
'client-select',
'service-select',
'datetime-select',
'success',
];
const clientSteps = masterSteps.filter((step) => step !== 'client-select');
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;
}
};
}