обноление данных о юзере после покупки
This commit is contained in:
parent
ac7c001738
commit
9bb69f1ab2
@ -15,6 +15,9 @@ import { outCart } from "../Tariffs/Tariffs";
|
||||
import { inCart } from "../Tariffs/Tariffs";
|
||||
import { isTestServer } from "@/utils/hooks/useDomainDefine";
|
||||
import { useToken } from "@frontend/kitui";
|
||||
import { useSWRConfig } from "swr";
|
||||
import { makeRequest } from "@api/makeRequest";
|
||||
import { setUserAccount, setCustomerAccount } from "@/stores/user";
|
||||
|
||||
const tariff = "6844b8858258f5cc35791ef7";
|
||||
export default function PersonalizationAI() {
|
||||
@ -30,13 +33,12 @@ export default function PersonalizationAI() {
|
||||
const token = useToken();
|
||||
const userId = useUserStore((state) => state.userId);
|
||||
|
||||
const [isStartCreate, setStartCreate] = useState(false);
|
||||
const [gender, setGender] = useState<string>('');
|
||||
console.log("gender")
|
||||
console.log(gender)
|
||||
const [age, setAge] = useState<string>('');
|
||||
const [ageError, setAgeError] = useState(false);
|
||||
const [isModalOpen, setIsModalOpen] = useState(false);
|
||||
// Обновляем данные пользователя через SWR
|
||||
const { mutate } = useSWRConfig();
|
||||
|
||||
const resetForm = () => {
|
||||
setGender('');
|
||||
@ -44,11 +46,6 @@ export default function PersonalizationAI() {
|
||||
setAgeError(false);
|
||||
};
|
||||
|
||||
const checkPrivileges = async () => {
|
||||
if (privilegesOfUser?.quizGigaChat && privilegesOfUser.quizGigaChat.amount > 0) {
|
||||
return true;
|
||||
} else return false;
|
||||
};
|
||||
|
||||
const createNewLink = async () => {
|
||||
if (!quiz?.backendId) {
|
||||
@ -67,7 +64,7 @@ export default function PersonalizationAI() {
|
||||
|
||||
if (error) {
|
||||
enqueueSnackbar('Не удалось добавить ссылку', { variant: 'error' });
|
||||
return;
|
||||
return [, error];
|
||||
}
|
||||
|
||||
if (result) {
|
||||
@ -82,7 +79,42 @@ export default function PersonalizationAI() {
|
||||
enqueueSnackbar('Ссылка успешно добавлена', { variant: 'success' });
|
||||
resetForm();
|
||||
setIsModalOpen(false);
|
||||
setStartCreate(false);
|
||||
|
||||
// Обновляем данные пользователя после успешного создания ссылки
|
||||
try {
|
||||
const [userAccountResult, customerAccountResult] = await Promise.all([
|
||||
makeRequest({
|
||||
url: `${process.env.REACT_APP_DOMAIN}/squiz/account/get`,
|
||||
method: "GET",
|
||||
useToken: true,
|
||||
withCredentials: false,
|
||||
}).catch(error => {
|
||||
console.log(error)
|
||||
enqueueSnackbar("Ошибка при обновлении данных пользователя", { variant: "error" });
|
||||
return null;
|
||||
}),
|
||||
makeRequest({
|
||||
url: `${process.env.REACT_APP_DOMAIN}/customer/v1.0.1/account`,
|
||||
method: "GET",
|
||||
useToken: true,
|
||||
withCredentials: false,
|
||||
}).catch(error => {
|
||||
console.log(error)
|
||||
enqueueSnackbar("Ошибка при обновлении данных клиента", { variant: "error" });
|
||||
return null;
|
||||
})
|
||||
]);
|
||||
|
||||
if (userAccountResult) {
|
||||
setUserAccount(userAccountResult);
|
||||
}
|
||||
if (customerAccountResult) {
|
||||
setCustomerAccount(customerAccountResult);
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
enqueueSnackbar("Ошибка при обновлении данных", { variant: "error" });
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
enqueueSnackbar('Произошла ошибка при добавлении', { variant: 'error' });
|
||||
@ -174,10 +206,10 @@ export default function PersonalizationAI() {
|
||||
setUtmParams(paramString ? `&${paramString}` : "");
|
||||
};
|
||||
|
||||
console.log("______-------__________---------_____---__--__-__--___------__--_---_--_----__--__-__--_--__--__--_---_______-privilegesOfUser")
|
||||
console.log(privilegesOfUser)
|
||||
const startCreate = async () => {
|
||||
setStartCreate(true);
|
||||
const hasPrivileges = await checkPrivileges();
|
||||
if (hasPrivileges) {
|
||||
if (privilegesOfUser?.quizGigaChat && privilegesOfUser.quizGigaChat.amount > 0) {
|
||||
createNewLink();
|
||||
} else {
|
||||
setIsModalOpen(true);
|
||||
@ -186,7 +218,7 @@ export default function PersonalizationAI() {
|
||||
|
||||
const tryBuy = async ({ id, price }: { id: string; price: number }) => {
|
||||
//Если в корзине что-то было - выкладываем содержимое и запоминаем чо там лежало
|
||||
if (user.cart.length > 0) {
|
||||
if (user?.cart?.length > 0) {
|
||||
outCart(user.cart);
|
||||
}
|
||||
//Добавляем желаемый тариф в корзину
|
||||
@ -195,7 +227,6 @@ export default function PersonalizationAI() {
|
||||
if (addError) {
|
||||
//Развращаем товары в корзину
|
||||
inCart();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@ -214,12 +245,15 @@ export default function PersonalizationAI() {
|
||||
|
||||
//другая ошибка
|
||||
enqueueSnackbar("Произошла ошибка. Попробуйте позже");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
//Развращаем товары в корзину
|
||||
inCart();
|
||||
|
||||
|
||||
// Если покупка прошла успешно, создаем новую ссылку
|
||||
createNewLink();
|
||||
};
|
||||
|
||||
return (
|
||||
@ -321,7 +355,6 @@ export default function PersonalizationAI() {
|
||||
open={isModalOpen}
|
||||
onClose={() => {
|
||||
setIsModalOpen(false);
|
||||
setStartCreate(false);
|
||||
}}
|
||||
onCreate={tryBuy}
|
||||
/>
|
||||
|
@ -1,7 +1,7 @@
|
||||
import useSWR from 'swr';
|
||||
import { getUser } from '@api/user';
|
||||
import type { User } from '@frontend/kitui';
|
||||
|
||||
|
||||
export const useUser = () => {
|
||||
return useSWR<User>('user', getUser);
|
||||
};
|
@ -1,9 +1,7 @@
|
||||
import { useEffect, useLayoutEffect, useRef } from "react";
|
||||
import { createUserAccount, devlog } from "@frontend/kitui";
|
||||
import { isAxiosError } from "axios";
|
||||
|
||||
import { makeRequest } from "@api/makeRequest";
|
||||
|
||||
import type { UserAccount } from "@frontend/kitui";
|
||||
import { setUserAccount } from "@/stores/user";
|
||||
|
||||
@ -20,10 +18,12 @@ export const useUserAccountFetcher = <T = UserAccount>({
|
||||
}) => {
|
||||
const onNewUserAccountRef = useRef(onNewUserAccount);
|
||||
const onErrorRef = useRef(onError);
|
||||
|
||||
useLayoutEffect(() => {
|
||||
onNewUserAccountRef.current = onNewUserAccount;
|
||||
onErrorRef.current = onError;
|
||||
}, [onError, onNewUserAccount]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!userId) return;
|
||||
const controller = new AbortController();
|
||||
|
Loading…
Reference in New Issue
Block a user