create useIsMaster hook to prevent duplication

This commit is contained in:
vchikalkin 2025-06-26 13:23:03 +03:00
parent f47ec19551
commit fead5353e7
11 changed files with 38 additions and 39 deletions

View File

@ -2,14 +2,12 @@
'use client';
import FloatingActionPanel from '../shared/action-panel';
import { type OrderComponentProps } from './types';
import { useCustomerQuery } from '@/hooks/api/customers';
import { useIsMaster } from '@/hooks/api/customers';
import { useOrderMutation, useOrderQuery } from '@/hooks/api/orders';
import { Enum_Customer_Role, Enum_Order_State } from '@repo/graphql/types';
import { Enum_Order_State } from '@repo/graphql/types';
export function OrderButtons({ documentId }: Readonly<OrderComponentProps>) {
const { data: { customer } = {} } = useCustomerQuery();
const isMaster = customer?.role === Enum_Customer_Role.Master;
const isMaster = useIsMaster();
const { data: { order } = {} } = useOrderQuery({ documentId });

View File

@ -1,10 +1,8 @@
/* eslint-disable canonical/id-match */
'use client';
import { useCustomerQuery } from '@/hooks/api/customers';
import { useCustomerQuery, useIsMaster } from '@/hooks/api/customers';
import { useOrdersQuery } from '@/hooks/api/orders';
import { useDateTimeStore } from '@/stores/datetime';
import { Enum_Customer_Role } from '@repo/graphql/types';
import { HorizontalCalendar } from '@repo/ui/components/ui/horizontal-calendar';
import dayjs from 'dayjs';
import { useState } from 'react';
@ -12,7 +10,7 @@ import { useState } from 'react';
export function DateSelect() {
const { data: { customer } = {} } = useCustomerQuery();
const isMaster = customer?.role === Enum_Customer_Role.Master;
const isMaster = useIsMaster();
const [currentMonthDate, setCurrentMonthDate] = useState(new Date());

View File

@ -1,17 +1,15 @@
/* eslint-disable canonical/id-match */
'use client';
import { OrderCard } from '@/components/shared/order-card';
import { useCustomerQuery } from '@/hooks/api/customers';
import { useCustomerQuery, useIsMaster } from '@/hooks/api/customers';
import { useOrdersQuery } from '@/hooks/api/orders';
import { useDateTimeStore } from '@/stores/datetime';
import { Enum_Customer_Role } from '@repo/graphql/types';
import { formatDate } from '@repo/utils/datetime-format';
export function ClientsOrdersList() {
const { data: { customer } = {} } = useCustomerQuery();
const isMaster = customer?.role === Enum_Customer_Role.Master;
const isMaster = useIsMaster();
const selectedDate = useDateTimeStore((store) => store.date);

View File

@ -1,15 +1,10 @@
/* eslint-disable canonical/id-match */
'use client';
import { type ProfileProps } from '../types';
import { LinkButton } from './link-button';
import { useCustomerQuery } from '@/hooks/api/customers';
import { Enum_Customer_Role } from '@repo/graphql/types';
import { useIsMaster } from '@/hooks/api/customers';
export function LinksCard({ telegramId }: Readonly<ProfileProps>) {
const { data: { customer } = {} } = useCustomerQuery({ telegramId });
const isMaster = customer?.role === Enum_Customer_Role.Master;
export function LinksCard() {
const isMaster = useIsMaster();
return (
<div className="flex flex-col gap-4 p-4 py-0">

View File

@ -1,15 +1,13 @@
/* eslint-disable canonical/id-match */
'use client';
import { OrderCard } from '../shared/order-card';
import { type ProfileProps } from './types';
import { useCustomerQuery } from '@/hooks/api/customers';
import { useCustomerQuery, useIsMaster } from '@/hooks/api/customers';
import { useOrdersQuery } from '@/hooks/api/orders';
import { Enum_Customer_Role } from '@repo/graphql/types';
export function ProfileOrdersList({ telegramId }: Readonly<ProfileProps>) {
const { data: { customer } = {} } = useCustomerQuery();
const isMaster = customer?.role === Enum_Customer_Role.Master;
const isMaster = useIsMaster();
const { data: { customer: profile } = {} } = useCustomerQuery({ telegramId });

View File

@ -1,6 +1,7 @@
'use client';
import { getCustomer, updateCustomer } from '@/actions/api/customers';
import { isCustomerMaster } from '@repo/utils/customer';
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import { useSession } from 'next-auth/react';
@ -15,6 +16,14 @@ export const useCustomerQuery = (variables?: Parameters<typeof getCustomer>[0])
});
};
export const useIsMaster = () => {
const { data: { customer } = {} } = useCustomerQuery();
if (!customer) return false;
return isCustomerMaster(customer);
};
export const useCustomerMutation = () => {
const { data: session } = useSession();
const telegramId = session?.user?.telegramId;

View File

@ -14,6 +14,7 @@
"test:e2e": "playwright test"
},
"dependencies": {
"@repo/graphql": "workspace:*",
"@repo/ui": "workspace:*",
"@repo/utils": "workspace:",
"@tanstack/react-query": "^5.64.1",
@ -35,7 +36,6 @@
"devDependencies": {
"@playwright/test": "^1.49.1",
"@repo/eslint-config": "workspace:*",
"@repo/graphql": "workspace:*",
"@repo/lint-staged-config": "workspace:*",
"@repo/typescript-config": "workspace:*",
"@testing-library/react": "^16.1.0",

View File

@ -1,9 +1,7 @@
/* eslint-disable canonical/id-match */
'use client';
import { useOrderStore } from './context';
import { type Steps } from './types';
import { useCustomerQuery } from '@/hooks/api/customers';
import { Enum_Customer_Role } from '@repo/graphql/types';
import { useCustomerQuery, useIsMaster } from '@/hooks/api/customers';
import { useEffect } from 'react';
const STEPS: Steps[] = [
@ -23,22 +21,21 @@ export function useInitOrderStore() {
const setClientId = useOrderStore((store) => store.setClientId);
const setStep = useOrderStore((store) => store.setStep);
const setStepsSequence = useOrderStore((store) => store._setStepSequence);
const isMaster = useIsMaster();
useEffect(() => {
const role = customer?.role;
if (role === Enum_Customer_Role.Master && customer) {
if (isMaster && customer) {
setMasterId(customer?.documentId);
}
if (role === Enum_Customer_Role.Client && customer) {
if (!isMaster && customer) {
setClientId(customer?.documentId);
}
const steps = role === Enum_Customer_Role.Master ? MASTER_STEPS : CLIENT_STEPS;
const steps = isMaster ? MASTER_STEPS : CLIENT_STEPS;
const initialStep = steps[0] as Steps;
setStepsSequence(steps);
setStep(initialStep);
}, [customer, setClientId, setMasterId, setStep, setStepsSequence]);
}, [customer, isMaster, setClientId, setMasterId, setStep, setStepsSequence]);
}

View File

@ -7,7 +7,8 @@
"test:unit": "vitest"
},
"exports": {
"./datetime-format": "./src/datetime-format.ts"
"./datetime-format": "./src/datetime-format.ts",
"./customer": "./src/customer.ts"
},
"dependencies": {
"dayjs": "catalog:",

View File

@ -0,0 +1,5 @@
import * as GQL from '../../graphql/types';
export function isCustomerMaster(customer: GQL.CustomerFieldsFragment) {
return customer?.role === GQL.Enum_Customer_Role.Master;
}

6
pnpm-lock.yaml generated
View File

@ -144,6 +144,9 @@ importers:
apps/web:
dependencies:
'@repo/graphql':
specifier: workspace:*
version: link:../../packages/graphql
'@repo/ui':
specifier: workspace:*
version: link:../../packages/ui
@ -202,9 +205,6 @@ importers:
'@repo/eslint-config':
specifier: workspace:*
version: link:../../packages/eslint-config
'@repo/graphql':
specifier: workspace:*
version: link:../../packages/graphql
'@repo/lint-staged-config':
specifier: workspace:*
version: link:../../packages/lint-staged-config