41 lines
1.1 KiB
TypeScript
41 lines
1.1 KiB
TypeScript
/* eslint-disable canonical/id-match */
|
|
'use client';
|
|
import { OrderStoreContext } from './context';
|
|
import { type OrderStore, type Steps } from './types';
|
|
import { useProfileQuery } from '@/hooks/profile';
|
|
import { Enum_Customer_Role } from '@repo/graphql/types';
|
|
import { useContext } from 'react';
|
|
import { useStore } from 'zustand';
|
|
|
|
export const MASTER_STEPS: Steps[] = [
|
|
'master-select',
|
|
'client-select',
|
|
'service-select',
|
|
'datetime-select',
|
|
'success',
|
|
];
|
|
|
|
export const CLIENT_STEPS: Steps[] = MASTER_STEPS.filter((step) => step !== 'client-select');
|
|
|
|
export function useInitialState() {
|
|
const { data } = useProfileQuery();
|
|
const role = data?.role;
|
|
const steps = role === Enum_Customer_Role.Master ? MASTER_STEPS : CLIENT_STEPS;
|
|
const initialStep = steps[0];
|
|
|
|
return {
|
|
_stepSequence: steps,
|
|
step: initialStep,
|
|
} as OrderStore;
|
|
}
|
|
|
|
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);
|
|
};
|