From 284142eb4fd2cf7698264851aaed665fbe35d935 Mon Sep 17 00:00:00 2001 From: vchikalkin Date: Thu, 13 Feb 2025 17:41:19 +0300 Subject: [PATCH] bring the results of queries and hooks into a single form --- apps/bot/src/index.ts | 13 +++++++---- apps/web/actions/contacts.ts | 12 ++++------ apps/web/actions/profile.ts | 10 ++++---- .../schedule/components/slot-card.tsx | 16 +++++++------ .../web/components/schedule/datetime-card.tsx | 9 ++++---- apps/web/components/schedule/orders-list.tsx | 23 +++++++++---------- apps/web/components/schedule/slot-buttons.tsx | 5 ++-- apps/web/components/schedule/slots-list.tsx | 3 ++- apps/web/hooks/contacts/query.ts | 10 ++++---- packages/graphql/api/auth.ts | 4 +--- packages/graphql/api/customer.ts | 12 +++------- packages/graphql/api/slot.ts | 12 ++-------- 12 files changed, 59 insertions(+), 70 deletions(-) diff --git a/apps/bot/src/index.ts b/apps/bot/src/index.ts index 76562c2..2e9c648 100644 --- a/apps/bot/src/index.ts +++ b/apps/bot/src/index.ts @@ -16,7 +16,8 @@ import { message } from 'telegraf/filters'; const bot = new Telegraf(environment.BOT_TOKEN); bot.start(async (context) => { - const customer = await getCustomer({ telegramId: context.from.id }); + const data = await getCustomer({ telegramId: context.from.id }); + const customer = data?.data?.customers?.at(0); if (customer) { return context.reply( @@ -33,7 +34,8 @@ bot.start(async (context) => { }); bot.command('addcontact', async (context) => { - const customer = await getCustomer({ telegramId: context.from.id }); + const data = await getCustomer({ telegramId: context.from.id }); + const customer = data?.data?.customers?.at(0); if (!customer) { return context.reply( @@ -50,7 +52,8 @@ bot.command('addcontact', async (context) => { }); bot.command('becomemaster', async (context) => { - const customer = await getCustomer({ telegramId: context.from.id }); + const data = await getCustomer({ telegramId: context.from.id }); + const customer = data?.data?.customers?.at(0); if (!customer) { return context.reply('Сначала поделитесь своим номером телефона.', KEYBOARD_SHARE_PHONE); @@ -73,7 +76,9 @@ bot.command('becomemaster', async (context) => { }); bot.on(message('contact'), async (context) => { - const customer = await getCustomer({ telegramId: context.from.id }); + const data = await getCustomer({ telegramId: context.from.id }); + const customer = data?.data?.customers?.at(0); + const isRegistration = !customer; const { contact } = context.message; diff --git a/apps/web/actions/contacts.ts b/apps/web/actions/contacts.ts index 8d9f530..3b1bcf4 100644 --- a/apps/web/actions/contacts.ts +++ b/apps/web/actions/contacts.ts @@ -9,11 +9,9 @@ export async function getClients() { const { user } = session; - const getCustomerClientsResponse = await getCustomerClients({ telegramId: user?.telegramId }); + const response = await getCustomerClients({ telegramId: user?.telegramId }); - return { - clients: getCustomerClientsResponse?.clients, - }; + return response.data?.customers?.at(0); } export async function getMasters() { @@ -22,9 +20,7 @@ export async function getMasters() { const { user } = session; - const getCustomerMastersResponse = await getCustomerMasters({ telegramId: user?.telegramId }); + const response = await getCustomerMasters({ telegramId: user?.telegramId }); - return { - masters: getCustomerMastersResponse?.masters, - }; + return response.data?.customers?.at(0); } diff --git a/apps/web/actions/profile.ts b/apps/web/actions/profile.ts index b400e59..e92a3c2 100644 --- a/apps/web/actions/profile.ts +++ b/apps/web/actions/profile.ts @@ -9,10 +9,11 @@ export async function getProfile(input?: GetCustomerQueryVariables) { if (!session) throw new Error('Missing session'); const { user } = session; - const telegramId = input?.telegramId || user?.telegramId; - const customer = await getCustomer({ telegramId }); + const { data } = await getCustomer({ telegramId }); + const customer = data?.customers?.at(0); + return customer; } @@ -22,11 +23,12 @@ export async function updateProfile(input: CustomerInput) { const { user } = session; - const customer = await getCustomer({ telegramId: user?.telegramId }); + const { data } = await getCustomer({ telegramId: user?.telegramId }); + const customer = data.customers.at(0); if (!customer) throw new Error('Customer not found'); await updateCustomerProfile({ data: input, - documentId: customer.documentId, + documentId: customer?.documentId, }); } diff --git a/apps/web/components/schedule/components/slot-card.tsx b/apps/web/components/schedule/components/slot-card.tsx index 6b3e436..d7b642c 100644 --- a/apps/web/components/schedule/components/slot-card.tsx +++ b/apps/web/components/schedule/components/slot-card.tsx @@ -28,19 +28,21 @@ export const SlotCard = withContext(ContextProvider)(function ( const { documentId } = props; const { data } = useSlotQuery({ documentId }); - if (!data?.slot) return null; + const slot = data?.data?.slot; - const ordersNumber = data.slot.orders?.length; + if (!slot) return null; + + const ordersNumber = slot.orders?.length; const hasOrders = Boolean(ordersNumber); - const isOpened = data?.slot?.state === Enum_Slot_State.Open; - const isClosed = data?.slot?.state === Enum_Slot_State.Closed; + const isOpened = slot?.state === Enum_Slot_State.Open; + const isClosed = slot?.state === Enum_Slot_State.Closed; return (
- +
- {data.slot.state && ( + {slot.state && ( - {getBadgeText(data.slot.state)} + {getBadgeText(slot.state)} )}
diff --git a/apps/web/components/schedule/datetime-card.tsx b/apps/web/components/schedule/datetime-card.tsx index 5c43ffd..413dd81 100644 --- a/apps/web/components/schedule/datetime-card.tsx +++ b/apps/web/components/schedule/datetime-card.tsx @@ -5,17 +5,18 @@ import { formatDate, formatTime } from '@/utils/date'; export function DateTimeCard({ documentId }: Readonly) { const { data } = useSlotQuery({ documentId }); + const slot = data?.data?.slot; - if (!data?.slot) return null; + if (!slot) return null; return (
- {formatDate(data?.slot?.date).user()} + {formatDate(slot?.date).user()} - {formatTime(data?.slot?.time_start).user()} + {formatTime(slot?.time_start).user()} - {formatTime(data?.slot?.time_end).user()} + {formatTime(slot?.time_end).user()}
); diff --git a/apps/web/components/schedule/orders-list.tsx b/apps/web/components/schedule/orders-list.tsx index a776671..9390509 100644 --- a/apps/web/components/schedule/orders-list.tsx +++ b/apps/web/components/schedule/orders-list.tsx @@ -22,11 +22,12 @@ export function OrdersList({ documentId, }: Readonly & { readonly className?: string }) { const { data } = useSlotQuery({ documentId }); + const slot = data?.data?.slot; return (

Записи

- {data?.slot?.orders.map((order) => { + {slot?.orders.map((order) => { return order && ; })}
@@ -41,26 +42,24 @@ function getBadgeText(state: Enum_Order_State) { function Order({ documentId }: Readonly) { const { data } = useOrderQuery({ documentId }); + const order = data?.data?.order; - if (!data?.data.order) return null; + if (!order) return null; - const isCompleted = data?.data.order?.state === Enum_Order_State.Completed; - const isCancelled = data?.data.order?.state === Enum_Order_State.Cancelled; + const isCompleted = order?.state === Enum_Order_State.Completed; + const isCancelled = order?.state === Enum_Order_State.Cancelled; return (
- + - {data.data.order.services.map((service) => service?.name).join(', ')} + {order.services.map((service) => service?.name).join(', ')} - {data?.data.order?.client?.name} + {order?.client?.name}
- {data?.data.order?.state && ( + {order?.state && ( ) { isCancelled ? 'bg-red-100 text-red-500 dark:bg-red-700 dark:text-red-100' : '', )} > - {getBadgeText(data?.data.order?.state)} + {getBadgeText(order?.state)} )}
diff --git a/apps/web/components/schedule/slot-buttons.tsx b/apps/web/components/schedule/slot-buttons.tsx index 241fb17..8a80c12 100644 --- a/apps/web/components/schedule/slot-buttons.tsx +++ b/apps/web/components/schedule/slot-buttons.tsx @@ -9,9 +9,10 @@ import { useRouter } from 'next/navigation'; export function SlotButtons({ documentId }: Readonly) { const { data } = useSlotQuery({ documentId }); + const slot = data?.data?.slot; - const isOpened = data?.slot?.state === Enum_Slot_State.Open; - const isClosed = data?.slot?.state === Enum_Slot_State.Closed; + const isOpened = slot?.state === Enum_Slot_State.Open; + const isClosed = slot?.state === Enum_Slot_State.Closed; const { mutate: mutateSlot } = useSlotMutation({ documentId }); diff --git a/apps/web/components/schedule/slots-list.tsx b/apps/web/components/schedule/slots-list.tsx index e4259fa..b99631d 100644 --- a/apps/web/components/schedule/slots-list.tsx +++ b/apps/web/components/schedule/slots-list.tsx @@ -4,7 +4,8 @@ import { LoadingSpinner } from '@/components/common/spinner'; import { useSlots } from '@/hooks/slots'; export function SlotsList() { - const { data: slots, isLoading } = useSlots(); + const { data, isLoading } = useSlots(); + const slots = data?.data.slots; if (isLoading) return ; diff --git a/apps/web/hooks/contacts/query.ts b/apps/web/hooks/contacts/query.ts index 69273a4..7f2046d 100644 --- a/apps/web/hooks/contacts/query.ts +++ b/apps/web/hooks/contacts/query.ts @@ -1,10 +1,8 @@ import { getClients, getMasters } from '@/actions/contacts'; import { useQuery } from '@tanstack/react-query'; -export const useClientsQuery = () => { - return useQuery({ queryFn: getClients, queryKey: ['contacts', 'clients', 'get'] }); -}; +export const useClientsQuery = () => + useQuery({ queryFn: getClients, queryKey: ['contacts', 'clients', 'get'] }); -export const useMastersQuery = () => { - return useQuery({ queryFn: getMasters, queryKey: ['contacts', 'masters', 'get'] }); -}; +export const useMastersQuery = () => + useQuery({ queryFn: getMasters, queryKey: ['contacts', 'masters', 'get'] }); diff --git a/packages/graphql/api/auth.ts b/packages/graphql/api/auth.ts index 138348b..667cb7f 100644 --- a/packages/graphql/api/auth.ts +++ b/packages/graphql/api/auth.ts @@ -5,13 +5,11 @@ import * as GQL from '../types'; export async function login() { const { mutate } = createApolloClient(); - const response = await mutate({ + return mutate({ mutation: GQL.LoginDocument, variables: { identifier: environment.LOGIN_GRAPHQL, password: environment.PASSWORD_GRAPHQL, }, }); - - return response; } diff --git a/packages/graphql/api/customer.ts b/packages/graphql/api/customer.ts index 352735e..024a0de 100644 --- a/packages/graphql/api/customer.ts +++ b/packages/graphql/api/customer.ts @@ -33,34 +33,28 @@ export async function createOrUpdateUser(input: GQL.CreateCustomerMutationVariab export async function getCustomer(input: GQL.GetCustomerQueryVariables) { const { query } = await getClientWithToken(); - const response = await query({ + return query({ query: GQL.GetCustomerDocument, variables: input, }); - - return response?.data?.customers?.at(0); } export async function getCustomerMasters(input: GQL.GetCustomerMastersQueryVariables) { const { query } = await getClientWithToken(); - const response = await query({ + return query({ query: GQL.GetCustomerMastersDocument, variables: input, }); - - return response?.data?.customers?.at(0); } export async function getCustomerClients(input: GQL.GetCustomerClientsQueryVariables) { const { query } = await getClientWithToken(); - const response = await query({ + return query({ query: GQL.GetCustomerClientsDocument, variables: input, }); - - return response?.data?.customers?.at(0); } type AddCustomerMasterInput = Pick & { diff --git a/packages/graphql/api/slot.ts b/packages/graphql/api/slot.ts index fd9cdbe..8e872ad 100644 --- a/packages/graphql/api/slot.ts +++ b/packages/graphql/api/slot.ts @@ -5,36 +5,28 @@ import * as GQL from '../types'; export async function createSlot(input: GQL.CreateSlotMutationVariables['input']) { const { mutate } = await getClientWithToken(); - // TODO: check slot does not overlap with others - return mutate({ mutation: GQL.CreateSlotDocument, variables: { input }, - }).catch((err) => { - console.error(JSON.stringify(err, null, 2)); }); } export async function getSlots(input: GQL.GetSlotsQueryVariables) { const { query } = await getClientWithToken(); - const response = await query({ + return query({ query: GQL.GetSlotsDocument, variables: input, }); - - return response?.data?.slots; } export async function getSlot(input: GQL.GetSlotQueryVariables) { const { query } = await getClientWithToken(); - const response = await query({ + return query({ query: GQL.GetSlotDocument, variables: input, }); - - return response?.data; } export async function updateSlot(input: GQL.UpdateSlotMutationVariables) {