adminFront/src/pages/Setting/CardPrivilegie.tsx

165 lines
4.9 KiB
TypeScript
Raw Normal View History

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";
2023-09-16 18:02:01 +00:00
import { putPrivilege } from "@root/api/privilegies";
import SaveIcon from '@mui/icons-material/Save';
import { currencyFormatter } from "@root/utils/currencyFormatter";
2023-09-16 18:02:01 +00:00
interface CardPrivilege {
2023-09-01 13:17:24 +00:00
privilege: PrivilegeWithAmount;
}
2023-09-16 18:02:01 +00:00
export const СardPrivilege = ({ privilege }: CardPrivilege) => {
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-09-01 13:17:24 +00:00
const translationType = {
count: "за единицу",
day: "за день",
mb: "за МБ",
};
2023-05-24 14:29:04 +00:00
2023-09-16 18:02:01 +00:00
const putPrivileges = async () => {
const [_, putedPrivilegeError] = await putPrivilege({
2023-09-01 13:17:24 +00:00
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-09-16 18:02:01 +00:00
if (putedPrivilegeError) {
return enqueueSnackbar(putedPrivilegeError);
2023-09-01 13:17:24 +00:00
}
2023-09-01 13:29:35 +00:00
if (!priceRef.current) {
return;
}
enqueueSnackbar("Изменения сохранены");
2023-09-01 13:17:24 +00:00
priceRef.current.innerText = "price: " + inputValue;
setInputValue("");
setInputOpen(false);
};
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 !== "") {
2023-09-16 18:02:01 +00:00
putPrivileges();
2023-09-01 13:17:24 +00:00
setInputOpen(false);
}
};
2023-08-02 11:36:50 +00:00
function handleSavePrice() {
setInputOpen(false);
if (!inputValue) return;
putPrivileges();
}
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>
</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,
},
}}
InputProps={{
endAdornment: (
<IconButton onClick={handleSavePrice}>
<SaveIcon />
</IconButton>
)
}}
2023-09-01 13:17:24 +00:00
/>
) : (
<div ref={priceRef} style={{ color: "white", marginRight: "5px" }}>
price: {currencyFormatter.format(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>
);
};