import { useEffect, useState } from "react"; import { Box, Button, Typography, Modal, TextField, useTheme, useMediaQuery, IconButton, } from "@mui/material"; import { DataGrid, GridLoadingOverlay, GridToolbar } from "@mui/x-data-grid"; import { DatePicker } from "@mui/x-date-pickers/DatePicker"; import { usePrivilegeStore } from "@root/stores/privilegesStore"; import { fadeIn } from "@root/utils/style/keyframes"; import ContentCopyIcon from "@mui/icons-material/ContentCopy"; import type { GridColDef } from "@mui/x-data-grid"; import type { Promocode, PromocodeStatistics } from "@root/model/promocodes"; const host = window.location.hostname; let isTest = host.includes("s"); type StatisticsModalProps = { id: string; to: number; from: number; setId: (id: string) => void; setTo: (date: number) => void; setFrom: (date: number) => void; promocodes: Promocode[]; promocodeStatistics: PromocodeStatistics | null | undefined; createFastLink: (id: string) => Promise; }; type Row = { id: number; link: string; useCount: number; }; const COLUMNS: GridColDef[] = [ { field: "copy", headerName: "копировать", width: 50, sortable: false, valueGetter: ({ row }) => String(row.useCount), renderCell: (params) => { return ( navigator.clipboard.writeText( `https://${isTest ? "s" : ""}hub.pena.digital/?fl=${params.row.link }` ) } > ); }, }, { field: "link", headerName: "Ссылка", width: 320, sortable: false, valueGetter: ({ row }) => row.link, renderCell: ({ value }) => value?.split("|").map((link) => {link}), }, { field: "useCount", headerName: "Использований", width: 120, sortable: false, valueGetter: ({ row }) => String(row.useCount), }, { field: "purchasesCount", headerName: "Покупок", width: 70, sortable: false, valueGetter: ({ row }) => String(0), }, ]; export const StatisticsModal = ({ id, setId, setFrom, from, to, setTo, promocodeStatistics, promocodes, createFastLink, }: StatisticsModalProps) => { const [startDate, setStartDate] = useState(new Date()); const [endDate, setEndDate] = useState(new Date()); const theme = useTheme(); const isMobile = useMediaQuery(theme.breakpoints.down(550)); const [rows, setRows] = useState([]); const { privileges } = usePrivilegeStore(); const currentPrivilegeId = promocodes.find((promocode) => promocode.id === id) ?.bonus.privilege.privilegeID; const privilege = privileges.find((item) => item.privilegeId === currentPrivilegeId); const promocode = promocodes.find((item) => item.id === id); console.log(promocode) const createFastlink = async () => { await createFastLink(id); getParseData(); }; const getParseData = async () => { const rows = promocodes .find((promocode) => promocode.id === id) ?.fastLinks?.map((link, index) => ({ link, id: index, useCount: promocodeStatistics?.usageMap[link] ?? 0, })) as Row[]; setRows(rows); }; useEffect(() => { if (id.length > 0) { getParseData(); } if (!id) { setRows([]); } }, [id]); // const formatTo = to === null ? 0 : moment(to).unix() // const formatFrom = from === null ? 0 : moment(from).unix() // useEffect(() => { // (async () => { // const gottenGeneral = await promocodeStatistics(id, startDate, endDate) // setGeneral(gottenGeneral[0]) // })() // }, [to, from]); return ( { setId(""); setStartDate(new Date()); setEndDate(new Date()); }} sx={{ "& > .MuiBox-root": { outline: "none", padding: "32px 32px 16px" }, }} > от date && setStartDate(date)} renderInput={(params) => ( )} InputProps={{ sx: { height: "40px", color: theme.palette.secondary.main, border: "1px solid", borderColor: theme.palette.secondary.main, "& .MuiSvgIcon-root": { color: theme.palette.secondary.main, }, }, }} /> до date && setEndDate(date)} renderInput={(params) => ( )} InputProps={{ sx: { height: "40px", color: theme.palette.secondary.main, border: "1px solid", borderColor: theme.palette.secondary.main, "& .MuiSvgIcon-root": { color: theme.palette.secondary.main, }, }, }} /> { privilege === undefined ? Нет привилегии : название привилегии: {privilege.name} {promocode?.activationCount} активаций из {promocode?.activationLimit} приветствие: "{promocode?.greetings}" {promocode?.bonus?.discount?.factor !== undefined && скидка: {100 - (promocode?.bonus?.discount?.factor * 100)}%} {количество привилегии: {promocode?.bonus?.privilege?.amount}} {promocode?.dueTo !== undefined && promocode.dueTo > 0 && действует до: {new Date(promocode.dueTo).toLocaleString()}} } ); };