From 518a25a1fca55d83399526f609bc6f79c4e0cf7e Mon Sep 17 00:00:00 2001 From: IlyaDoronin Date: Mon, 26 Feb 2024 12:27:10 +0300 Subject: [PATCH] feat: promocode requests --- src/api/promocode.ts | 88 +++++++++++++++++++ .../CreatePromocodeForm.tsx | 22 ++++- 2 files changed, 107 insertions(+), 3 deletions(-) create mode 100644 src/api/promocode.ts diff --git a/src/api/promocode.ts b/src/api/promocode.ts new file mode 100644 index 0000000..fedee00 --- /dev/null +++ b/src/api/promocode.ts @@ -0,0 +1,88 @@ +import { makeRequest } from "@frontend/kitui"; + +import { parseAxiosError } from "@root/utils/parse-error"; + +type CreatePromocodeBody = { + codeword: string; + description: string; + greetings: string; + dueTo: number; + activationCount: number; + bonus: { + privilege: { + privilegeID: string; + amount: number; + }; + discount: { + layer: number; + factor: number; + target: string; + threshold: number; + }; + }; +}; + +type GetPromocodeListBody = { + page: number; + limit: number; + filter: { + active: boolean; + text?: string; + }; +}; + +type Promocode = CreatePromocodeBody & { + outdated: boolean; + offLimit: boolean; + delete: boolean; + createdAt: string; +}; + +type PromocodeList = { + count: 0; + items: Promocode[]; +}; + +const baseUrl = process.env.REACT_APP_DOMAIN + "/codeword/promocode"; + +export const getPromocodeList = async ( + body: GetPromocodeListBody +): Promise<[PromocodeList | null, string?]> => { + try { + const promocodeListResponse = await makeRequest< + GetPromocodeListBody, + PromocodeList + >({ + url: baseUrl + "/getList", + body, + useToken: false, + }); + + return [promocodeListResponse]; + } catch (nativeError) { + const [error] = parseAxiosError(nativeError); + + return [null, `Ошибка при получении списка промокодов. ${error}`]; + } +}; + +export const createPromocode = async ( + body: CreatePromocodeBody +): Promise<[Promocode | null, string?]> => { + try { + const createPromocodeResponse = await makeRequest< + CreatePromocodeBody, + Promocode + >({ + url: baseUrl + "/create", + body, + useToken: false, + }); + + return [createPromocodeResponse]; + } catch (nativeError) { + const [error] = parseAxiosError(nativeError); + + return [null, `Ошибка создания промокода. ${error}`]; + } +}; diff --git a/src/pages/dashboard/Content/PromocodeManagement/CreatePromocodeForm.tsx b/src/pages/dashboard/Content/PromocodeManagement/CreatePromocodeForm.tsx index 74a81f5..33212fa 100644 --- a/src/pages/dashboard/Content/PromocodeManagement/CreatePromocodeForm.tsx +++ b/src/pages/dashboard/Content/PromocodeManagement/CreatePromocodeForm.tsx @@ -15,6 +15,7 @@ import { DesktopDatePicker } from "@mui/x-date-pickers/DesktopDatePicker"; import { requestPrivileges } from "@root/services/privilegies.service"; import { usePrivilegeStore } from "@root/stores/privilegesStore"; +import { createPromocode } from "@root/api/promocode"; import { SERVICE_LIST } from "@root/model/privilege"; import theme from "@root/theme"; @@ -93,17 +94,32 @@ export const CreatePromocodeForm = () => { requestPrivileges(); }, []); - const createPromocode = (values: FormValues) => { + const submitForm = async (values: FormValues) => { const body = { ...values, threshold: values.layer === 1 ? values.threshold : values.threshold * 100, }; - console.log(body); + await createPromocode({ + codeword: body.codeword, + description: body.description, + greetings: body.greetings, + dueTo: body.dueTo, + activationCount: body.activationCount, + bonus: { + privilege: { privilegeID: body.privilegeId, amount: body.amount }, + discount: { + layer: body.layer, + factor: body.factor, + target: body.target, + threshold: body.threshold, + }, + }, + }); }; return ( - + {({ values, handleChange, handleBlur, setFieldValue }) => (