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
|
export type Color = string; // #000000
|
||||||
|
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
import { IsoDate } from "../common";
|
||||||
|
|
||||||
import { Car, Customer, Washing } from ".";
|
import { Car, Customer, Washing } from ".";
|
||||||
|
|
||||||
export type Id = string;
|
export type Id = string;
|
||||||
@ -25,14 +27,16 @@ export type Create = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export type View = {
|
export type View = {
|
||||||
id: Id;
|
|
||||||
orderDate: string,
|
|
||||||
status: Status,
|
|
||||||
phone: Customer.PhoneNumber;
|
phone: Customer.PhoneNumber;
|
||||||
carNumber: Car.RegistrationNumber;
|
carNumber: Car.RegistrationNumber;
|
||||||
carBody: Car.BodyStyle;
|
carBody: Car.BodyStyle;
|
||||||
carColor?: Car.Color;
|
carColor?: Car.Color;
|
||||||
location: Washing.Location;
|
location: Washing.Location;
|
||||||
datetimeBegin: Washing.AvailableBeginDateTime;
|
startWashTime: Washing.AvailableBeginDateTime;
|
||||||
datetimeEnd: Washing.AvailableEndDateTime;
|
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,
|
"success": true,
|
||||||
"body": {
|
"body": {
|
||||||
"id": "order1",
|
"phone": "+79876543210",
|
||||||
"orderDate": "2024-11-24T08:41:46.366Z",
|
"carNumber": "А123АА16",
|
||||||
"status": "progress",
|
"carBody": 2,
|
||||||
"carNumber": "A123BC",
|
"carColor": "#ffffff",
|
||||||
"carBody": 1,
|
"startWashTime": "2025-01-19T14:03:00.000Z",
|
||||||
"startWashTime": "2024-11-24T10:30:00.000Z",
|
"endWashTime": "2025-01-19T14:03:00.000Z",
|
||||||
"endWashTime": "2024-11-24T16:30:00.000Z",
|
"location": "55.793833888711006,49.19037910644527 Республика Татарстан (Татарстан), Казань, жилой район Седьмое Небо",
|
||||||
"phone": "79001234563",
|
"status": "progress",
|
||||||
"location": "55.754364, 48.743295 Университетская улица, 1, Иннополис, Верхнеуслонский район, Республика Татарстан (Татарстан), 420500"
|
"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