From ef4000ea079808dd0f95b01b88ec868aebfdd0c1 Mon Sep 17 00:00:00 2001 From: Nastya Date: Thu, 15 Jun 2023 23:29:27 +0300 Subject: [PATCH] =?UTF-8?q?=D0=95=D0=B4=D0=B8=D0=BD=D0=B0=D1=8F=20=D1=84?= =?UTF-8?q?=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D1=8F=20=D0=B7=D0=B0=D0=BF=D1=80?= =?UTF-8?q?=D0=BE=D1=81=D0=B0=20=D1=82=D0=B0=D1=80=D0=B8=D1=84=D0=BE=D0=B2?= =?UTF-8?q?=20=D0=B2=20=D0=BA=D0=BE=D1=80=D0=BD=D0=B5=20=D1=81=D1=82=D1=80?= =?UTF-8?q?=D0=B0=D0=BD=D0=B8=D1=86=D1=8B=20=D1=82=D0=B0=D1=80=D0=B8=D1=84?= =?UTF-8?q?=D0=BE=D0=B2.=20=D0=91=D0=B5=D0=BA=D0=A2=D0=B0=D1=80=D0=B8?= =?UTF-8?q?=D1=84=D1=8B=20=D0=BF=D1=80=D0=B8=D0=B2=D0=B5=D0=B4=D0=B5=D0=BD?= =?UTF-8?q?=D1=8B=20=D0=BA=20=D1=84=D1=80=D0=BE=D0=BD=D1=82=D0=A2=D0=B0?= =?UTF-8?q?=D1=80=D0=B8=D1=84=D0=B0=D0=BC.=204=20=D1=84=D1=83=D0=BD=D0=BA?= =?UTF-8?q?=D1=86=D0=B8=D0=B8=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D1=8B=20?= =?UTF-8?q?=D1=81=D0=BE=20=D1=81=D1=82=D0=BE=D1=80=D0=BE=D0=BC=20=D1=82?= =?UTF-8?q?=D0=B0=D1=80=D0=B8=D1=84=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/kitUI/Cart/Cart.tsx | 33 ---- src/kitUI/Cart/calc.ts | 4 +- src/model/tariff.ts | 44 ++++-- .../Content/Tariffs/CreateTariff.tsx | 146 +++++++++--------- .../dashboard/Content/Tariffs/EditModal.tsx | 6 +- src/pages/dashboard/Content/Tariffs/index.tsx | 52 ++++++- .../dashboard/Content/Tariffs/tariffsDG.tsx | 115 ++------------ src/pages/dashboard/Content/Tariffs/types.ts | 23 --- src/stores/tariffs.ts | 30 +++- 9 files changed, 198 insertions(+), 255 deletions(-) delete mode 100644 src/pages/dashboard/Content/Tariffs/types.ts diff --git a/src/kitUI/Cart/Cart.tsx b/src/kitUI/Cart/Cart.tsx index d8745f2..cd2913b 100644 --- a/src/kitUI/Cart/Cart.tsx +++ b/src/kitUI/Cart/Cart.tsx @@ -24,9 +24,6 @@ import { calcCartData, createCartItem, findDiscountFactor, formatDiscountFactor import { useTariffStore } from "@root/stores/tariffs"; import { AnyDiscount, CartItemTotal } from "@root/model/cart"; import { findPrivilegeById } from "@root/stores/privileges"; -import { Privilege } from "@root/model/tariff"; -import { enqueueSnackbar } from "notistack"; -import axios from "axios"; interface Props { selectedTariffs: GridSelectionModel; @@ -64,36 +61,6 @@ export default function Cart({ selectedTariffs }: Props) { console.log(cartTotal, "cartTotal"); - const getTariffs = () => { - axios({ - method: "get", - url: "https://admin.pena.digital/strator/tariff", - }) - .then((data: any) => { - setTariffs(data.data.tariffs); - - // data.data.tariffs.forEach(async (t:any) => { - // if (t._id) { - // console.log(t._id) - // await axios({ - // method: "delete", - // url: "https://admin.pena.digital/strator/tariff/delete", - // headers: { - // Authorization: `Bearer ${token}`, - // }, - // data: { id: t._id }, - // }); - // } - - // }) - }) - .catch((error) => { - enqueueSnackbar("Ошибка получения тарифов"); - }); - }; - useEffect(() => { - getTariffs(); - }, []); const cartRows = cartTotal?.items.map((cartItemTotal) => { const privilege = findPrivilegeById(cartItemTotal.tariff.privilegeId); diff --git a/src/kitUI/Cart/calc.ts b/src/kitUI/Cart/calc.ts index 792f9cc..b14edcc 100644 --- a/src/kitUI/Cart/calc.ts +++ b/src/kitUI/Cart/calc.ts @@ -10,7 +10,7 @@ import { ServiceDiscount, UserDiscount, } from "@root/model/cart"; -import { ServiceType, SERVICE_LIST, Tariff } from "../../model/tariff"; +import { Tariff_BACKEND } from "@root/model/tariff"; import { User } from "../../model/user"; import { findPrivilegeById } from "@root/stores/privileges"; @@ -38,6 +38,8 @@ export function calcCartData({ }; cartItems.forEach((cartItem) => { + + console.log(cartItem) console.log(cartItem.tariff.privilegeId) const privilege = findPrivilegeById(cartItem.tariff.privilegeId); console.log(privilege) diff --git a/src/model/tariff.ts b/src/model/tariff.ts index a7e47dd..b77640d 100644 --- a/src/model/tariff.ts +++ b/src/model/tariff.ts @@ -17,24 +17,34 @@ export type ServiceType = (typeof SERVICE_LIST)[number]["serviceKey"]; export type PrivilegeType = "unlim" | "gencount" | "activequiz" | "abcount" | "extended"; -export interface Privilege { - serviceKey: ServiceType; - name: PrivilegeType; - privilegeId: string; - description: string; - /** Единица измерения привелегии: время в днях/кол-во */ - type: "day" | "count"; - /** Стоимость одной единицы привелегии */ - price: number; -} - -export interface Tariff { - id: string; +export interface Privilege_BACKEND { name: string; privilegeId: string; - /** Количество единиц привелегии */ + serviceKey: string; amount: number; - /** Кастомная цена, если есть, то используется вместо privilege.price */ - customPricePerUnit?: number; - isFront?: boolean + description: string; + price: number; + type: string; + value: string; + updatedAt: string; + _id: string; } +export type Tariff_BACKEND = { + _id: string, + name: string, + price: number, + isCustom: boolean, + isFront: boolean, + privilegies: Privilege_BACKEND[], + isDeleted: boolean, + createdAt: string, + updatedAt: string +} +export type Tariff_FRONTEND = { + id: string, + name: string, + amount:number, + isFront: boolean, + privilegeId: string, + customPricePerUnit: number +} \ No newline at end of file diff --git a/src/pages/dashboard/Content/Tariffs/CreateTariff.tsx b/src/pages/dashboard/Content/Tariffs/CreateTariff.tsx index 83e43a7..026954b 100644 --- a/src/pages/dashboard/Content/Tariffs/CreateTariff.tsx +++ b/src/pages/dashboard/Content/Tariffs/CreateTariff.tsx @@ -6,11 +6,11 @@ import axios from "axios"; import { CustomTextField } from "@root/kitUI/CustomTextField"; -import { Tariff } from "@root/model/tariff"; import { addTariffs } from "@root/stores/tariffs"; import { authStore } from "@root/stores/auth"; import { mergedPrivilegeStore } from "@root/stores/mergedPrivileges"; +import { Tariff_BACKEND } from "@root/model/tariff"; export default function CreateTariff() { const theme = useTheme(); @@ -29,77 +29,77 @@ export default function CreateTariff() { console.log(privilege); - function handleCreateTariffClick() { - if (nameField === "") { - enqueueSnackbar("Пустое название тарифа"); - } - if (amountField === "") { - enqueueSnackbar("Пустое кол-во едениц привилегия"); - } - if (privilegeIdField === "") { - enqueueSnackbar("Не выбрана привилегия"); - } +// function handleCreateTariffClick() { +// if (nameField === "") { +// enqueueSnackbar("Пустое название тарифа"); +// } +// if (amountField === "") { +// enqueueSnackbar("Пустое кол-во едениц привилегия"); +// } +// if (privilegeIdField === "") { +// enqueueSnackbar("Не выбрана привилегия"); +// } - const amount = Number(amountField); - const customPrice = Number(customPriceField); +// const amount = Number(amountField); +// const customPrice = Number(customPriceField); - if (isNaN(amount) || !privilege) return; +// if (isNaN(amount) || !privilege) return; - const newTariff: Tariff = { - id: nanoid(5), - name: nameField, - amount:amount, -isFront: true, - privilegeId: privilege.privilegeId, - customPricePerUnit: customPrice ? customPrice / amount : undefined, - }; - addTariffs([newTariff]); +// const newTariff: Tariff = { +// id: nanoid(5), +// name: nameField, +// amount:amount, +// isFront: true, +// privilegeId: privilege.privilegeId, +// customPricePerUnit: customPrice ? customPrice / amount : undefined, +// }; +// addTariffs([newTariff]); - } +// } - const createTariff = async () => { - if (nameField === "" || amountField === "" || privilegeIdField === "") { - return; - } +// const createTariff = async () => { +// if (nameField === "" || amountField === "" || privilegeIdField === "") { +// return; +// } - try { - if (!privilege) { - throw new Error("Привилегия не выбрана"); - } +// try { +// if (!privilege) { +// throw new Error("Привилегия не выбрана"); +// } - if (!privilege._id) { - return; - } +// if (!privilege._id) { +// return; +// } - const { data } = await axios({ - url: "https://admin.pena.digital/strator/tariff/", - method: "post", - headers: { - Authorization: `Bearer ${token}`, - }, - data: { - name: nameField, - price: Number(customPriceField) * 100, - isCustom: false, - privilegies: [ - { - name: privilege.name, - privilegeId: privilege._id, - serviceKey: privilege.serviceKey, - description: privilege.description, - type: privilege.type, - value: privilege.value, - price: privilege.price, - amount: Number(amountField), - }, - ], - }, - }); - } catch (error) { - enqueueSnackbar((error as Error).message); - } - }; +// const { data } = await axios({ +// url: "https://admin.pena.digital/strator/tariff/", +// method: "post", +// headers: { +// Authorization: `Bearer ${token}`, +// }, +// data: { +// name: nameField, +// price: Number(customPriceField) * 100, +// isCustom: false, +// privilegies: [ +// { +// name: privilege.name, +// privilegeId: privilege._id, +// serviceKey: privilege.serviceKey, +// description: privilege.description, +// type: privilege.type, +// value: privilege.value, +// price: privilege.price, +// amount: Number(amountField), +// }, +// ], +// }, +// }); +// } catch (error) { +// enqueueSnackbar((error as Error).message); +// } +// }; return (