vchikalkin d8f374d5da feat(customers): add getCustomers API and enhance customer queries
- Introduced getCustomers action and corresponding server method to fetch customer data with pagination and sorting.
- Updated hooks to support infinite querying of customers, improving data handling in components.
- Refactored ContactsList and related components to utilize the new customer fetching logic, enhancing user experience.
- Adjusted filter labels in dropdowns for better clarity and user understanding.
2025-09-10 12:50:54 +03:00

70 lines
2.2 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 {
setStep(UNIFIED_STEPS[0] ?? 'loading');
}
initialized.current = true;
}, [
customer,
initData,
setClientId,
setMasterId,
setServiceIds,
setStep,
setStepsSequence,
step,
]);
}