2024-05-13 13:24:41 +00:00
|
|
|
|
import { useEffect, useLayoutEffect, useRef, useState } from "react";
|
2024-04-12 21:05:15 +00:00
|
|
|
|
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-05-13 13:24:41 +00:00
|
|
|
|
import { makeRequest } from "@api/makeRequest";
|
2024-05-02 08:38:57 +00:00
|
|
|
|
import { getDiscounts } from "@api/discounts";
|
|
|
|
|
|
|
2024-05-13 13:24:41 +00:00
|
|
|
|
import {
|
|
|
|
|
|
clearUserData,
|
|
|
|
|
|
OriginalUserAccount,
|
|
|
|
|
|
setUserAccount,
|
|
|
|
|
|
useUserStore,
|
|
|
|
|
|
} from "@root/user";
|
2024-04-12 21:05:15 +00:00
|
|
|
|
import { parseAxiosError } from "@utils/parse-error";
|
2024-05-15 11:44:10 +00:00
|
|
|
|
import { useUserAccountFetcher } from "@utils/hooks/useUserAccountFetcher";
|
2024-05-02 08:38:57 +00:00
|
|
|
|
import type { Discount } from "@model/discounts";
|
2024-05-15 12:37:42 +00:00
|
|
|
|
import { clearAuthToken, createUserAccount, devlog } from "@frontend/kitui";
|
2024-05-13 13:24:41 +00:00
|
|
|
|
import { useNavigate } from "react-router-dom";
|
|
|
|
|
|
import { isAxiosError } from "axios";
|
2024-05-15 11:44:10 +00:00
|
|
|
|
import { activatePromocode } from "@api/promocode";
|
|
|
|
|
|
import { getAccount } from "@api/user";
|
2024-05-02 08:38:57 +00:00
|
|
|
|
|
2024-04-12 21:05:15 +00:00
|
|
|
|
export function CheckFastlink() {
|
2024-05-11 18:37:59 +00:00
|
|
|
|
const user = useUserStore();
|
2024-05-12 01:03:56 +00:00
|
|
|
|
const userId = useUserStore((state) => state.userId);
|
|
|
|
|
|
const navigate = useNavigate();
|
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("");
|
|
|
|
|
|
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) {
|
2024-05-15 11:44:10 +00:00
|
|
|
|
const [greetings, activationError] = await activatePromocode(promocode);
|
|
|
|
|
|
|
|
|
|
|
|
if (activationError || !greetings) {
|
|
|
|
|
|
enqueueSnackbar(activationError);
|
2024-04-12 21:05:15 +00:00
|
|
|
|
localStorage.setItem("fl", "");
|
2024-05-15 11:44:10 +00:00
|
|
|
|
|
|
|
|
|
|
return;
|
2024-04-12 21:05:15 +00:00
|
|
|
|
}
|
2024-05-15 11:44:10 +00:00
|
|
|
|
|
|
|
|
|
|
enqueueSnackbar(
|
|
|
|
|
|
greetings !== "" ? greetings : "Промокод успешно активирован",
|
|
|
|
|
|
);
|
|
|
|
|
|
localStorage.setItem("fl", "");
|
|
|
|
|
|
const [responseAccount, accountError] = await getAccount();
|
|
|
|
|
|
|
|
|
|
|
|
if (accountError || !responseAccount) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
setUserAccount(responseAccount);
|
|
|
|
|
|
mutate("discounts");
|
|
|
|
|
|
return greetings;
|
2024-04-12 21:05:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
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-13 13:24:41 +00:00
|
|
|
|
}, [
|
|
|
|
|
|
user.userId,
|
|
|
|
|
|
discounts,
|
|
|
|
|
|
user.customerAccount?.createdAt,
|
|
|
|
|
|
user.userAccount?.created_at,
|
|
|
|
|
|
]);
|
2024-05-12 01:03:56 +00:00
|
|
|
|
|
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>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|