2024-03-03 13:30:57 +00:00
|
|
|
import { IconButton } from "@mui/material";
|
|
|
|
import { GridColDef } from "@mui/x-data-grid";
|
|
|
|
import { Promocode } from "@root/model/promocodes";
|
2024-03-30 20:19:53 +00:00
|
|
|
import { useMemo, useState } from "react";
|
2024-03-03 13:30:57 +00:00
|
|
|
|
2024-05-14 22:21:07 +00:00
|
|
|
import { BarChart, Delete, Edit } from "@mui/icons-material";
|
2024-04-26 11:12:51 +00:00
|
|
|
import { promocodeApi } from "@root/api/promocode/requests";
|
2024-03-26 15:41:22 +00:00
|
|
|
|
|
|
|
export function usePromocodeGridColDef(
|
2024-05-14 22:21:07 +00:00
|
|
|
setEdit: (id: string) => void,
|
2024-03-26 15:41:22 +00:00
|
|
|
setStatistics: (id: string) => void,
|
|
|
|
deletePromocode: (id: string) => void
|
|
|
|
) {
|
2024-04-26 11:12:51 +00:00
|
|
|
const validity = (value: string | number) => {
|
|
|
|
if (value === 0) {
|
|
|
|
return "неоганичен";
|
|
|
|
} else {
|
|
|
|
return new Date(value).toLocaleString();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return useMemo<GridColDef<Promocode, string | number, string>[]>(
|
2024-03-26 15:41:22 +00:00
|
|
|
() => [
|
|
|
|
{
|
|
|
|
field: "id",
|
|
|
|
headerName: "ID",
|
|
|
|
width: 30,
|
|
|
|
sortable: false,
|
|
|
|
valueGetter: ({ row }) => row.id,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
field: "codeword",
|
|
|
|
headerName: "Кодовое слово",
|
|
|
|
width: 160,
|
|
|
|
sortable: false,
|
|
|
|
valueGetter: ({ row }) => row.codeword,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
field: "factor",
|
|
|
|
headerName: "Коэф. скидки",
|
|
|
|
width: 120,
|
|
|
|
sortable: false,
|
|
|
|
valueGetter: ({ row }) =>
|
|
|
|
Math.round(row.bonus.discount.factor * 1000) / 1000,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
field: "activationCount",
|
|
|
|
headerName: "Кол-во активаций",
|
|
|
|
width: 140,
|
|
|
|
sortable: false,
|
|
|
|
valueGetter: ({ row }) => row.activationCount,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
field: "dueTo",
|
|
|
|
headerName: "Истекает",
|
|
|
|
width: 160,
|
|
|
|
sortable: false,
|
|
|
|
valueGetter: ({ row }) => row.dueTo * 1000,
|
2024-03-29 20:27:07 +00:00
|
|
|
valueFormatter: ({ value }) => `${validity(value)}`,
|
2024-03-26 15:41:22 +00:00
|
|
|
},
|
2024-04-26 11:12:51 +00:00
|
|
|
{
|
|
|
|
field: "description",
|
|
|
|
headerName: "Описание",
|
|
|
|
minWidth: 200,
|
|
|
|
flex: 1,
|
|
|
|
sortable: false,
|
|
|
|
valueGetter: ({ row }) => row.description,
|
|
|
|
},
|
2024-05-14 22:21:07 +00:00
|
|
|
{
|
|
|
|
field: "edit",
|
|
|
|
headerName: "",
|
|
|
|
width: 60,
|
|
|
|
sortable: false,
|
|
|
|
renderCell: (params) => {
|
|
|
|
return (
|
|
|
|
<IconButton
|
|
|
|
onClick={() => {
|
|
|
|
setEdit(params.row.id);
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Edit />
|
|
|
|
</IconButton>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
},
|
2024-03-26 15:41:22 +00:00
|
|
|
{
|
|
|
|
field: "settings",
|
|
|
|
headerName: "",
|
|
|
|
width: 60,
|
|
|
|
sortable: false,
|
|
|
|
renderCell: (params) => {
|
|
|
|
return (
|
2024-04-26 11:12:51 +00:00
|
|
|
<IconButton
|
|
|
|
onClick={() => {
|
|
|
|
setStatistics(params.row.id);
|
|
|
|
promocodeApi.getPromocodeStatistics(params.row.id, 0, 0);
|
|
|
|
}}
|
|
|
|
>
|
2024-03-26 15:41:22 +00:00
|
|
|
<BarChart />
|
|
|
|
</IconButton>
|
|
|
|
);
|
2024-03-03 13:30:57 +00:00
|
|
|
},
|
2024-03-26 15:41:22 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
field: "delete",
|
|
|
|
headerName: "",
|
|
|
|
width: 60,
|
|
|
|
sortable: false,
|
|
|
|
renderCell: (params) => {
|
|
|
|
return (
|
|
|
|
<IconButton onClick={() => deletePromocode(params.row.id)}>
|
|
|
|
<Delete />
|
|
|
|
</IconButton>
|
|
|
|
);
|
2024-03-03 13:30:57 +00:00
|
|
|
},
|
2024-03-26 15:41:22 +00:00
|
|
|
},
|
|
|
|
],
|
2024-05-14 22:21:07 +00:00
|
|
|
[deletePromocode, setStatistics, setEdit]
|
2024-03-26 15:41:22 +00:00
|
|
|
);
|
2024-03-03 13:30:57 +00:00
|
|
|
}
|