From 8d455f08cc8c1ae4a1fff7e0fe999b42c35a25a0 Mon Sep 17 00:00:00 2001 From: IlyaDoronin Date: Thu, 2 May 2024 11:38:57 +0300 Subject: [PATCH] feat: getDiscounts request --- src/api/discounts.ts | 22 ++++++++++++++++ src/model/discounts.ts | 49 +++++++++++++++++++++++++++++++++++ src/pages/Tariffs/Tariffs.tsx | 34 ++++++++++++++++-------- src/ui_kit/CheckFastlink.tsx | 25 ++++++++++++------ 4 files changed, 111 insertions(+), 19 deletions(-) create mode 100644 src/api/discounts.ts create mode 100644 src/model/discounts.ts diff --git a/src/api/discounts.ts b/src/api/discounts.ts new file mode 100644 index 00000000..025021b1 --- /dev/null +++ b/src/api/discounts.ts @@ -0,0 +1,22 @@ +import { makeRequest } from "@frontend/kitui"; +import { parseAxiosError } from "@utils/parse-error"; + +import type { Discount } from "@model/discounts"; + +const API_URL = process.env.REACT_APP_DOMAIN + "/price/discount"; + +export async function getDiscounts( + userId: string, +): Promise<[Discount[] | null, string?]> { + try { + const { Discounts } = await makeRequest( + { method: "GET", url: `${API_URL}/user/${userId}` }, + ); + + return [Discounts]; + } catch (nativeError) { + const [error] = parseAxiosError(nativeError); + + return [null, `Не удалось получить скидки. ${error}`]; + } +} diff --git a/src/model/discounts.ts b/src/model/discounts.ts new file mode 100644 index 00000000..19479044 --- /dev/null +++ b/src/model/discounts.ts @@ -0,0 +1,49 @@ +type Audit = { + UpdatedAt: Date; + CreatedAt: Date; + Deleted: boolean; +}; + +type Condition = { + Period: Period; + User: string; + UserType: string; + Coupon: string; + PurchasesAmount: string; + CartPurchasesAmount: string; + Product: string; + Term: string; + Usage: string; + PriceFrom: string; + Group: string; +}; + +type Period = { + From: Date; + To: Date; +}; + +type Target = { + Products: Product[]; + Factor: number; + TargetScope: string; + TargetGroup: string; + Overhelm: boolean; +}; + +type Product = { + ID: string; + Factor: number; + Overhelm: boolean; +}; + +export type Discount = { + ID: string; + Name: string; + Layer: number; + Description: string; + Condition: Condition; + Target: Target; + Audit: Audit; + Deprecated: boolean; +}; diff --git a/src/pages/Tariffs/Tariffs.tsx b/src/pages/Tariffs/Tariffs.tsx index 650705f5..eba9f78a 100644 --- a/src/pages/Tariffs/Tariffs.tsx +++ b/src/pages/Tariffs/Tariffs.tsx @@ -32,6 +32,9 @@ import { createTariffElements } from "./tariffsUtils/createTariffElements"; import { currencyFormatter } from "./tariffsUtils/currencyFormatter"; import { useWallet, setCash } from "@root/cash"; import { handleLogoutClick } from "@utils/HandleLogoutClick"; +import { getDiscounts } from "@api/discounts"; + +import type { Discount } from "@model/discounts"; const StepperText: Record = { day: "Тарифы на время", @@ -48,7 +51,7 @@ function TariffPage() { const navigate = useNavigate(); const [tariffs, setTariffs] = useState([]); const [user, setUser] = useState(); - const [discounts, setDiscounts] = useState(); + const [discounts, setDiscounts] = useState([]); const [openModal, setOpenModal] = useState({}); const { cashString, cashCop, cashRub } = useWallet(); const [selectedItem, setSelectedItem] = useState<"count" | "day" | "dop">( @@ -90,13 +93,18 @@ function TariffPage() { url: process.env.REACT_APP_DOMAIN + "/customer/account", }); const tariffsList = await getTariffsList(); - const discounts = await makeRequest({ - method: "GET", - url: `${process.env.REACT_APP_DOMAIN}/price/discount/user/${userId}`, - }); + + if (userId) { + const [discounts] = await getDiscounts(userId); + + if (discounts?.length) { + setDiscounts(discounts); + } + } + setUser(user); setTariffs(tariffsList); - setDiscounts(discounts.Discounts); + let cs = currencyFormatter.format(Number(user.wallet.cash) / 100); let cc = Number(user.wallet.cash); let cr = Number(user.wallet.cash) / 100; @@ -178,11 +186,15 @@ function TariffPage() { .then(async (greetings) => { enqueueSnackbar(greetings); - const discounts = await makeRequest({ - method: "GET", - url: `${process.env.REACT_APP_DOMAIN}/price/discount/user/${userId}`, - }); - setDiscounts(discounts.Discounts); + if (!userId) { + return; + } + + const [discounts] = await getDiscounts(userId); + + if (discounts?.length) { + setDiscounts(discounts); + } }) .catch(enqueueSnackbar); } diff --git a/src/ui_kit/CheckFastlink.tsx b/src/ui_kit/CheckFastlink.tsx index e92b214a..1b38eee5 100644 --- a/src/ui_kit/CheckFastlink.tsx +++ b/src/ui_kit/CheckFastlink.tsx @@ -1,24 +1,33 @@ import { useEffect, useLayoutEffect, useState } from "react"; -import { useUserStore } from "@root/user"; import { Box, Button, Modal, Typography } from "@mui/material"; -import { mutate } from "swr"; import { enqueueSnackbar } from "notistack"; +import { mutate } from "swr"; + import makeRequest from "@api/makeRequest"; +import { getDiscounts } from "@api/discounts"; + +import { useUserStore } from "@root/user"; import { parseAxiosError } from "@utils/parse-error"; +import type { Discount } from "@model/discounts"; + export function CheckFastlink() { const userId = useUserStore((state) => state.userId); - const [discounts, setDiscounts] = useState(); + const [discounts, setDiscounts] = useState([]); const [askToChange, setAskToChange] = useState(false); const [promocode, setPromocode] = useState(""); useEffect(() => { const get = async () => { - const discounts = await makeRequest({ - method: "GET", - url: `${process.env.REACT_APP_DOMAIN}/price/discount/user/${userId}`, - }); - setDiscounts(discounts.Discounts); + if (!userId) { + return; + } + + const [discounts] = await getDiscounts(userId); + + if (discounts?.length) { + setDiscounts(discounts); + } }; get(); }, []);