--
This commit is contained in:
parent
ea4c238813
commit
219041ead6
@ -20,7 +20,7 @@ import { createTariffElements } from "./tariffsUtils/createTariffElements";
|
||||
function TariffPage() {
|
||||
const theme = useTheme();
|
||||
const isTablet = useMediaQuery(theme.breakpoints.down(1000));
|
||||
const location = useLocation();
|
||||
// const location = useLocation();
|
||||
const navigate = useNavigate();
|
||||
|
||||
const [tariffs, setTariffs] = useState();
|
||||
@ -57,27 +57,31 @@ function TariffPage() {
|
||||
console.log("discounts ", discounts);
|
||||
|
||||
const openModalHC = (tariffInfo: any) => setOpenModal(tariffInfo);
|
||||
const tryBuy = async ({ id, price }: { id: string, price: number }) => {
|
||||
openModalHC({})
|
||||
const tryBuy = async ({ id, price }: { id: string; price: number }) => {
|
||||
openModalHC({});
|
||||
//Если в корзине что-то было - выкладываем содержимое и запоминаем чо там лежало
|
||||
if (user.cart.length > 0) {
|
||||
outCart(user.cart)
|
||||
outCart(user.cart);
|
||||
}
|
||||
//Если нам хватает денежек - покупаем тариф
|
||||
if (price <= user.wallet.cash) {
|
||||
try {
|
||||
await makeRequest({
|
||||
method: "POST",
|
||||
url: "https://suiz.pena.digital/customer/cart/pay"
|
||||
})
|
||||
url: "https://suiz.pena.digital/customer/cart/pay",
|
||||
});
|
||||
} catch (e) {
|
||||
enqueueSnackbar("Произошла ошибка. Попробуйте позже")
|
||||
enqueueSnackbar("Произошла ошибка. Попробуйте позже");
|
||||
}
|
||||
//Развращаем товары в корзину
|
||||
inCart()
|
||||
inCart();
|
||||
} else {
|
||||
//Деняк не хватило
|
||||
navigate("https://hub.pena.digital/wallet?action=squizpay")
|
||||
// history.pushState({}, null, "https://hub.pena.digital/wallet?action=squizpay");
|
||||
var link = document.createElement('a');
|
||||
link.href = `https://hub.pena.digital/payment?action=squizpay&dif=${(price - user.wallet) * 100}`
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
}
|
||||
};
|
||||
|
||||
@ -126,12 +130,20 @@ function TariffPage() {
|
||||
p: 4,
|
||||
display: "flex",
|
||||
justifyContent: "center",
|
||||
flexDirection: "column"
|
||||
}}>
|
||||
<Typography id="modal-modal-title" variant="h6" component="h2" mb="20px">
|
||||
flexDirection: "column",
|
||||
}}
|
||||
>
|
||||
<Typography
|
||||
id="modal-modal-title"
|
||||
variant="h6"
|
||||
component="h2"
|
||||
mb="20px"
|
||||
>
|
||||
Вы подтверждаете платёж в сумму {openModal.price} ₽
|
||||
</Typography>
|
||||
<Button variant="contained" onClick={() => tryBuy(openModal)}>купить</Button>
|
||||
<Button variant="contained" onClick={() => tryBuy(openModal)}>
|
||||
купить
|
||||
</Button>
|
||||
</Paper>
|
||||
</Modal>
|
||||
</>
|
||||
@ -163,39 +175,37 @@ const LoadingPage = () => (
|
||||
);
|
||||
|
||||
const inCart = () => {
|
||||
let saveCart = JSON.parse(localStorage.getItem("saveCart") || "[]")
|
||||
let saveCart = JSON.parse(localStorage.getItem("saveCart") || "[]");
|
||||
saveCart.forEach(async (id: string) => {
|
||||
try {
|
||||
await makeRequest({
|
||||
method: "PATCH",
|
||||
url: `https://hub.pena.digital/customer/cart?id=${id}`
|
||||
})
|
||||
url: `https://hub.pena.digital/customer/cart?id=${id}`,
|
||||
});
|
||||
|
||||
let index = saveCart.indexOf('green');
|
||||
let index = saveCart.indexOf("green");
|
||||
if (index !== -1) {
|
||||
saveCart.splice(index, 1);
|
||||
}
|
||||
localStorage.setItem("saveCart", JSON.stringify(saveCart))
|
||||
localStorage.setItem("saveCart", JSON.stringify(saveCart));
|
||||
} catch (e) {
|
||||
console.log("Я не смог добавить тариф в корзину :( " + id)
|
||||
console.log("Я не смог добавить тариф в корзину :( " + id);
|
||||
}
|
||||
})
|
||||
}
|
||||
});
|
||||
};
|
||||
const outCart = (cart: string[]) => {
|
||||
//Сделаем муторно и подольше, зато при прерывании сессии данные потеряются минимально
|
||||
cart.forEach(async (id: string) => {
|
||||
try {
|
||||
await makeRequest({
|
||||
method: "DELETE",
|
||||
url: `https://suiz.pena.digital/customer/cart?id=${id}`
|
||||
})
|
||||
let saveCart = JSON.parse(localStorage.getItem("saveCart") || "[]")
|
||||
saveCart = saveCart.push(id)
|
||||
localStorage.setItem("saveCart", JSON.stringify(saveCart))
|
||||
url: `https://suiz.pena.digital/customer/cart?id=${id}`,
|
||||
});
|
||||
let saveCart = JSON.parse(localStorage.getItem("saveCart") || "[]");
|
||||
saveCart = saveCart.push(id);
|
||||
localStorage.setItem("saveCart", JSON.stringify(saveCart));
|
||||
} catch (e) {
|
||||
console.log("Я не смог удалить из корзины тариф :(")
|
||||
console.log("Я не смог удалить из корзины тариф :(");
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
});
|
||||
};
|
||||
|
||||
@ -46,7 +46,8 @@ export const createTariffElements = (
|
||||
}
|
||||
buttonProps={{
|
||||
text: "Выбрать",
|
||||
onClick: () => onclick({id: tariff._id, price: priceBeforeDiscounts / 100}),
|
||||
onClick: () =>
|
||||
onclick({ id: tariff._id, price: priceBeforeDiscounts / 100 }),
|
||||
}}
|
||||
headerText={tariff.name}
|
||||
text={tariff.privileges.map((p) => `${p.name} - ${p.amount}`)}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user