feat: create&view order api&model (#65)
This commit is contained in:
parent
1ea43d6b99
commit
fd79ab4e72
1
src/api/index.ts
Normal file
1
src/api/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './landing';
|
@ -1,25 +0,0 @@
|
||||
import { getConfigValue } from '@brojs/cli';
|
||||
|
||||
import { Order } from '../models/landing';
|
||||
|
||||
enum LandingEndpoints {
|
||||
ORDER_VIEW = '/order'
|
||||
}
|
||||
|
||||
const LandingService = () => {
|
||||
const endpoint = getConfigValue('dry-wash.api');
|
||||
|
||||
const fetchOrder = async (orderId: Order.Id) => {
|
||||
const response = await fetch(`${endpoint}${LandingEndpoints.ORDER_VIEW}/${orderId}`);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to fetch order: ${response.status}`);
|
||||
}
|
||||
|
||||
return await response.json();
|
||||
};
|
||||
|
||||
return { fetchOrder };
|
||||
};
|
||||
|
||||
export { LandingService, LandingEndpoints };
|
107
src/api/landing.tsx
Normal file
107
src/api/landing.tsx
Normal file
@ -0,0 +1,107 @@
|
||||
import { getConfigValue } from '@brojs/cli';
|
||||
import { useEffect, useState } from 'react';
|
||||
|
||||
import { CreateOrder, GetOrder } from '../models/api';
|
||||
|
||||
import { QueryState, Trigger } from './types';
|
||||
|
||||
enum LandingEndpoints {
|
||||
ORDER = '/order',
|
||||
ORDER_CREATE = '/order/create',
|
||||
}
|
||||
|
||||
const endpoint = getConfigValue('dry-wash.api');
|
||||
|
||||
const useCreateOrderMutation = <D extends CreateOrder.Response>(): [
|
||||
Trigger<CreateOrder.Params, QueryState<D>['data']>,
|
||||
QueryState<D>,
|
||||
] => {
|
||||
const [isLoading, setIsLoading] = useState<QueryState<D>['isLoading']>(false);
|
||||
const [isSuccess, setIsSuccess] = useState<QueryState<D>['isSuccess']>();
|
||||
const [data, setData] = useState<QueryState<D>['data']>();
|
||||
const [isError, setIsError] = useState<QueryState<D>['isError']>();
|
||||
const [error, setError] = useState<QueryState<D>['error']>();
|
||||
|
||||
const createOrder = async ({ body }: CreateOrder.Params) => {
|
||||
setIsLoading(true);
|
||||
|
||||
try {
|
||||
const response = await fetch(
|
||||
`${endpoint}${LandingEndpoints.ORDER_CREATE}`,
|
||||
{
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(body),
|
||||
},
|
||||
);
|
||||
|
||||
if (!response.ok) {
|
||||
const errorResponseObject =
|
||||
(await response.json()) as QueryState<D>['error'];
|
||||
setIsError(true);
|
||||
setError(errorResponseObject);
|
||||
throw errorResponseObject;
|
||||
}
|
||||
|
||||
const dataResponseObject =
|
||||
(await response.json()) as QueryState<D>['data'];
|
||||
setIsSuccess(true);
|
||||
setData(dataResponseObject);
|
||||
|
||||
return dataResponseObject;
|
||||
} catch (error) {
|
||||
setIsError(true);
|
||||
setError(error);
|
||||
throw error;
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
return [createOrder, { isLoading, isSuccess, data, isError, error }];
|
||||
};
|
||||
|
||||
const useGetOrderQuery = <D extends GetOrder.Response>({
|
||||
orderId,
|
||||
}: GetOrder.Params): QueryState<D> => {
|
||||
const [isLoading, setIsLoading] = useState<QueryState<D>['isLoading']>(true);
|
||||
const [isSuccess, setIsSuccess] = useState<QueryState<D>['isSuccess']>();
|
||||
const [data, setData] = useState<QueryState<D>['data']>();
|
||||
const [isError, setIsError] = useState<QueryState<D>['isError']>();
|
||||
const [error, setError] = useState<QueryState<D>['error']>();
|
||||
|
||||
useEffect(() => {
|
||||
(async () => {
|
||||
try {
|
||||
const response = await fetch(
|
||||
`${endpoint}${LandingEndpoints.ORDER}/${orderId}`,
|
||||
);
|
||||
|
||||
if (!response.ok) {
|
||||
const errorResponseObject =
|
||||
(await response.json()) as QueryState<D>['error'];
|
||||
setIsError(true);
|
||||
setError(errorResponseObject);
|
||||
throw errorResponseObject;
|
||||
}
|
||||
|
||||
const dataResponseObject =
|
||||
(await response.json()) as QueryState<D>['data'];
|
||||
setIsSuccess(true);
|
||||
setData(dataResponseObject);
|
||||
} catch (error) {
|
||||
setIsError(true);
|
||||
setError(error);
|
||||
throw error;
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
})();
|
||||
}, []);
|
||||
|
||||
return { isLoading, isSuccess, data, isError, error };
|
||||
};
|
||||
|
||||
export { useCreateOrderMutation, useGetOrderQuery };
|
22
src/api/types.ts
Normal file
22
src/api/types.ts
Normal file
@ -0,0 +1,22 @@
|
||||
export type QueryData<D> = {
|
||||
success: true;
|
||||
body: D;
|
||||
};
|
||||
|
||||
export type QueryErrorData = {
|
||||
success: false;
|
||||
error: string;
|
||||
};
|
||||
|
||||
export type QueryState<D> = {
|
||||
isLoading: boolean;
|
||||
isSuccess: boolean;
|
||||
data: QueryData<D>;
|
||||
isError: boolean;
|
||||
error: {
|
||||
status: number;
|
||||
data: QueryErrorData;
|
||||
};
|
||||
};
|
||||
|
||||
export type Trigger<P, D> = (params: P) => Promise<D>;
|
@ -1 +1 @@
|
||||
export * from './order-view';
|
||||
export * from './order';
|
@ -1,14 +0,0 @@
|
||||
import { Order } from "../landing";
|
||||
|
||||
export type FetchOrderQueryResponse = {
|
||||
id: string;
|
||||
orderDate: string;
|
||||
carNumber: string;
|
||||
carBody: number;
|
||||
carColor?: string;
|
||||
startWashTime: string;
|
||||
endWashTime: string;
|
||||
status: Order.Status;
|
||||
phone: string;
|
||||
location: string;
|
||||
};
|
18
src/models/api/order.ts
Normal file
18
src/models/api/order.ts
Normal file
@ -0,0 +1,18 @@
|
||||
/* eslint-disable @typescript-eslint/no-namespace */
|
||||
import { Order } from "../landing";
|
||||
|
||||
export namespace CreateOrder {
|
||||
export type Response = {
|
||||
id: Order.Id
|
||||
};
|
||||
export type Params = {
|
||||
body: Order.Create
|
||||
};
|
||||
};
|
||||
|
||||
export namespace GetOrder {
|
||||
export type Response = Order.View;
|
||||
export type Params = {
|
||||
orderId: Order.Id
|
||||
};
|
||||
};
|
1
src/models/common.ts
Normal file
1
src/models/common.ts
Normal file
@ -0,0 +1 @@
|
||||
export type IsoDate = string; // YYYY-MM-DDThh:mm:ss.mmmZ
|
@ -1,4 +1,4 @@
|
||||
export type RegistrationNumber = string; // А012ВЕ
|
||||
export type RegistrationNumber = string; // А012ВЕ16
|
||||
|
||||
export type Color = string; // #000000
|
||||
|
||||
|
@ -1,3 +1,5 @@
|
||||
import { IsoDate } from "../common";
|
||||
|
||||
import { Car, Customer, Washing } from ".";
|
||||
|
||||
export type Id = string;
|
||||
@ -25,14 +27,16 @@ export type Create = {
|
||||
};
|
||||
|
||||
export type View = {
|
||||
id: Id;
|
||||
orderDate: string,
|
||||
status: Status,
|
||||
phone: Customer.PhoneNumber;
|
||||
carNumber: Car.RegistrationNumber;
|
||||
carBody: Car.BodyStyle;
|
||||
carColor?: Car.Color;
|
||||
location: Washing.Location;
|
||||
datetimeBegin: Washing.AvailableBeginDateTime;
|
||||
datetimeEnd: Washing.AvailableEndDateTime;
|
||||
startWashTime: Washing.AvailableBeginDateTime;
|
||||
endWashTime: Washing.AvailableEndDateTime;
|
||||
status: Status,
|
||||
notes: string;
|
||||
created: IsoDate;
|
||||
updated: IsoDate;
|
||||
id: Id;
|
||||
};
|
@ -1,5 +1,7 @@
|
||||
export type Location = string; // ?
|
||||
import { IsoDate } from "../common";
|
||||
|
||||
export type AvailableBeginDateTime = string; // YYYY-MM-DDThh:mm
|
||||
export type Location = string; // 55.754364, 48.743295 Университетская улица, 1, Иннополис, Верхнеуслонский район, Республика Татарстан (Татарстан), 420500
|
||||
|
||||
export type AvailableEndDateTime = string; // YYYY-MM-DDThh:mm
|
||||
export type AvailableBeginDateTime = IsoDate;
|
||||
|
||||
export type AvailableEndDateTime = IsoDate;
|
@ -1,14 +1,17 @@
|
||||
{
|
||||
"success": true,
|
||||
"body": {
|
||||
"id": "order1",
|
||||
"orderDate": "2024-11-24T08:41:46.366Z",
|
||||
"status": "progress",
|
||||
"carNumber": "A123BC",
|
||||
"carBody": 1,
|
||||
"startWashTime": "2024-11-24T10:30:00.000Z",
|
||||
"endWashTime": "2024-11-24T16:30:00.000Z",
|
||||
"phone": "79001234563",
|
||||
"location": "55.754364, 48.743295 Университетская улица, 1, Иннополис, Верхнеуслонский район, Республика Татарстан (Татарстан), 420500"
|
||||
"phone": "+79876543210",
|
||||
"carNumber": "А123АА16",
|
||||
"carBody": 2,
|
||||
"carColor": "#ffffff",
|
||||
"startWashTime": "2025-01-19T14:03:00.000Z",
|
||||
"endWashTime": "2025-01-19T14:03:00.000Z",
|
||||
"location": "55.793833888711006,49.19037910644527 Республика Татарстан (Татарстан), Казань, жилой район Седьмое Небо",
|
||||
"status": "progress",
|
||||
"notes": "",
|
||||
"created": "2025-01-19T14:04:02.985Z",
|
||||
"updated": "2025-01-19T14:04:02.987Z",
|
||||
"id": "678d06527d78ec30be2679d8"
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user