Vlad Chikalkin 7bcae12d54
Some checks failed
Build & Deploy Web & Bot / Build and Push to Docker Hub (push) Has been cancelled
Build & Deploy Web & Bot / Deploy to VPS (push) Has been cancelled
Fix/bugs after first release (#26)
* web/packages: upgrade next

* fix(api/orders): update master validation logic to handle optional masters

* fix(api/notify, api/orders): enhance notification messages and update order state handling for masters

* fix react typings

* refactor(order-buttons, action-panel): streamline button handlers and add return functionality

* fix(contacts, orders): replace empty state messages with DataNotFound component for better user feedback

* feat(bot): add share bot command and update environment configuration for BOT_URL

* fix: pnpm-lock.yaml

* feat(bot): implement add contact wizard scene and enhance contact handling logic

* feat(profile): add BookContactButton component to enhance booking functionality

* fix(order-buttons): update cancel and confirm button logic based on order state

* feat(service-select): share services list for all
enhance service card display with duration formatting and improve layout
2025-07-03 16:36:10 +03:00

78 lines
2.4 KiB
TypeScript

/* eslint-disable sonarjs/cognitive-complexity */
'use client';
import { useOrderStore } from './context';
import { type Steps } from './types';
import { useCustomerQuery, useIsMaster } from '@/hooks/api/customers';
import { type OrderFieldsFragment } from '@repo/graphql/types';
import { useEffect, useRef } from 'react';
const STEPS: Steps[] = [
'master-select',
'client-select',
'service-select',
'datetime-select',
'success',
];
export const MASTER_STEPS: Steps[] = STEPS.filter((step) => step !== 'master-select');
export const CLIENT_STEPS: Steps[] = STEPS.filter((step) => step !== 'client-select');
export function useInitOrderStore(initData: null | OrderFieldsFragment) {
const initialized = useRef(false);
const { data: { customer } = {} } = useCustomerQuery();
const isMaster = useIsMaster();
const setMasterId = useOrderStore((store) => store.setMasterId);
const setClientId = useOrderStore((store) => store.setClientId);
const setServiceId = useOrderStore((store) => store.setServiceId);
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;
const steps = isMaster ? MASTER_STEPS : CLIENT_STEPS;
setStepsSequence(steps);
// Инициализация из initData (например, для повторного заказа)
if (initData) {
const masterId = initData.slot?.master?.documentId;
const clientId = initData.client?.documentId;
const serviceId = initData.services?.[0]?.documentId;
if (masterId) setMasterId(masterId);
if (clientId) setClientId(clientId);
if (serviceId) setServiceId(serviceId);
if (masterId && clientId && serviceId) {
setStep('datetime-select');
} else if (masterId && clientId) {
setStep('service-select');
} else {
setStep(steps[0] ?? 'loading');
}
} else {
// Обычная инициализация (новый заказ)
if (isMaster) {
setMasterId(customer.documentId);
} else {
setClientId(customer.documentId);
}
setStep(steps[0] ?? 'loading');
}
initialized.current = true;
}, [
customer,
initData,
isMaster,
setClientId,
setMasterId,
setServiceId,
setStep,
setStepsSequence,
step,
]);
}