57 lines
1.8 KiB
TypeScript
57 lines
1.8 KiB
TypeScript
/* eslint-disable canonical/id-match */
|
|
'use client';
|
|
|
|
import { OrderStoreContext } from './context';
|
|
import { type OrderStore, type Steps } from './types';
|
|
import { useCustomerQuery } from '@/hooks/api/customers';
|
|
import { Enum_Customer_Role } from '@repo/graphql/types';
|
|
import { useContext, useEffect } from 'react';
|
|
import { useStore } from 'zustand';
|
|
|
|
export const useOrderStore = <T,>(selector: (store: OrderStore) => T): T => {
|
|
const orderStoreContext = useContext(OrderStoreContext);
|
|
|
|
if (!orderStoreContext) {
|
|
throw new Error(`useOrderStore must be used within OrderStoreProvider`);
|
|
}
|
|
|
|
return useStore(orderStoreContext, selector);
|
|
};
|
|
|
|
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() {
|
|
const { data: { customer } = {} } = useCustomerQuery();
|
|
|
|
const setMasterId = useOrderStore((store) => store.setMasterId);
|
|
const setClientId = useOrderStore((store) => store.setClientId);
|
|
const setStep = useOrderStore((store) => store.setStep);
|
|
const setStepsSequence = useOrderStore((store) => store._setStepSequence);
|
|
|
|
useEffect(() => {
|
|
const role = customer?.role;
|
|
|
|
if (role === Enum_Customer_Role.Master && customer) {
|
|
setMasterId(customer?.documentId);
|
|
}
|
|
|
|
if (role === Enum_Customer_Role.Client && customer) {
|
|
setClientId(customer?.documentId);
|
|
}
|
|
|
|
const steps = role === Enum_Customer_Role.Master ? MASTER_STEPS : CLIENT_STEPS;
|
|
const initialStep = steps[0] as Steps;
|
|
|
|
setStepsSequence(steps);
|
|
setStep(initialStep);
|
|
}, [customer, setClientId, setMasterId, setStep, setStepsSequence]);
|
|
}
|