forked from purpleio/purple-admin-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproduct.ts
60 lines (51 loc) · 1.39 KB
/
product.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { ISO8601DateTime } from "@/types/common";
import qs from "qs";
import useSWR from "swr";
import { fetchApi } from "../base";
export interface IProduct {
id: number;
code: string;
brand: string;
name: string;
price: number;
status: string;
description?: string;
css?: string;
js?: string;
createdAt: ISO8601DateTime;
updatedAt: ISO8601DateTime;
}
export interface IProductFormValue extends Omit<IProduct, "id" | "createdAt" | "updatedAt"> {}
interface IProductsParams {
page?: number;
}
export interface IProductsResponse {
code: number;
message: string;
data: {
items: IProduct[];
page: {
currentPage: number;
pageSize: number;
totalPage: number;
totalCount: number;
};
};
}
export interface IProductResponse {
code: number;
message: string;
data: IProduct;
}
export const useProducts = (params: IProductsParams = {}) => {
return useSWR<IProductsResponse>(`api/sample/products?${qs.stringify(params)}`);
};
export const useProduct = (id: string | number) => {
return useSWR<IProductResponse>(`api/sample/products/${id}`);
};
export const createProduct = (value: IProductFormValue) => {
return fetchApi.post(`api/sample/products`, { body: JSON.stringify(value) });
};
export const updateProduct = (id: string, value: IProductFormValue) => {
return fetchApi.put(`api/sample/products/${id}`, { body: JSON.stringify(value) });
};