adminFront/src/pages/Setting/CardPrivilegie.tsx

160 lines
4.5 KiB
TypeScript
Raw Normal View History

2023-05-24 13:34:46 +00:00
import { 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";
interface CardPrivilegie {
2023-09-01 13:17:24 +00:00
privilege: PrivilegeWithAmount;
}
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>("");
const priceRef = useRef<any>(null);
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,
price: Number(inputValue),
});
2023-09-01 13:17:24 +00:00
if (putedPrivilegieError) {
return enqueueSnackbar(putedPrivilegieError);
}
2023-09-01 13:17:24 +00:00
priceRef.current.innerText = "price: " + inputValue;
setInputValue("");
setInputOpen(false);
};
2023-09-01 13:17:24 +00:00
const requestOnclickEnter = (event: any) => {
if (event.key === "Enter" && inputValue !== "") {
putPrivilegies();
setInputOpen(false);
}
};
2023-08-02 11:36:50 +00:00
2023-09-01 13:17:24 +00:00
const onCloseInput = (event: any) => {
if (event.key === "Escape") {
setInputOpen(false);
}
};
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"
title={
<Typography sx={{ fontSize: "16px" }}>
{privilege.description}
</Typography>
}
>
<IconButton disableRipple>
<svg
width="40"
height="40"
viewBox="0 0 20 20"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<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>
</Box>
2023-09-01 13:17:24 +00:00
</Box>
<Box
sx={{ width: "600px", display: "flex", justifyContent: "space-around" }}
>
{inputOpen ? (
<TextField
type="number"
onKeyDown={onCloseInput}
onKeyPress={requestOnclickEnter}
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" }}>
price: {privilege.price}
</div>
)}
<Typography sx={{ color: "white" }}>
{translationType[privilege.type]}
</Typography>
</Box>
</Box>
);
};