front-hub/src/pages/TariffConstructor/CustomTariffCard.tsx

184 lines
4.9 KiB
TypeScript
Raw Normal View History

2023-07-28 13:24:21 +00:00
import {
2023-11-05 23:33:40 +00:00
Box,
Button,
Divider,
2023-12-15 19:49:10 +00:00
Badge,
2023-11-05 23:33:40 +00:00
Typography,
useMediaQuery,
useTheme,
} from "@mui/material"
import TariffPrivilegeSlider from "./TariffItem"
2023-07-28 13:24:21 +00:00
import {
2023-11-05 23:33:40 +00:00
createAndSendTariff,
useCustomTariffsStore,
} from "@root/stores/customTariffs"
import { cardShadow } from "@root/utils/theme"
import { currencyFormatter } from "@root/utils/currencyFormatter"
import { Privilege, getMessageFromFetchError } from "@frontend/kitui"
import { enqueueSnackbar } from "notistack"
import { updateTariffs } from "@root/stores/tariffs"
import { addTariffToCart } from "@root/stores/user"
2023-05-27 11:50:21 +00:00
interface Props {
serviceKey: string;
privileges: Privilege[];
2023-05-27 11:50:21 +00:00
}
2023-07-27 16:51:12 +00:00
export default function CustomTariffCard({ serviceKey, privileges }: Props) {
2023-11-05 23:33:40 +00:00
const theme = useTheme()
const upMd = useMediaQuery(theme.breakpoints.up("md"))
const summaryPriceBeforeDiscounts = useCustomTariffsStore(
(state) => state.summaryPriceBeforeDiscountsMap
)
const summaryPriceAfterDiscounts = useCustomTariffsStore(
(state) => state.summaryPriceAfterDiscountsMap
)
2023-11-05 23:33:40 +00:00
const priceBeforeDiscounts = summaryPriceBeforeDiscounts[serviceKey] ?? 0
const priceAfterDiscounts = summaryPriceAfterDiscounts[serviceKey] ?? 0
2023-05-27 11:50:21 +00:00
2023-11-05 23:33:40 +00:00
async function handleConfirmClick() {
const [createTariffResponse, createTariffError] = await createAndSendTariff(
serviceKey
)
2023-11-05 23:33:40 +00:00
if (createTariffError) {
return enqueueSnackbar(createTariffError)
}
2023-08-30 14:30:41 +00:00
2023-11-05 23:33:40 +00:00
if (createTariffResponse) {
updateTariffs([createTariffResponse])
2023-08-30 14:30:41 +00:00
2023-11-05 23:33:40 +00:00
await addTariffToCart(createTariffResponse._id)
2023-05-27 11:50:21 +00:00
2023-11-05 23:33:40 +00:00
enqueueSnackbar("Тариф добавлен в корзину")
}
}
2023-08-09 17:14:12 +00:00
2023-11-05 23:33:40 +00:00
return (
<Box
sx={{
backgroundColor: "white",
width: "100%",
display: "flex",
flexDirection: upMd ? "row" : "column",
borderRadius: "12px",
boxShadow: cardShadow,
}}
>
<Box
sx={{
p: "20px",
pr: upMd ? "35px" : undefined,
display: "flex",
flexBasis: 0,
flexGrow: 2.37,
flexWrap: "wrap",
flexDirection: "column",
gap: "25px",
}}
>
{privileges.map((privilege) => (
<TariffPrivilegeSlider key={privilege._id} privilege={privilege} />
))}
</Box>
{!upMd && (
<Divider
sx={{ mx: "20px", my: "10px", borderColor: theme.palette.gray.main }}
/>
)}
<Box
sx={{
display: "flex",
flexBasis: 0,
flexGrow: 1,
flexDirection: "column",
justifyContent: "space-between",
alignItems: "start",
color: theme.palette.gray.dark,
p: "20px",
pl: upMd ? "33px" : undefined,
borderLeft: upMd ? `1px solid ${theme.palette.gray.main}` : undefined,
}}
>
<Box
sx={{
display: "flex",
justifyContent: "space-between",
gap: "15%",
mb: "auto",
width: "100%",
}}
>
<Typography>
2023-08-30 14:30:41 +00:00
Чем больше пакеты, тем дешевле подписки и опции{" "}
2023-11-05 23:33:40 +00:00
</Typography>
</Box>
<Typography mb="20px" mt="18px">
Сумма с учетом скидки
2023-11-05 23:33:40 +00:00
</Typography>
<Badge
badgeContent={
priceBeforeDiscounts - priceAfterDiscounts ? (
<span
style={{
backgroundColor: "#ff4904",
color: "white",
fontSize: "14px",
}}
>
-
{((priceBeforeDiscounts - priceAfterDiscounts))?`${((priceBeforeDiscounts - priceAfterDiscounts)/priceBeforeDiscounts*100).toFixed(0)}%`:null}
</span>
) : null
}
color="success"
sx={{
"& .MuiBadge-dot": {
backgroundColor: "#ff4904",
width: "10px",
height: "10px",
},
"& .MuiBadge-anchorOriginTopRightRectangle": {
backgroundColor: "#ff4904",
top: "5px",
right: "5px",
},
"& .MuiBadge-anchorOriginTopRightRectangular": {
backgroundColor: "#ff4904",
height: "31px",
padding: "5px 10px",
right: "-25px",
},
}}>
2023-11-05 23:33:40 +00:00
<Box
sx={{
display: "flex",
alignItems: "center",
gap: "20px",
mb: "30px",
}}
>
<Typography variant="price">
{currencyFormatter.format(priceAfterDiscounts / 100)}
</Typography>
{(priceAfterDiscounts - priceBeforeDiscounts) ?
2023-11-05 23:33:40 +00:00
<Typography variant="oldPrice" pt="3px">
{currencyFormatter.format(priceBeforeDiscounts / 100)}
</Typography>:null
}
2023-11-05 23:33:40 +00:00
</Box>
2023-12-15 19:49:10 +00:00
</Badge>
2023-11-05 23:33:40 +00:00
<Button
disabled={!priceBeforeDiscounts}
variant="pena-contained-dark"
onClick={handleConfirmClick}
>
Выбрать
2023-11-05 23:33:40 +00:00
</Button>
</Box>
</Box>
)
2023-05-27 11:50:21 +00:00
}