feat: promocode requests

This commit is contained in:
IlyaDoronin 2024-02-26 12:27:10 +03:00
parent 430a6c2436
commit 518a25a1fc
2 changed files with 107 additions and 3 deletions

88
src/api/promocode.ts Normal file

@ -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}`];
}
};

@ -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 (
<Formik initialValues={initialValues} onSubmit={createPromocode}>
<Formik initialValues={initialValues} onSubmit={submitForm}>
{({ values, handleChange, handleBlur, setFieldValue }) => (
<Form
style={{