From 99a3325b506a0b633b27c46ede8207461db2c992 Mon Sep 17 00:00:00 2001 From: Tamara Date: Tue, 14 Nov 2023 14:07:48 +0300 Subject: [PATCH 1/7] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20=D1=84=D0=BE=D1=80=D0=BC=D0=B8=D0=BA=20=D0=B2=20=D1=82?= =?UTF-8?q?=D0=B0=D1=80=D0=B8=D1=84=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/Authorization/signin.tsx | 2 +- .../Content/Tariffs/CreateTariff.tsx | 317 ++++++++++-------- 2 files changed, 174 insertions(+), 145 deletions(-) diff --git a/src/pages/Authorization/signin.tsx b/src/pages/Authorization/signin.tsx index 8c0fa26..107ff22 100644 --- a/src/pages/Authorization/signin.tsx +++ b/src/pages/Authorization/signin.tsx @@ -222,4 +222,4 @@ const SigninForm = () => { ); }; -export default SigninForm; +export default SigninForm; \ 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 2250207..467709c 100644 --- a/src/pages/dashboard/Content/Tariffs/CreateTariff.tsx +++ b/src/pages/dashboard/Content/Tariffs/CreateTariff.tsx @@ -9,6 +9,7 @@ import { InputLabel, useTheme, Box, + OutlinedInput } from "@mui/material"; import { enqueueSnackbar } from "notistack"; @@ -21,44 +22,62 @@ import { usePrivilegeStore, } from "@root/stores/privilegesStore"; import { currencyFormatter } from "@root/utils/currencyFormatter"; +import { Formik, Field, Form, FormikHelpers } from "formik"; + +interface Values { + nameField: string, + amountField: string, + customPriceField: string, + privilegeIdField: string, +} export default function CreateTariff() { const theme = useTheme(); const privileges = usePrivilegeStore((store) => store.privileges); - const [nameField, setNameField] = useState(""); - const [amountField, setAmountField] = useState(""); - const [customPriceField, setCustomPriceField] = useState(""); + // const [nameField, setNameField] = useState(""); + // const [amountField, setAmountField] = useState(""); + // const [customPriceField, setCustomPriceField] = useState(""); const [privilegeIdField, setPrivilegeIdField] = useState(""); const privilege = findPrivilegeById(privilegeIdField); - const checkFulledFields = () => { - if (nameField.length === 0) { - enqueueSnackbar("Пустое название тарифа"); - return false; - } - if (amountField.length === 0) { - enqueueSnackbar("Пустое кол-во едениц привилегии"); - return false; - } - if (privilegeIdField.length === 0) { - enqueueSnackbar("Не выбрана привилегия"); - return false; - } - if (!privilege) { - enqueueSnackbar("Привилегия с таким id не найдена"); - return false; - } - return true; - }; + // const checkFulledFields = (values: Values) => { + // if (values.nameField.length === 0) { + // enqueueSnackbar("Пустое название тарифа"); + // return false; + // } + // if (values.amountField.length === 0) { + // enqueueSnackbar("Пустое кол-во едениц привилегии"); + // return false; + // } + // if (privilegeIdField.length === 0) { + // enqueueSnackbar("Не выбрана привилегия"); + // return false; + // } + // if (!privilege) { + // enqueueSnackbar("Привилегия с таким id не найдена"); + // return false; + // } + // return true; + // }; - const createTariffBackend = async () => { - if (checkFulledFields() && privilege !== null) { + const initialValues: Values = { + nameField: "hello", + amountField: "54", + customPriceField: "", + privilegeIdField: "", + }; + + const createTariffBackend = async ( + values: Values, + formikHelpers: FormikHelpers + ) => { + if (privilege !== null) { const [_, createdTariffError] = await createTariff({ - name: nameField, - price: Number(customPriceField) * 100, + name: values.nameField, + price: Number(values.customPriceField) * 100, isCustom: false, privileges: [ { @@ -69,7 +88,7 @@ export default function CreateTariff() { type: privilege.type, value: privilege.value ?? "", price: privilege.price, - amount: Number(amountField), + amount: Number(values.amountField), }, ], }); @@ -80,9 +99,10 @@ export default function CreateTariff() { enqueueSnackbar("Тариф создан"); - requestTariffs(); + await requestTariffs(); } }; + // const createTariffFrontend = () => { // if (checkFulledFields() && privilege !== null) { // updateTariffStore({ @@ -97,122 +117,131 @@ export default function CreateTariff() { // } return ( - - - Создание тарифа - - - - Привилегия - - - - {privilege && ( - - - Имя: {privilege.name} - - - Сервис: {privilege.serviceKey} - - - Единица: {privilege.type} - - - Стандартная цена за единицу: {currencyFormatter.format(privilege.price / 100)} - - - )} - setNameField(e.target.value)} - /> - setAmountField(e.target.value)} - type="number" - /> - setCustomPriceField(e.target.value)} - type="number" - /> - - + {(props) => ( +
+ + + Создание тарифа + + + + Привилегия + + + + {privilege && ( + + + Имя: {privilege.name} + + + Сервис: {privilege.serviceKey} + + + Единица: {privilege.type} + + + Стандартная цена за единицу: {currencyFormatter.format(privilege.price / 100)} + + + )} + + + + + +
+ )} + + ); } From 5a7697c5aae3d6d018f50a4ddf4e6f295b99d102 Mon Sep 17 00:00:00 2001 From: Tamara Date: Tue, 14 Nov 2023 19:29:56 +0300 Subject: [PATCH 2/7] =?UTF-8?q?=D0=B2=D0=B0=D0=BB=D0=B8=D0=B4=D0=B0=D1=86?= =?UTF-8?q?=D0=B8=D1=8F=20=D0=B4=D0=BB=D1=8F=20=D1=82=D0=B0=D1=80=D0=B8?= =?UTF-8?q?=D1=84=D0=BE=D0=B2,=20=D1=83=D0=B4=D0=B0=D0=BB=D0=B5=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=BA=D0=BD=D0=BE=D0=BF=D0=BA=D0=B0=20=D0=BA=D0=B0?= =?UTF-8?q?=D0=BC=D0=B5=D1=80=D0=B0=20=D0=B8=20=D0=B4=D0=BE=D0=B1=D0=B0?= =?UTF-8?q?=D0=B2=D0=BB=D0=B5=D0=BD=D0=B0=20=D0=BD=D0=B0=20=D0=B3=D0=BB?= =?UTF-8?q?=D0=B0=D0=B2=D0=BD=D1=83=D1=8E=20=D0=BD=D0=B0=20=D1=81=D1=82?= =?UTF-8?q?=D1=80=D0=B0=D0=BD=D0=B8=D1=86=D1=83=20404?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/Error404/index.tsx | 10 ++- .../Content/Tariffs/CreateTariff.tsx | 69 ++++++++++++------- src/pages/dashboard/Menu/index.tsx | 1 - 3 files changed, 54 insertions(+), 26 deletions(-) diff --git a/src/pages/Error404/index.tsx b/src/pages/Error404/index.tsx index bb28b21..3709b15 100644 --- a/src/pages/Error404/index.tsx +++ b/src/pages/Error404/index.tsx @@ -1,8 +1,9 @@ import * as React from "react"; -import { Box, Typography } from "@mui/material"; +import {Box, Button, Typography} from "@mui/material"; import { ThemeProvider } from "@mui/material"; import theme from "../../theme"; import CssBaseline from '@mui/material/CssBaseline'; +import {Link} from "react-router-dom"; const Error404: React.FC = () => { @@ -46,6 +47,13 @@ const Error404: React.FC = () => { 4 + + + + + + diff --git a/src/pages/dashboard/Content/Tariffs/CreateTariff.tsx b/src/pages/dashboard/Content/Tariffs/CreateTariff.tsx index 467709c..2736884 100644 --- a/src/pages/dashboard/Content/Tariffs/CreateTariff.tsx +++ b/src/pages/dashboard/Content/Tariffs/CreateTariff.tsx @@ -9,10 +9,9 @@ import { InputLabel, useTheme, Box, - OutlinedInput } from "@mui/material"; import { enqueueSnackbar } from "notistack"; - +import OutlinedInput from "@kitUI/outlinedInput"; import { CustomTextField } from "@root/kitUI/CustomTextField"; import { requestTariffs } from "@root/services/tariffs.service"; import { createTariff } from "@root/api/tariffs"; @@ -43,29 +42,28 @@ export default function CreateTariff() { const privilege = findPrivilegeById(privilegeIdField); - // const checkFulledFields = (values: Values) => { - // if (values.nameField.length === 0) { - // enqueueSnackbar("Пустое название тарифа"); - // return false; - // } - // if (values.amountField.length === 0) { - // enqueueSnackbar("Пустое кол-во едениц привилегии"); - // return false; - // } - // if (privilegeIdField.length === 0) { - // enqueueSnackbar("Не выбрана привилегия"); - // return false; - // } - // if (!privilege) { - // enqueueSnackbar("Привилегия с таким id не найдена"); - // return false; - // } - // return true; - // }; + const checkFulledFields = (values: Values) => { + const errors = {nameField: "", amountField: "", privilegeIdField: ""} as any; + // values.nameField = "aa" + // values.amountField = "aa" + + if (values.nameField.length === 0) { + errors.nameField = "Пустое название тарифа" + } + if (values.amountField.length === 0) { + errors.amountField = "Пустое кол-во едениц привилегии" + } + if (privilegeIdField.length === 0) { + errors.privilegeIdField = "Не выбрана привилегия" + } + console.log(values.amountField) + return errors; + + }; const initialValues: Values = { - nameField: "hello", - amountField: "54", + nameField: "", + amountField: "", customPriceField: "", privilegeIdField: "", }; @@ -119,10 +117,11 @@ export default function CreateTariff() { return ( {(props) => ( -
+ @@ -164,6 +164,7 @@ export default function CreateTariff() { id="privilege-select" value={privilegeIdField} label="Привилегия" + error={props.touched.privilegeIdField && !!props.errors.privilegeIdField} onChange={(e) => setPrivilegeIdField(e.target.value)} sx={{ color: theme.palette.secondary.main, @@ -214,15 +215,29 @@ export default function CreateTariff() { as={OutlinedInput} id="tariff-name" name="nameField" + variant="filled" label="Название тарифа" type="text" + error={props.touched.nameField && !!props.errors.nameField} + helperText={ + + {props.errors.nameField} + + } /> + {props.errors.amountField} + + } /> Создать +
)} diff --git a/src/pages/dashboard/Menu/index.tsx b/src/pages/dashboard/Menu/index.tsx index e9c55db..aec86e4 100644 --- a/src/pages/dashboard/Menu/index.tsx +++ b/src/pages/dashboard/Menu/index.tsx @@ -106,7 +106,6 @@ const links: { path: string; element: JSX.Element; title: string; className: str { path: "/discounts", element: , title: "Скидки", className: "menu" }, { path: "/promocode", element: , title: "Промокод", className: "menu" }, { path: "/settingRoles", element: , title: "Настройки", className: "menu" }, - { path: "/jjj", element: , title: "Камера", className: "menu" }, { path: "/support", element: , title: "Служба поддержки", className: "menu" }, ]; From bc48cc0c3379f15b36001593885d063da892aaca Mon Sep 17 00:00:00 2001 From: Tamara Date: Tue, 14 Nov 2023 23:11:03 +0300 Subject: [PATCH 3/7] =?UTF-8?q?=D0=BE=D0=B3=D1=80=D0=B0=D0=BD=D0=B8=D1=87?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BD=D0=B0=20=D0=B2=D0=B2=D0=BE?= =?UTF-8?q?=D0=B4=20=D0=B1=D1=83=D0=BA=D0=B2=20=D0=BD=D0=B0=20=D1=87=D0=B8?= =?UTF-8?q?=D1=81=D0=BB=D0=BE=D0=B2=D0=BE=D0=BC=20=D0=B8=D0=BD=D0=BF=D1=83?= =?UTF-8?q?=D1=82=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Content/Tariffs/CreateTariff.tsx | 20 +++++++------------ 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/src/pages/dashboard/Content/Tariffs/CreateTariff.tsx b/src/pages/dashboard/Content/Tariffs/CreateTariff.tsx index 2736884..8001eae 100644 --- a/src/pages/dashboard/Content/Tariffs/CreateTariff.tsx +++ b/src/pages/dashboard/Content/Tariffs/CreateTariff.tsx @@ -45,7 +45,7 @@ export default function CreateTariff() { const checkFulledFields = (values: Values) => { const errors = {nameField: "", amountField: "", privilegeIdField: ""} as any; // values.nameField = "aa" - // values.amountField = "aa" + values.amountField = String(Number(values.amountField.replace(/[^0-9]/g,""))) if (values.nameField.length === 0) { errors.nameField = "Пустое название тарифа" @@ -225,20 +225,14 @@ export default function CreateTariff() { } /> - props.handleChange(e.target.value)} + value={props.values.amountField} label="Кол-во единиц привилегии" - error={props.touched.amountField && !!props.errors.amountField} - helperText={ - - {props.errors.amountField} - - } - /> + type="number" + /> Date: Thu, 16 Nov 2023 16:33:01 +0300 Subject: [PATCH 4/7] =?UTF-8?q?=D1=84=D0=BE=D1=80=D0=BC=D0=B8=D0=BA=20?= =?UTF-8?q?=D0=B4=D0=BB=D1=8F=20=D1=81=D0=BA=D0=B8=D0=B4=D0=BE=D0=BA(?= =?UTF-8?q?=D0=B2=20=D0=BF=D1=80=D0=BE=D1=86=D0=B5=D1=81=D1=81=D0=B5)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DiscountManagement/CreateDiscount.tsx | 496 ++++++++++-------- .../Content/Tariffs/CreateTariff.tsx | 6 - 2 files changed, 267 insertions(+), 235 deletions(-) diff --git a/src/pages/dashboard/Content/DiscountManagement/CreateDiscount.tsx b/src/pages/dashboard/Content/DiscountManagement/CreateDiscount.tsx index 4c78723..c20e856 100644 --- a/src/pages/dashboard/Content/DiscountManagement/CreateDiscount.tsx +++ b/src/pages/dashboard/Content/DiscountManagement/CreateDiscount.tsx @@ -24,6 +24,19 @@ import { enqueueSnackbar } from "notistack"; import { DiscountType, discountTypes } from "@root/model/discount"; import { createDiscount } from "@root/api/discounts"; import usePrivileges from "@root/utils/hooks/usePrivileges"; +import { Formik, Field, Form, FormikHelpers } from "formik"; + +interface Values { + discountNameField: string, + discountDescriptionField: string, + discountFactorField: string, + serviceType: string, + discountType: DiscountType, + purchasesAmountField: string, + cartPurchasesAmountField: string, + discountMinValueField: string, + privilegeIdField: string, +} export default function CreateDiscount() { const theme = useTheme(); @@ -54,15 +67,30 @@ export default function CreateDiscount() { setServiceType(event.target.value as ServiceType); }; - async function handleCreateDiscount() { - const purchasesAmount = parseFloat(purchasesAmountField.replace(",", ".")); + const initialValues: Values = { + discountNameField: "", + discountDescriptionField: "", + discountFactorField: "", + serviceType: "", + discountType: "purchasesAmount", + purchasesAmountField: "", + cartPurchasesAmountField: "", + discountMinValueField: "", + privilegeIdField: "", + } + + async function handleCreateDiscount( + values: Values, + formikHelpers: FormikHelpers + ) { //валидация? + const purchasesAmount = parseFloat(values.purchasesAmountField.replace(",", ".")); const discountFactor = - (100 - parseFloat(discountFactorField.replace(",", "."))) / 100; + (100 - parseFloat(values.discountFactorField.replace(",", "."))) / 100; const cartPurchasesAmount = parseFloat( - cartPurchasesAmountField.replace(",", ".") + values.cartPurchasesAmountField.replace(",", ".") ); const discountMinValue = parseFloat( - discountMinValueField.replace(",", ".") + values.discountMinValueField.replace(",", ".") ); if (!isFinite(purchasesAmount)) @@ -87,13 +115,13 @@ export default function CreateDiscount() { discountFactor, discountMinValue, purchasesAmount, - discountDescription: discountDescriptionField, - discountName: discountNameField, + discountDescription: values.discountDescriptionField, + discountName: values.discountNameField, startDate: new Date().toISOString(), endDate: new Date(Date.now() + 1000 * 3600 * 24 * 30).toISOString(), - serviceType, - discountType, - privilegeId: privilegeIdField, + serviceType: values.serviceType, + discountType: values.discountType, + privilegeId: values.privilegeIdField, }); if (createdDiscountError) { @@ -108,225 +136,235 @@ export default function CreateDiscount() { } return ( - - setDiscountNameField(e.target.value)} - /> - setDiscountDescriptionField(e.target.value)} - /> - - Условия: - - setDiscountFactorField(e.target.value)} - /> - - - Тип скидки - - - {Object.keys(discountTypes).map((type) => ( - } - label={discountTypes[type as DiscountType]} - /> - ))} - - - {discountType === "purchasesAmount" && ( - setPurchasesAmountField(e.target.value)} - /> - )} - {discountType === "cartPurchasesAmount" && ( - setCartPurchasesAmountField(e.target.value)} - /> - )} - {discountType === "service" && ( - <> - - setDiscountMinValueField(e.target.value)} - /> - - )} - {discountType === "privilege" && ( - <> - - - Привилегия - - - - setDiscountMinValueField(e.target.value)} - /> - - )} - - - - + {(props) => ( +
+ + + + + Условия: + + + + + Тип скидки + + + {Object.keys(discountTypes).map((type) => ( + } + label={discountTypes[type as DiscountType]} + /> + ))} + + + {discountType === "purchasesAmount" && ( + + )} + {discountType === "cartPurchasesAmount" && ( + + )} + {discountType === "service" && ( + <> + + {SERVICE_LIST.map((service) => ( + + {service.displayName} + + ))} + + + + )} + {discountType === "privilege" && ( + <> + + + Привилегия + + + {privileges.map((privilege, index) => ( + + {privilege.description} + + ))} + + + + + )} + + + +
+ + )} +
+ ); } diff --git a/src/pages/dashboard/Content/Tariffs/CreateTariff.tsx b/src/pages/dashboard/Content/Tariffs/CreateTariff.tsx index 8001eae..b1d2b7f 100644 --- a/src/pages/dashboard/Content/Tariffs/CreateTariff.tsx +++ b/src/pages/dashboard/Content/Tariffs/CreateTariff.tsx @@ -247,12 +247,6 @@ export default function CreateTariff() { > Создать - )} From 2503b7e499c41341e59265e691b2b49e0f6b52a1 Mon Sep 17 00:00:00 2001 From: Tamara Date: Tue, 28 Nov 2023 19:12:12 +0300 Subject: [PATCH 5/7] --amend --- src/kitUI/CustomTextField.tsx | 9 +- .../DiscountManagement/CreateDiscount.tsx | 587 ++++++++++-------- .../Content/Tariffs/CreateTariff.tsx | 30 +- 3 files changed, 356 insertions(+), 270 deletions(-) diff --git a/src/kitUI/CustomTextField.tsx b/src/kitUI/CustomTextField.tsx index 1d632fc..e373820 100644 --- a/src/kitUI/CustomTextField.tsx +++ b/src/kitUI/CustomTextField.tsx @@ -1,11 +1,15 @@ import { SxProps, TextField, Theme, useTheme } from "@mui/material"; import { HTMLInputTypeAttribute, ChangeEvent } from "react"; +import {InputBaseProps} from "@mui/material/InputBase"; -export function CustomTextField({ id, label, value, type, sx, onChange: setValue }: { +export function CustomTextField({ id, label, value, name, onBlur,error, type, sx, onChange: setValue }: { id: string; label: string; value: number | string | null; + name?: string; + onBlur?: InputBaseProps['onBlur']; + error?: boolean; type?: HTMLInputTypeAttribute; sx?: SxProps; onChange: (e: ChangeEvent) => void; @@ -18,6 +22,9 @@ export function CustomTextField({ id, label, value, type, sx, onChange: setValue id={id} label={label} variant="filled" + name={name} + onBlur={onBlur} + error={error} color="secondary" type={type} sx={sx} diff --git a/src/pages/dashboard/Content/DiscountManagement/CreateDiscount.tsx b/src/pages/dashboard/Content/DiscountManagement/CreateDiscount.tsx index c20e856..68689a3 100644 --- a/src/pages/dashboard/Content/DiscountManagement/CreateDiscount.tsx +++ b/src/pages/dashboard/Content/DiscountManagement/CreateDiscount.tsx @@ -1,14 +1,14 @@ import { - Box, - Typography, - Button, - useTheme, - FormControl, - FormLabel, - RadioGroup, - FormControlLabel, - Radio, - InputLabel, + Box, + Typography, + Button, + useTheme, + FormControl, + FormLabel, + RadioGroup, + FormControlLabel, + Radio, + InputLabel, TextField, } from "@mui/material"; import MenuItem from "@mui/material/MenuItem"; import Select, { SelectChangeEvent } from "@mui/material/Select"; @@ -25,6 +25,7 @@ import { DiscountType, discountTypes } from "@root/model/discount"; import { createDiscount } from "@root/api/discounts"; import usePrivileges from "@root/utils/hooks/usePrivileges"; import { Formik, Field, Form, FormikHelpers } from "formik"; +import OutlinedInput from "@kitUI/outlinedInput"; interface Values { discountNameField: string, @@ -79,10 +80,11 @@ export default function CreateDiscount() { privilegeIdField: "", } - async function handleCreateDiscount( + const handleCreateDiscount = async( values: Values, formikHelpers: FormikHelpers - ) { //валидация? + ) => { + console.log("работаю") const purchasesAmount = parseFloat(values.purchasesAmountField.replace(",", ".")); const discountFactor = (100 - parseFloat(values.discountFactorField.replace(",", "."))) / 100; @@ -93,22 +95,6 @@ export default function CreateDiscount() { values.discountMinValueField.replace(",", ".") ); - if (!isFinite(purchasesAmount)) - return enqueueSnackbar("Поле purchasesAmount не число"); - if (!isFinite(discountFactor)) - return enqueueSnackbar("Поле discountFactor не число"); - if (!isFinite(cartPurchasesAmount)) - return enqueueSnackbar("Поле cartPurchasesAmount не число"); - if (!isFinite(discountMinValue)) - return enqueueSnackbar("Поле discountMinValue не число"); - if (discountType === "privilege" && !privilegeIdField) - return enqueueSnackbar("Привилегия не выбрана"); - if (!discountNameField) return enqueueSnackbar('Поле "Имя" пустое'); - if (!discountDescriptionField) - return enqueueSnackbar('Поле "Описание" пустое'); - if (discountFactor < 0) - return enqueueSnackbar("Процент скидки не может быть больше 100"); - const [createdDiscountResponse, createdDiscountError] = await createDiscount({ cartPurchasesAmount, @@ -135,236 +121,323 @@ export default function CreateDiscount() { } } - return ( - { + const errors = {} as any; + if (values.discountNameField.length === 0) { + errors.discountNameField = 'Поле "Имя" пустое' + } + if (values.discountDescriptionField.length === 0) { + errors.discountDescriptionField = 'Поле "Описание" пустое' + } + if (((100 - parseFloat(values.discountFactorField.replace(",", "."))) / 100) < 0) { + errors.discountFactorField = "Процент скидки не может быть больше 100" + } + if (!isFinite(((100 - parseFloat(values.discountFactorField.replace(",", "."))) / 100))) { + errors.discountFactorField = 'Поле "Процент скидки" не число' + } + if (values.discountType === "privilege" && !values.privilegeIdField) + errors.privilegeIdField = "Привилегия не выбрана" + if (values.discountType === "purchasesAmount" && !isFinite(parseFloat(values.purchasesAmountField.replace(",", ".")))) { + errors.purchasesAmountField = 'Поле "Внесено больше" не число' + } + if (values.discountType === "cartPurchasesAmount" && !isFinite(parseFloat(values.cartPurchasesAmountField.replace(",", ".")))) { + errors.cartPurchasesAmountField = 'Поле "Объём в корзине" не число' + } + if (values.discountType === ("service" || "privilege") && !isFinite(parseFloat(values.discountMinValueField.replace(",", ".")))) { + errors.discountMinValueField = 'Поле "Минимальное значение" не число' + } + console.log(errors) + return errors; + } - > - {(props) => ( -
- - - - - Условия: - - - - - Тип скидки - - + {(props) => ( + + + + {props.errors.discountNameField} + + } + /> + + {props.errors.discountDescriptionField} + + } + InputProps={{ + style: { + backgroundColor: theme.palette.content.main, + color: theme.palette.secondary.main, + } + }} + InputLabelProps={{ + style: { + color: theme.palette.secondary.main + } + }} + /> + + Условия: + + { + props.setFieldValue("discountFactorField", e.target.value.replace(/[^\d]/g, '')) + }} + onBlur={props.handleBlur} + /> + + + Тип скидки + + + ) => { + props.setFieldValue("discountType", event.target.value as DiscountType); + }} + onBlur={props.handleBlur} + > + {Object.keys(discountTypes).map((type) => ( + } + label={discountTypes[type as DiscountType]} + /> + ))} + + + {props.values.discountType === "purchasesAmount" && ( + { + props.setFieldValue("purchasesAmountField", e.target.value.replace(/[^\d]/g, '')) + }} + value={props.values.purchasesAmountField} + onBlur={props.handleBlur} + sx={{ + marginTop: "15px", + }} + /> + )} + {props.values.discountType === "cartPurchasesAmount" && ( + { + props.setFieldValue("cartPurchasesAmountField", e.target.value.replace(/[^\d]/g, '')) + }} + value={props.values.cartPurchasesAmountField} + onBlur={props.handleBlur} + sx={{ + marginTop: "15px", + }} + /> + )} + {props.values.discountType === "service" && ( + <> + + { + props.setFieldValue("discountMinValueField", e.target.value.replace(/[^\d]/g, '')) + }} + value={props.values.discountMinValueField} + error={props.touched.discountMinValueField && !!props.errors.discountMinValueField} + sx={{ + marginTop: "15px", + }} + /> + + )} + {props.values.discountType === "privilege" && ( + <> + + + Привилегия + + + + { + props.setFieldValue("discountMinValueField", e.target.value.replace(/[^\d]/g, '')) + }} + error={props.touched.discountMinValueField && !!props.errors.discountMinValueField} + value={props.values.discountMinValueField} + sx={{ + marginTop: "15px", + }} + /> + + )} + + + + + + )} +
- onChange={handleDiscountTypeChange} - > - {Object.keys(discountTypes).map((type) => ( - } - label={discountTypes[type as DiscountType]} - /> - ))} - - - {discountType === "purchasesAmount" && ( - - )} - {discountType === "cartPurchasesAmount" && ( - - )} - {discountType === "service" && ( - <> - - {SERVICE_LIST.map((service) => ( - - {service.displayName} - - ))} - - - - )} - {discountType === "privilege" && ( - <> - - - Привилегия - -
- {privileges.map((privilege, index) => ( - - {privilege.description} - - ))} -
-
- - - )} - - - - - - )} - - - ); + ); } diff --git a/src/pages/dashboard/Content/Tariffs/CreateTariff.tsx b/src/pages/dashboard/Content/Tariffs/CreateTariff.tsx index b1d2b7f..703bc77 100644 --- a/src/pages/dashboard/Content/Tariffs/CreateTariff.tsx +++ b/src/pages/dashboard/Content/Tariffs/CreateTariff.tsx @@ -43,9 +43,7 @@ export default function CreateTariff() { const privilege = findPrivilegeById(privilegeIdField); const checkFulledFields = (values: Values) => { - const errors = {nameField: "", amountField: "", privilegeIdField: ""} as any; - // values.nameField = "aa" - values.amountField = String(Number(values.amountField.replace(/[^0-9]/g,""))) + const errors = {} as any; if (values.nameField.length === 0) { errors.nameField = "Пустое название тарифа" @@ -53,10 +51,10 @@ export default function CreateTariff() { if (values.amountField.length === 0) { errors.amountField = "Пустое кол-во едениц привилегии" } - if (privilegeIdField.length === 0) { + if (values.privilegeIdField.length === 0) { errors.privilegeIdField = "Не выбрана привилегия" } - console.log(values.amountField) + console.log(errors) return errors; }; @@ -162,10 +160,13 @@ export default function CreateTariff() { - { props.setFieldValue("discountMinValueField", e.target.value.replace(/[^\d]/g, '')) }} @@ -337,6 +377,22 @@ export default function CreateDiscount() { sx={{ marginTop: "15px", }} + helperText={ + + {props.errors.discountMinValueField} + + } + InputProps={{ + style: { + backgroundColor: theme.palette.content.main, + color: theme.palette.secondary.main, + } + }} + InputLabelProps={{ + style: { + color: theme.palette.secondary.main + } + }} /> )} @@ -393,18 +449,36 @@ export default function CreateDiscount() { ))} - { props.setFieldValue("discountMinValueField", e.target.value.replace(/[^\d]/g, '')) }} - error={props.touched.discountMinValueField && !!props.errors.discountMinValueField} value={props.values.discountMinValueField} + error={props.touched.discountMinValueField && !!props.errors.discountMinValueField} sx={{ marginTop: "15px", }} + helperText={ + + {props.errors.discountMinValueField} + + } + InputProps={{ + style: { + backgroundColor: theme.palette.content.main, + color: theme.palette.secondary.main, + } + }} + InputLabelProps={{ + style: { + color: theme.palette.secondary.main + } + }} /> )} diff --git a/src/pages/dashboard/Content/Tariffs/CreateTariff.tsx b/src/pages/dashboard/Content/Tariffs/CreateTariff.tsx index 703bc77..5f16b66 100644 --- a/src/pages/dashboard/Content/Tariffs/CreateTariff.tsx +++ b/src/pages/dashboard/Content/Tariffs/CreateTariff.tsx @@ -1,4 +1,3 @@ -import { useState } from "react"; import { Typography, Container, @@ -8,11 +7,9 @@ import { FormControl, InputLabel, useTheme, - Box, + Box, TextField, } from "@mui/material"; import { enqueueSnackbar } from "notistack"; -import OutlinedInput from "@kitUI/outlinedInput"; -import { CustomTextField } from "@root/kitUI/CustomTextField"; import { requestTariffs } from "@root/services/tariffs.service"; import { createTariff } from "@root/api/tariffs"; @@ -20,6 +17,7 @@ import { findPrivilegeById, usePrivilegeStore, } from "@root/stores/privilegesStore"; +import { PrivilegeWithAmount } from "@frontend/kitui"; import { currencyFormatter } from "@root/utils/currencyFormatter"; import { Formik, Field, Form, FormikHelpers } from "formik"; @@ -28,6 +26,7 @@ interface Values { amountField: string, customPriceField: string, privilegeIdField: string, + privilege: PrivilegeWithAmount | null } export default function CreateTariff() { @@ -35,13 +34,6 @@ export default function CreateTariff() { const privileges = usePrivilegeStore((store) => store.privileges); - // const [nameField, setNameField] = useState(""); - // const [amountField, setAmountField] = useState(""); - // const [customPriceField, setCustomPriceField] = useState(""); - const [privilegeIdField, setPrivilegeIdField] = useState(""); - - const privilege = findPrivilegeById(privilegeIdField); - const checkFulledFields = (values: Values) => { const errors = {} as any; @@ -64,26 +56,27 @@ export default function CreateTariff() { amountField: "", customPriceField: "", privilegeIdField: "", + privilege: null }; const createTariffBackend = async ( values: Values, formikHelpers: FormikHelpers ) => { - if (privilege !== null) { + if (values.privilege !== null) { const [_, createdTariffError] = await createTariff({ name: values.nameField, price: Number(values.customPriceField) * 100, isCustom: false, privileges: [ { - name: privilege.name, - privilegeId: privilege.privilegeId ?? "", - serviceKey: privilege.serviceKey, - description: privilege.description, - type: privilege.type, - value: privilege.value ?? "", - price: privilege.price, + name: values.privilege.name, + privilegeId: values.privilege.privilegeId ?? "", + serviceKey: values.privilege.serviceKey, + description: values.privilege.description, + type: values.privilege.type, + value: values.privilege.value ?? "", + price: values.privilege.price, amount: Number(values.amountField), }, ], @@ -165,6 +158,9 @@ export default function CreateTariff() { error={props.touched.privilegeIdField && !!props.errors.privilegeIdField} onChange={(e) => { props.setFieldValue("privilegeIdField", e.target.value) + props.setFieldValue("privilege", findPrivilegeById(e.target.value)) + if (props.values.privilege === null) + return enqueueSnackbar("Привилегия не найдена"); }} onBlur={props.handleBlur} sx={{ @@ -191,7 +187,7 @@ export default function CreateTariff() { ))} - {privilege && ( + {props.values.privilege && ( - Имя: {privilege.name} + Имя: {props.values.privilege.name} - Сервис: {privilege.serviceKey} + Сервис: {props.values.privilege.serviceKey} - Единица: {privilege.type} + Единица: {props.values.privilege.type} - Стандартная цена за единицу: {currencyFormatter.format(privilege.price / 100)} + Стандартная цена за единицу: {currencyFormatter.format(props.values.privilege.price / 100)} )} } + InputProps={{ + style: { + backgroundColor: theme.palette.content.main, + color: theme.palette.secondary.main, + } + }} + InputLabelProps={{ + style: { + color: theme.palette.secondary.main + } + }} /> - { props.setFieldValue("amountField", e.target.value.replace(/[^\d]/g,'')) }} value={props.values.amountField} onBlur={props.handleBlur} label="Кол-во единиц привилегии" + error={props.touched.amountField && !!props.errors.amountField} + helperText={ + + {props.errors.amountField} + + } + InputProps={{ + style: { + backgroundColor: theme.palette.content.main, + color: theme.palette.secondary.main, + } + }} + InputLabelProps={{ + style: { + color: theme.palette.secondary.main + } + }} /> - { props.setFieldValue("customPriceField", e.target.value.replace(/[^\d]/g,'')) }} value={props.values.customPriceField} onBlur={props.handleBlur} label="Кастомная цена (не обязательно)" + InputProps={{ + style: { + backgroundColor: theme.palette.content.main, + color: theme.palette.secondary.main, + } + }} + InputLabelProps={{ + style: { + color: theme.palette.secondary.main + } + }} />