2023-08-31 18:00:07 +00:00
|
|
|
|
import { KeyboardEvent, useRef, useState } from "react";
|
2023-05-24 14:20:54 +00:00
|
|
|
|
import { enqueueSnackbar } from "notistack";
|
|
|
|
|
import { Box, IconButton, TextField, Tooltip, Typography } from "@mui/material";
|
2023-05-20 13:36:46 +00:00
|
|
|
|
import ModeEditOutlineOutlinedIcon from "@mui/icons-material/ModeEditOutlineOutlined";
|
2023-09-01 13:17:24 +00:00
|
|
|
|
import { PrivilegeWithAmount } from "@frontend/kitui";
|
|
|
|
|
import { putPrivilegie } from "@root/api/privilegies";
|
2023-05-16 16:57:44 +00:00
|
|
|
|
|
|
|
|
|
interface CardPrivilegie {
|
2023-09-01 13:17:24 +00:00
|
|
|
|
privilege: PrivilegeWithAmount;
|
2023-05-16 16:57:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-02 11:36:50 +00:00
|
|
|
|
export const СardPrivilegie = ({ privilege }: CardPrivilegie) => {
|
2023-09-01 13:17:24 +00:00
|
|
|
|
const [inputOpen, setInputOpen] = useState<boolean>(false);
|
|
|
|
|
const [inputValue, setInputValue] = useState<string>("");
|
2023-09-01 13:29:35 +00:00
|
|
|
|
const priceRef = useRef<HTMLDivElement>(null);
|
2023-05-16 16:57:44 +00:00
|
|
|
|
|
2023-09-01 13:17:24 +00:00
|
|
|
|
const translationType = {
|
|
|
|
|
count: "за единицу",
|
|
|
|
|
day: "за день",
|
|
|
|
|
mb: "за МБ",
|
|
|
|
|
};
|
2023-05-24 14:29:04 +00:00
|
|
|
|
|
2023-09-01 13:17:24 +00:00
|
|
|
|
const putPrivilegies = async () => {
|
|
|
|
|
const [_, putedPrivilegieError] = await putPrivilegie({
|
|
|
|
|
name: privilege.name,
|
|
|
|
|
privilegeId: privilege.privilegeId,
|
|
|
|
|
serviceKey: privilege.serviceKey,
|
|
|
|
|
description: privilege.description,
|
|
|
|
|
amount: 1,
|
|
|
|
|
type: privilege.type,
|
|
|
|
|
value: privilege.value,
|
2023-09-12 19:55:51 +00:00
|
|
|
|
price: 100 * Number(inputValue),
|
2023-09-01 13:17:24 +00:00
|
|
|
|
});
|
2023-05-16 16:57:44 +00:00
|
|
|
|
|
2023-09-01 13:17:24 +00:00
|
|
|
|
if (putedPrivilegieError) {
|
|
|
|
|
return enqueueSnackbar(putedPrivilegieError);
|
|
|
|
|
}
|
2023-05-16 16:57:44 +00:00
|
|
|
|
|
2023-09-01 13:29:35 +00:00
|
|
|
|
if (!priceRef.current) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-01 13:17:24 +00:00
|
|
|
|
priceRef.current.innerText = "price: " + inputValue;
|
|
|
|
|
setInputValue("");
|
|
|
|
|
setInputOpen(false);
|
|
|
|
|
};
|
2023-05-16 16:57:44 +00:00
|
|
|
|
|
2023-09-01 13:29:35 +00:00
|
|
|
|
const onTextfieldKeyDown = (event: KeyboardEvent) => {
|
|
|
|
|
if (event.key === "Escape") {
|
|
|
|
|
return setInputOpen(false);
|
|
|
|
|
}
|
2023-09-01 13:17:24 +00:00
|
|
|
|
if (event.key === "Enter" && inputValue !== "") {
|
|
|
|
|
putPrivilegies();
|
|
|
|
|
setInputOpen(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
2023-08-02 11:36:50 +00:00
|
|
|
|
|
2023-09-01 13:17:24 +00:00
|
|
|
|
return (
|
|
|
|
|
<Box
|
|
|
|
|
key={privilege.type}
|
|
|
|
|
sx={{
|
|
|
|
|
px: "20px",
|
|
|
|
|
|
|
|
|
|
display: "flex",
|
|
|
|
|
alignItems: "center",
|
|
|
|
|
border: "1px solid gray",
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<Box sx={{ display: "flex", borderRight: "1px solid gray" }}>
|
|
|
|
|
<Box sx={{ width: "200px", py: "25px" }}>
|
|
|
|
|
<Typography
|
|
|
|
|
variant="h6"
|
|
|
|
|
sx={{
|
|
|
|
|
color: "#fe9903",
|
|
|
|
|
overflowWrap: "break-word",
|
|
|
|
|
overflow: "hidden",
|
2023-05-25 12:06:46 +00:00
|
|
|
|
}}
|
2023-09-01 13:17:24 +00:00
|
|
|
|
>
|
|
|
|
|
{privilege.name}
|
|
|
|
|
</Typography>
|
|
|
|
|
<Tooltip
|
|
|
|
|
sx={{
|
|
|
|
|
span: {
|
|
|
|
|
fontSize: "1rem",
|
|
|
|
|
},
|
|
|
|
|
}}
|
|
|
|
|
placement="top"
|
2023-09-12 19:55:51 +00:00
|
|
|
|
title={<Typography sx={{ fontSize: "16px" }}>{privilege.description}</Typography>}
|
2023-09-01 13:17:24 +00:00
|
|
|
|
>
|
|
|
|
|
<IconButton disableRipple>
|
2023-09-12 19:55:51 +00:00
|
|
|
|
<svg width="40" height="40" viewBox="0 0 20 20" fill="none" xmlns="http://www.w3.org/2000/svg">
|
2023-09-01 13:17:24 +00:00
|
|
|
|
<path
|
|
|
|
|
d="M9.25 9.25H10V14.5H10.75"
|
|
|
|
|
stroke="#7E2AEA"
|
|
|
|
|
strokeWidth="1.5"
|
|
|
|
|
strokeLinecap="round"
|
|
|
|
|
strokeLinejoin="round"
|
|
|
|
|
/>
|
|
|
|
|
<path
|
|
|
|
|
d="M9.8125 7C10.4338 7 10.9375 6.49632 10.9375 5.875C10.9375 5.25368 10.4338 4.75 9.8125 4.75C9.19118 4.75 8.6875 5.25368 8.6875 5.875C8.6875 6.49632 9.19118 7 9.8125 7Z"
|
|
|
|
|
fill="#7E2AEA"
|
|
|
|
|
/>
|
|
|
|
|
</svg>
|
|
|
|
|
</IconButton>
|
|
|
|
|
</Tooltip>
|
|
|
|
|
<IconButton onClick={() => setInputOpen(!inputOpen)}>
|
|
|
|
|
<ModeEditOutlineOutlinedIcon sx={{ color: "gray" }} />
|
|
|
|
|
</IconButton>
|
2023-05-16 16:57:44 +00:00
|
|
|
|
</Box>
|
2023-09-01 13:17:24 +00:00
|
|
|
|
</Box>
|
2023-09-12 19:55:51 +00:00
|
|
|
|
<Box sx={{ width: "600px", display: "flex", justifyContent: "space-around" }}>
|
2023-09-01 13:17:24 +00:00
|
|
|
|
{inputOpen ? (
|
|
|
|
|
<TextField
|
|
|
|
|
type="number"
|
2023-09-01 13:29:35 +00:00
|
|
|
|
onKeyDown={onTextfieldKeyDown}
|
2023-09-01 13:17:24 +00:00
|
|
|
|
placeholder="введите число"
|
|
|
|
|
fullWidth
|
|
|
|
|
onChange={(event) => setInputValue(event.target.value)}
|
|
|
|
|
sx={{
|
|
|
|
|
alignItems: "center",
|
|
|
|
|
width: "400px",
|
|
|
|
|
"& .MuiInputBase-root": {
|
|
|
|
|
backgroundColor: "#F2F3F7",
|
|
|
|
|
height: "48px",
|
|
|
|
|
},
|
|
|
|
|
}}
|
|
|
|
|
inputProps={{
|
|
|
|
|
sx: {
|
|
|
|
|
borderRadius: "10px",
|
|
|
|
|
fontSize: "18px",
|
|
|
|
|
lineHeight: "21px",
|
|
|
|
|
py: 0,
|
|
|
|
|
},
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
) : (
|
|
|
|
|
<div ref={priceRef} style={{ color: "white", marginRight: "5px" }}>
|
2023-09-12 19:55:51 +00:00
|
|
|
|
price: {privilege.price / 100}
|
2023-09-01 13:17:24 +00:00
|
|
|
|
</div>
|
|
|
|
|
)}
|
2023-09-12 19:55:51 +00:00
|
|
|
|
<Typography sx={{ color: "white" }}>{translationType[privilege.type]}</Typography>
|
2023-09-01 13:17:24 +00:00
|
|
|
|
</Box>
|
|
|
|
|
</Box>
|
|
|
|
|
);
|
2023-05-16 16:57:44 +00:00
|
|
|
|
};
|