Revert "contacts: skip client step for client"

This reverts commit db9af07dab9df9428561a1952f5a2c91c5b9d88d.
This commit is contained in:
vchikalkin 2025-04-16 13:56:05 +03:00
parent 47144e8126
commit 68d2343e98
3 changed files with 44 additions and 45 deletions

View File

@ -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 <LoadingSpinner />;
if (isClient) return null;
return (
<ContactsGridBase

View File

@ -1,6 +1,7 @@
'use client';
import { reducer } 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,7 +10,10 @@ export function useEntityState() {
}
export function useStep() {
const [state, dispatch] = useReducer(reducer, { step: 'master-select' });
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' });
const nextStep = () => dispatch({ type: 'NEXT_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 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;
}
};
}