vchikalkin 5dfef524e2 refactor(contact): remove customer master role checks and simplify contact addition
- Updated the `addContact` function to allow all users to add contacts, removing the previous restriction that only masters could do so.
- Deleted the `become-master` feature and related utility functions, streamlining the codebase.
- Adjusted command settings to reflect the removal of the master role functionality.
- Refactored components and hooks to eliminate dependencies on the master role, enhancing user experience and simplifying logic.
2025-09-08 12:51:35 +03:00

69 lines
2.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'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 {
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,
]);
}