diff --git a/src/kitUI/DeleteModal.tsx b/src/kitUI/DeleteModal.tsx new file mode 100644 index 0000000..31d13d3 --- /dev/null +++ b/src/kitUI/DeleteModal.tsx @@ -0,0 +1,69 @@ +import * as React from "react"; +import Box from "@mui/material/Box"; +import Button from "@mui/material/Button"; +import Typography from "@mui/material/Typography"; +import Modal from "@mui/material/Modal"; + +type DeleteModalProps = { + open: boolean; + handleClose: () => void; + + tariffDelete: (tarifIid: string) => Promise; + errorDelete: boolean; + tariffId: string; + tariffName: string; +}; + +export default function DeleteModal({ + open, + handleClose, + tariffDelete, + errorDelete, + tariffId, + tariffName, +}: DeleteModalProps) { + const onClickTariffDelete = () => { + if (errorDelete) { + return; + } + tariffDelete(tariffId); + handleClose(); + }; + return ( +
+ + + + Вы уверены, что хотите удалить тариф + + + + Тариф: {tariffName} + + + +
+ ); +} diff --git a/src/kitUI/EditModal.tsx b/src/kitUI/EditModal.tsx new file mode 100644 index 0000000..16b6ea3 --- /dev/null +++ b/src/kitUI/EditModal.tsx @@ -0,0 +1,99 @@ +import React, { useState } from "react"; +import Box from "@mui/material/Box"; +import Button from "@mui/material/Button"; +import Typography from "@mui/material/Typography"; +import Modal from "@mui/material/Modal"; +import TextField from "@mui/material/TextField"; + +type EditModalProps = { + open: boolean; + handleClose: () => void; + errorEdit: boolean; + tariffEdit: (tarifIid: string, tariffName: string, tariffPrice: string) => Promise; + tariff: + | { + id: string; + name: string; + serviceName: "Шаблонизатор документов" | "Опросник" | "Аналитика сокращателя" | undefined; + privilege: string; + amount: number; + type: string; + pricePerUnit: number; + isCustomPrice: string; + isDeleted: boolean; + total: number; + } + | undefined; +}; + +export default function EditModal({ open, handleClose, errorEdit, tariffEdit, tariff }: EditModalProps) { + const [editOpen, setEditOpen] = useState(false); + const [name, setName] = useState(""); + const [price, setPrice] = useState(""); + + const updateTariffDetails = () => { + if (!tariff || errorEdit) { + return; + } + + tariffEdit(tariff.id, name, price); + + handleClose(); + }; + return ( +
+ + + + Вы уверены, что хотите изменить тариф + + + {editOpen && tariff && ( + + Название Тарифа: {tariff.name} + setName(event.target.value)} + label="Имя" + name="name" + value={name} + sx={{ marginBottom: "10px" }} + /> + Цена за единицу: {tariff.pricePerUnit} + setPrice(event.target.value)} + label="Цена за единицу" + name="price" + value={price} + sx={{ marginBottom: "10px" }} + /> + + + )} + + +
+ ); +} diff --git a/src/pages/dashboard/Content/DiscountManagement/DiscountDataGrid.tsx b/src/pages/dashboard/Content/DiscountManagement/DiscountDataGrid.tsx index a5f7394..8806cdc 100644 --- a/src/pages/dashboard/Content/DiscountManagement/DiscountDataGrid.tsx +++ b/src/pages/dashboard/Content/DiscountManagement/DiscountDataGrid.tsx @@ -1,165 +1,197 @@ import { Box, Button, styled, useTheme } from "@mui/material"; import { DataGrid, GridColDef, GridRowsProp, GridToolbar } from "@mui/x-data-grid"; import { findDiscountFactor, formatDiscountFactor } from "@root/kitUI/Cart/calc"; -import { activateDiscounts, deactivateDiscounts, setSelectedDiscountIds, useDiscountStore } from "@root/stores/discounts"; +import { + activateDiscounts, + deactivateDiscounts, + setSelectedDiscountIds, + useDiscountStore, +} from "@root/stores/discounts"; +import axios from "axios"; +import { enqueueSnackbar } from "notistack"; +import { useEffect, useState } from "react"; - -const BoxButton = styled('div')(({ theme }) => ({ - [theme.breakpoints.down(400)]: { - justifyContent: 'center' - }, +const BoxButton = styled("div")(({ theme }) => ({ + [theme.breakpoints.down(400)]: { + justifyContent: "center", + }, })); const columns: GridColDef[] = [ - { - field: "id", - headerName: "ID", - width: 70, - sortable: false, - }, - { - field: "name", - headerName: "Название скидки", - width: 150, - sortable: false, - }, - { - field: "description", - headerName: "Описание", - width: 120, - sortable: false, - }, - { - field: "conditionType", - headerName: "Тип условия", - width: 120, - sortable: false, - }, - { - field: "factor", - headerName: "Процент скидки", - width: 120, - sortable: false, - }, - { - field: "value", - headerName: "Значение", - width: 120, - sortable: false, - }, - { - field: "active", - headerName: "Активна", - width: 120, - sortable: false, - }, + { + field: "id", + headerName: "ID", + width: 70, + sortable: false, + }, + { + field: "name", + headerName: "Название скидки", + width: 150, + sortable: false, + }, + { + field: "description", + headerName: "Описание", + width: 120, + sortable: false, + }, + { + field: "conditionType", + headerName: "Тип условия", + width: 120, + sortable: false, + }, + { + field: "factor", + headerName: "Процент скидки", + width: 120, + sortable: false, + }, + { + field: "value", + headerName: "Значение", + width: 120, + sortable: false, + }, + { + field: "active", + headerName: "Активна", + width: 120, + sortable: false, + }, ]; export default function DiscountDataGrid() { - const theme = useTheme(); - const discounts = useDiscountStore(state => state.discounts); - const selectedDiscountIds = useDiscountStore(state => state.selectedDiscountIds); + const theme = useTheme(); + const discounts = useDiscountStore((state) => state.discounts); + const selectedDiscountIds = useDiscountStore((state) => state.selectedDiscountIds); - const discountsGridData: GridRowsProp = discounts.map(discount => { - const value = - (discount.condition as any).purchasesAmount ?? - (discount.condition as any).cartPurchasesAmount ?? - (discount.condition as any).service?.value ?? - (discount.condition as any).privilege?.value ?? - "-"; + const [discount, setDiscount] = useState(); - return { - id: discount._id, - name: discount.name, - description: discount.description, - conditionType: discount.conditionType, - factor: formatDiscountFactor(findDiscountFactor(discount)), - active: discount.disabled ? "🚫" : "✅", - value, - }; - }); + useEffect(() => { + const axiosDiscount = async () => { + try { + const { data } = await axios({ + method: "get", + url: "https://penahub.gitlab.yandexcloud.net/price/discount/all", + }); + setDiscount(data); + console.log(data); + } catch (error) { + enqueueSnackbar("Ошибка получения скидок"); + } + }; - return ( - - - - - - - - - - - - ); -} \ No newline at end of file + axiosDiscount(); + }, []); + + const discountsGridData: GridRowsProp = discounts.map((discount) => { + const value = + (discount.condition as any).purchasesAmount ?? + (discount.condition as any).cartPurchasesAmount ?? + (discount.condition as any).service?.value ?? + (discount.condition as any).privilege?.value ?? + "-"; + + return { + id: discount._id, + name: discount.name, + description: discount.description, + conditionType: discount.conditionType, + factor: formatDiscountFactor(findDiscountFactor(discount)), + active: discount.disabled ? "🚫" : "✅", + value, + }; + }); + + return ( + + + + + + + + + + + + ); +} diff --git a/src/pages/dashboard/Content/Tariffs/index.tsx b/src/pages/dashboard/Content/Tariffs/index.tsx index c9ec9e7..40d06c0 100644 --- a/src/pages/dashboard/Content/Tariffs/index.tsx +++ b/src/pages/dashboard/Content/Tariffs/index.tsx @@ -29,7 +29,10 @@ export default function Tariffs() { Список тарифов - setSelectedTariffs(selectionModel)} /> + setSelectedTariffs(selectionModel)} + /> ); diff --git a/src/pages/dashboard/Content/Tariffs/tariffsDG.tsx b/src/pages/dashboard/Content/Tariffs/tariffsDG.tsx index ffbd6ab..2fbde80 100644 --- a/src/pages/dashboard/Content/Tariffs/tariffsDG.tsx +++ b/src/pages/dashboard/Content/Tariffs/tariffsDG.tsx @@ -1,6 +1,7 @@ +import React from "react"; import { useEffect, useState } from "react"; import { GridColDef, GridSelectionModel, GridToolbar } from "@mui/x-data-grid"; -import { IconButton } from "@mui/material"; +import { Box, Button, IconButton } from "@mui/material"; import BackspaceIcon from "@mui/icons-material/Backspace"; import { enqueueSnackbar } from "notistack"; @@ -12,22 +13,60 @@ import { findPrivilegeById } from "@root/stores/privileges"; import axios from "axios"; import { authStore } from "@root/stores/auth"; +import DeleteModal from "@root/kitUI/DeleteModal"; +import EditModal from "@root/kitUI/EditModal"; interface Props { + selectedTariffs: GridSelectionModel; handleSelectionChange: (selectionModel: GridSelectionModel) => void; } -export default function TariffsDG({ handleSelectionChange }: Props) { +export default function TariffsDG({ selectedTariffs, handleSelectionChange }: Props) { const exampleTariffs = useTariffStore((state) => state.tariffs); const [tariffs, setTariffs] = useState(); const [deletedRows, setDeletedRows] = useState([]); + const [openDeleteModal, setOpenDeleteModal] = useState(false); + const [openEditModal, setOpenEditModal] = useState(false); const { token } = authStore(); + const [errorEdit, setErrorEdit] = useState(false); + const [errorDelete, setErrorDelete] = useState(false); + const mergeTariffs = [...exampleTariffs, ...(tariffs?.tariffs ?? [])]; - console.log(mergeTariffs); + const tariffEdit = async (tarifIid: string, tariffName: string, tariffPrice: string) => { + try { + await axios({ + method: "put", + url: `https://admin.pena.digital/strator/tariff/${tarifIid}`, + headers: { + Authorization: `Bearer ${token}`, + }, + data: { + name: tariffName, + price: tariffPrice, + isCustom: true, + privilegies: [ + { + name: "Количество попыток использования", + privilegeId: "507f1f77bcf86cd799439011", + serviceKey: "docx-templater-service", + description: "Количество попыток использования", + type: "count", + value: "200", + price: 12300, + amount: 300, + }, + ], + }, + }); + } catch (error: any) { + setErrorEdit(true); + enqueueSnackbar(error.message); + } + }; - const tariffsDelete = async (tarifIid: string) => { + const tariffDelete = async (tarifIid: string) => { try { await axios({ method: "delete", @@ -38,8 +77,9 @@ export default function TariffsDG({ handleSelectionChange }: Props) { data: { id: tarifIid }, }); setDeletedRows((prevDeletedRows) => [...prevDeletedRows, tarifIid]); - } catch (error) { - enqueueSnackbar("Ошибка удаления тарифов"); + } catch (error: any) { + setErrorDelete(true); + enqueueSnackbar(error.message); } }; @@ -79,7 +119,7 @@ export default function TariffsDG({ handleSelectionChange }: Props) { return ( { - tariffsDelete(row.id); + tariffDelete(row.id); }} > @@ -120,14 +160,47 @@ export default function TariffsDG({ handleSelectionChange }: Props) { : 0, }) ); + + const selectedTariff = gridData.find((tariff) => tariff.id === selectedTariffs[0]); + const selectedTariffName = selectedTariff ? selectedTariff.name : ""; + + console.log(selectedTariff); + return ( - row.id} - components={{ Toolbar: GridToolbar }} - onSelectionModelChange={handleSelectionChange} - /> + <> + row.id} + components={{ Toolbar: GridToolbar }} + onSelectionModelChange={handleSelectionChange} + /> + {selectedTariffs.length ? ( + + + + + ) : ( + + )} + setOpenDeleteModal(false)} + /> + setOpenEditModal(false)} + /> + ); }