2024-04-12 21:05:15 +00:00
|
|
|
|
import { useEffect, useLayoutEffect, useState } from "react";
|
|
|
|
|
|
import { Box, Button, Modal, Typography } from "@mui/material";
|
|
|
|
|
|
import { enqueueSnackbar } from "notistack";
|
2024-05-02 08:38:57 +00:00
|
|
|
|
import { mutate } from "swr";
|
|
|
|
|
|
|
2024-04-16 20:20:46 +00:00
|
|
|
|
import makeRequest from "@api/makeRequest";
|
2024-05-02 08:38:57 +00:00
|
|
|
|
import { getDiscounts } from "@api/discounts";
|
|
|
|
|
|
|
|
|
|
|
|
import { useUserStore } from "@root/user";
|
2024-04-12 21:05:15 +00:00
|
|
|
|
import { parseAxiosError } from "@utils/parse-error";
|
|
|
|
|
|
|
2024-05-02 08:38:57 +00:00
|
|
|
|
import type { Discount } from "@model/discounts";
|
|
|
|
|
|
|
2024-04-12 21:05:15 +00:00
|
|
|
|
export function CheckFastlink() {
|
2024-05-11 18:37:59 +00:00
|
|
|
|
const user = useUserStore();
|
2024-05-02 08:38:57 +00:00
|
|
|
|
const [discounts, setDiscounts] = useState<Discount[]>([]);
|
2024-04-12 21:05:15 +00:00
|
|
|
|
const [askToChange, setAskToChange] = useState(false);
|
|
|
|
|
|
const [promocode, setPromocode] = useState("");
|
2024-05-11 18:37:59 +00:00
|
|
|
|
console.log(
|
|
|
|
|
|
user.userAccount,
|
|
|
|
|
|
user.customerAccount)
|
2024-04-12 21:05:15 +00:00
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
const get = async () => {
|
2024-05-11 18:37:59 +00:00
|
|
|
|
if (!user.userId) {
|
2024-05-02 08:38:57 +00:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-11 18:37:59 +00:00
|
|
|
|
const [discounts] = await getDiscounts(user.userId);
|
2024-05-02 08:38:57 +00:00
|
|
|
|
|
|
|
|
|
|
if (discounts?.length) {
|
|
|
|
|
|
setDiscounts(discounts);
|
|
|
|
|
|
}
|
2024-04-12 21:05:15 +00:00
|
|
|
|
};
|
2024-05-02 08:43:10 +00:00
|
|
|
|
|
2024-04-12 21:05:15 +00:00
|
|
|
|
get();
|
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
|
|
const fetchPromocode = async () => {
|
|
|
|
|
|
if (promocode.length > 0) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const response = await makeRequest<
|
|
|
|
|
|
{ codeword: string } | { fastLink: string },
|
|
|
|
|
|
{ greetings: string }
|
|
|
|
|
|
>({
|
|
|
|
|
|
url:
|
|
|
|
|
|
process.env.REACT_APP_DOMAIN + "/codeword/promocode" + "/activate",
|
|
|
|
|
|
method: "POST",
|
|
|
|
|
|
contentType: true,
|
|
|
|
|
|
body: { fastLink: promocode },
|
|
|
|
|
|
});
|
2024-04-12 23:28:08 +00:00
|
|
|
|
enqueueSnackbar("Промокод успешно применён");
|
2024-04-12 21:05:15 +00:00
|
|
|
|
localStorage.setItem("fl", "");
|
|
|
|
|
|
mutate("discounts");
|
|
|
|
|
|
return response.greetings;
|
|
|
|
|
|
} catch (nativeError) {
|
|
|
|
|
|
const [error] = parseAxiosError(nativeError);
|
|
|
|
|
|
enqueueSnackbar(error);
|
|
|
|
|
|
localStorage.setItem("fl", "");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
useLayoutEffect(() => {
|
|
|
|
|
|
//Промокод может быть или в урл адресе или в ЛС
|
|
|
|
|
|
const params = new URLSearchParams(window.location.search);
|
|
|
|
|
|
const flURL = params.get("fl");
|
|
|
|
|
|
|
|
|
|
|
|
if (flURL !== null) {
|
|
|
|
|
|
localStorage.setItem("fl", flURL);
|
|
|
|
|
|
var url = new URL(window.location.href);
|
|
|
|
|
|
url.searchParams.delete("fl");
|
|
|
|
|
|
history.pushState(null, document.title, url);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const flLS = localStorage.getItem("fl");
|
|
|
|
|
|
|
|
|
|
|
|
if (flLS !== null && flLS.length > 0) {
|
|
|
|
|
|
setPromocode(flLS);
|
|
|
|
|
|
|
2024-05-11 18:37:59 +00:00
|
|
|
|
if (user.userId !== null) {
|
2024-04-12 21:05:15 +00:00
|
|
|
|
//У нас есть промокод и юзер авторизован. Проверяем есть ли у него применённый промокод
|
2024-05-11 18:37:59 +00:00
|
|
|
|
//Проверяем были ли запросы на аккаунт и кастомер аккаунт
|
|
|
|
|
|
if (user.userAccount !== null && user.customerAccount !== null) {
|
|
|
|
|
|
if (discounts?.find((e) => e.Condition.User === user.userId)) {
|
|
|
|
|
|
setAskToChange(true);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
fetchPromocode();
|
|
|
|
|
|
}
|
2024-04-12 21:05:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-05-11 19:28:00 +00:00
|
|
|
|
}, [user.userId, discounts, user.customerAccount?.createdAt, user.userAccount?.created_at]);
|
2024-04-12 21:05:15 +00:00
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
|
<Modal
|
|
|
|
|
|
open={askToChange}
|
|
|
|
|
|
onClose={() => setAskToChange(false)}
|
|
|
|
|
|
aria-labelledby="modal-modal-title"
|
|
|
|
|
|
aria-describedby="modal-modal-description"
|
|
|
|
|
|
>
|
|
|
|
|
|
<Box
|
|
|
|
|
|
sx={{
|
|
|
|
|
|
position: "absolute" as "absolute",
|
|
|
|
|
|
top: "50%",
|
|
|
|
|
|
left: "50%",
|
|
|
|
|
|
transform: "translate(-50%, -50%)",
|
|
|
|
|
|
bgcolor: "background.paper",
|
|
|
|
|
|
p: 4,
|
|
|
|
|
|
borderRadius: 2,
|
|
|
|
|
|
}}
|
|
|
|
|
|
>
|
|
|
|
|
|
<Typography textAlign="center" variant="h6" component="h2">
|
|
|
|
|
|
Заменить текущий промокод?
|
|
|
|
|
|
</Typography>
|
|
|
|
|
|
<Box
|
|
|
|
|
|
sx={{
|
|
|
|
|
|
display: "flex",
|
|
|
|
|
|
mt: "20px",
|
|
|
|
|
|
flexWrap: "wrap",
|
|
|
|
|
|
justifyContent: "space-evenly",
|
|
|
|
|
|
}}
|
|
|
|
|
|
>
|
|
|
|
|
|
<Button
|
|
|
|
|
|
sx={{ margin: "5px", minWidth: " auto" }}
|
|
|
|
|
|
variant="pena-contained-dark"
|
2024-04-12 23:28:08 +00:00
|
|
|
|
onClick={() => {
|
|
|
|
|
|
fetchPromocode();
|
|
|
|
|
|
setAskToChange(false);
|
|
|
|
|
|
}}
|
2024-04-12 21:05:15 +00:00
|
|
|
|
>
|
|
|
|
|
|
Да
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
<Button
|
|
|
|
|
|
sx={{ margin: "5px", minWidth: " auto" }}
|
|
|
|
|
|
variant="pena-contained-dark"
|
|
|
|
|
|
onClick={() => {
|
|
|
|
|
|
setAskToChange(false);
|
|
|
|
|
|
localStorage.setItem("fl", "");
|
|
|
|
|
|
}}
|
|
|
|
|
|
>
|
|
|
|
|
|
Нет
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
</Box>
|
|
|
|
|
|
</Box>
|
|
|
|
|
|
</Modal>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|