refactor services api
This commit is contained in:
parent
7d368d9ef0
commit
39498dbcff
18
apps/web/actions/api/services.ts
Normal file
18
apps/web/actions/api/services.ts
Normal file
@ -0,0 +1,18 @@
|
||||
'use server';
|
||||
|
||||
import { useService } from './lib/service';
|
||||
import { ServicesService } from '@repo/graphql/api/services';
|
||||
|
||||
const getServicesService = useService(ServicesService);
|
||||
|
||||
export async function getService(...variables: Parameters<ServicesService['getService']>) {
|
||||
const service = await getServicesService();
|
||||
|
||||
return service.getService(...variables);
|
||||
}
|
||||
|
||||
export async function getServices(...variables: Parameters<ServicesService['getServices']>) {
|
||||
const service = await getServicesService();
|
||||
|
||||
return service.getServices(...variables);
|
||||
}
|
||||
@ -4,6 +4,7 @@
|
||||
import type * as ApolloTypes from '../../../packages/graphql/node_modules/@apollo/client/core';
|
||||
import { getCustomer, getCustomerMasters } from './api/customers';
|
||||
import { _temporaryGetCustomer } from './api/lib/service';
|
||||
import { getService } from './api/services';
|
||||
import { getSlot } from './api/slots';
|
||||
import * as api from '@repo/graphql/api';
|
||||
import { Enum_Customer_Role, Enum_Slot_State } from '@repo/graphql/types';
|
||||
@ -61,9 +62,9 @@ export async function createOrder(input: OrderInput) {
|
||||
throw new Error('Invalid master');
|
||||
}
|
||||
|
||||
const service = await api.getService({ documentId: input.serviceId });
|
||||
const { service } = await getService({ documentId: input.serviceId });
|
||||
|
||||
const endTime = sumTime(input.time, service?.data?.service?.duration);
|
||||
const endTime = sumTime(input.time, service?.duration);
|
||||
const payload = {
|
||||
client: input.clientId,
|
||||
date: formatDate(input.date).db(),
|
||||
|
||||
@ -1,21 +0,0 @@
|
||||
'use server';
|
||||
import { getCustomer } from './api/customers';
|
||||
import { _temporaryGetCustomer } from './api/lib/service';
|
||||
import * as api from '@repo/graphql/api/service';
|
||||
// eslint-disable-next-line sonarjs/no-internal-api-use
|
||||
import type * as ApolloTypes from '@repo/graphql/node_modules/@apollo/client/core';
|
||||
import { type GetServicesQueryVariables } from '@repo/graphql/types';
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
type FixTypescriptCringe = ApolloTypes.FetchResult;
|
||||
|
||||
export async function getServices(input?: GetServicesQueryVariables) {
|
||||
const variables = await _temporaryGetCustomer();
|
||||
const { customer } = await getCustomer(variables);
|
||||
|
||||
const filters = input || { filters: { master: { documentId: { eq: customer?.documentId } } } };
|
||||
|
||||
return api.getServices(filters);
|
||||
}
|
||||
|
||||
export const getService = api.getService;
|
||||
@ -1,17 +1,27 @@
|
||||
'use client';
|
||||
import { useServicesQuery } from '@/hooks/services';
|
||||
import { useServicesQuery } from '@/hooks/api/services';
|
||||
import { useOrderStore } from '@/stores/order';
|
||||
import { type ServiceFieldsFragment } from '@repo/graphql/types';
|
||||
import { cn } from '@repo/ui/lib/utils';
|
||||
|
||||
export function ServiceSelect() {
|
||||
const { data } = useServicesQuery();
|
||||
const masterId = useOrderStore((store) => store.masterId);
|
||||
|
||||
const { data: { services } = {} } = useServicesQuery({
|
||||
filters: {
|
||||
master: {
|
||||
documentId: {
|
||||
eq: masterId,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (!services?.length) return null;
|
||||
|
||||
return (
|
||||
<div>
|
||||
{data?.data.services.map(
|
||||
(service) => service && <ServiceCard key={service.documentId} {...service} />,
|
||||
)}
|
||||
{services.map((service) => service && <ServiceCard key={service.documentId} {...service} />)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
18
apps/web/hooks/api/services.ts
Normal file
18
apps/web/hooks/api/services.ts
Normal file
@ -0,0 +1,18 @@
|
||||
'use client';
|
||||
import { getService, getServices } from '@/actions/api/services';
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
|
||||
export const useServicesQuery = (input: Parameters<typeof getServices>[0]) => {
|
||||
const masterId = input.filters?.master?.documentId?.eq;
|
||||
|
||||
return useQuery({
|
||||
queryFn: () => getServices(input),
|
||||
queryKey: ['services', 'master', masterId, 'list'],
|
||||
});
|
||||
};
|
||||
|
||||
export const useServiceQuery = (input: Parameters<typeof getService>[0]) =>
|
||||
useQuery({
|
||||
queryFn: () => getService(input),
|
||||
queryKey: ['services', 'documentId', input.documentId],
|
||||
});
|
||||
@ -1,15 +0,0 @@
|
||||
'use client';
|
||||
import { getServices } from '@/actions/services';
|
||||
// eslint-disable-next-line sonarjs/no-internal-api-use
|
||||
import type * as ApolloTypes from '@repo/graphql/node_modules/@apollo/client/core';
|
||||
import { type GetServicesQueryVariables } from '@repo/graphql/types';
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
type FixTypescriptCringe = ApolloTypes.FetchResult;
|
||||
|
||||
export const useServicesQuery = (input?: GetServicesQueryVariables) =>
|
||||
useQuery({
|
||||
queryFn: () => getServices(input),
|
||||
queryKey: ['services', 'list'],
|
||||
});
|
||||
@ -1,5 +1,5 @@
|
||||
export * from './auth';
|
||||
export * from './customers';
|
||||
export * from './order';
|
||||
export * from './service';
|
||||
export * from './services';
|
||||
export * from './slots';
|
||||
|
||||
@ -1,21 +0,0 @@
|
||||
'use server';
|
||||
import { getClientWithToken } from '../apollo/client';
|
||||
import * as GQL from '../types';
|
||||
|
||||
export async function getServices(input: GQL.GetServicesQueryVariables) {
|
||||
const { query } = await getClientWithToken();
|
||||
|
||||
return query({
|
||||
query: GQL.GetServicesDocument,
|
||||
variables: input,
|
||||
});
|
||||
}
|
||||
|
||||
export async function getService(input: GQL.GetServiceQueryVariables) {
|
||||
const { query } = await getClientWithToken();
|
||||
|
||||
return query({
|
||||
query: GQL.GetServiceDocument,
|
||||
variables: input,
|
||||
});
|
||||
}
|
||||
32
packages/graphql/api/services.ts
Normal file
32
packages/graphql/api/services.ts
Normal file
@ -0,0 +1,32 @@
|
||||
import { getClientWithToken } from '../apollo/client';
|
||||
import * as GQL from '../types';
|
||||
import { BaseService } from './base';
|
||||
import { type VariablesOf } from '@graphql-typed-document-node/core';
|
||||
|
||||
export class ServicesService extends BaseService {
|
||||
async getService(variables: VariablesOf<typeof GQL.GetServiceDocument>) {
|
||||
const { query } = await getClientWithToken();
|
||||
|
||||
const result = await query({
|
||||
query: GQL.GetServiceDocument,
|
||||
variables,
|
||||
});
|
||||
|
||||
if (result.error) throw new Error(result.error.message);
|
||||
|
||||
return result.data;
|
||||
}
|
||||
|
||||
async getServices(variables: VariablesOf<typeof GQL.GetServicesDocument>) {
|
||||
const { query } = await getClientWithToken();
|
||||
|
||||
const result = await query({
|
||||
query: GQL.GetServicesDocument,
|
||||
variables,
|
||||
});
|
||||
|
||||
if (result.error) throw new Error(result.error.message);
|
||||
|
||||
return result.data;
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user