- Added a new step for client selection in the order initialization process when only a masterId is present. - Disabled cognitive complexity checks for improved code maintainability.
72 lines
2.4 KiB
TypeScript
72 lines
2.4 KiB
TypeScript
/* eslint-disable sonarjs/cognitive-complexity */
|
||
'use client';
|
||
import { useOrderStore } from './context';
|
||
import { type Steps } from './types';
|
||
import { useCustomerQuery } from '@/hooks/api/customers';
|
||
import { type OrderFieldsFragment } from '@repo/graphql/types';
|
||
import { sift } from 'radashi';
|
||
import { useEffect, useRef } from 'react';
|
||
|
||
// Унифицированные шаги для всех пользователей
|
||
const UNIFIED_STEPS: Steps[] = [
|
||
'master-select',
|
||
'client-select',
|
||
'service-select',
|
||
'datetime-select',
|
||
'success',
|
||
];
|
||
|
||
export function useInitOrderStore(initData: null | OrderFieldsFragment) {
|
||
const initialized = useRef(false);
|
||
const { data: { customer } = {} } = useCustomerQuery();
|
||
|
||
const setMasterId = useOrderStore((store) => store.setMasterId);
|
||
const setClientId = useOrderStore((store) => store.setClientId);
|
||
const setServiceIds = useOrderStore((store) => store.setServiceIds);
|
||
const setStep = useOrderStore((store) => store.setStep);
|
||
const setStepsSequence = useOrderStore((store) => store._setStepSequence);
|
||
const step = useOrderStore((store) => store.step);
|
||
|
||
useEffect(() => {
|
||
if (initialized.current || !customer || step !== 'loading') return;
|
||
|
||
setStepsSequence(UNIFIED_STEPS);
|
||
|
||
// Инициализация из initData (например, для повторного заказа)
|
||
if (initData) {
|
||
const masterId = initData.slot?.master?.documentId;
|
||
const clientId = initData.client?.documentId;
|
||
const serviceIds = sift(initData.services).map(({ documentId }) => documentId);
|
||
|
||
if (masterId) setMasterId(masterId);
|
||
if (clientId) setClientId(clientId);
|
||
if (serviceIds) setServiceIds(serviceIds);
|
||
|
||
if (masterId && clientId && serviceIds.length) {
|
||
setStep('datetime-select');
|
||
} else if (masterId && clientId) {
|
||
setStep('service-select');
|
||
} else if (masterId) {
|
||
setStep('client-select');
|
||
} else {
|
||
setStep(UNIFIED_STEPS[0] ?? 'loading');
|
||
}
|
||
} else {
|
||
// Обычная инициализация (новый заказ) - начинаем с выбора мастера
|
||
setClientId(customer.documentId);
|
||
setStep(UNIFIED_STEPS[0] ?? 'loading');
|
||
}
|
||
|
||
initialized.current = true;
|
||
}, [
|
||
customer,
|
||
initData,
|
||
setClientId,
|
||
setMasterId,
|
||
setServiceIds,
|
||
setStep,
|
||
setStepsSequence,
|
||
step,
|
||
]);
|
||
}
|