This commit is contained in:
MuslemRahimi 2025-04-13 14:41:18 +02:00
parent 5d5ac25457
commit adabaf1f3f

View File

@ -1079,24 +1079,38 @@
} }
if (data?.user?.credits > totalCreditCost && tickers?.length > 0) { if (data?.user?.credits > totalCreditCost && tickers?.length > 0) {
data.user.credits = data?.user?.credits - totalCreditCost; toast.promise(
const response = await fetch("/api/bulk-download", { (async () => {
method: "POST", data.user.credits = data.user.credits - totalCreditCost;
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ tickers: tickers, bulkData: bulkData }),
});
if (response.ok) { const response = await fetch("/api/bulk-download", {
const blob = await response.blob(); method: "POST",
const url = URL.createObjectURL(blob); headers: { "Content-Type": "application/json" },
const a = document.createElement("a"); body: JSON.stringify({ tickers: tickers, bulkData: bulkData }),
a.href = url; });
a.download = "historical_data.zip";
document.body.appendChild(a); if (!response.ok) {
a.click(); throw new Error("Download request failed");
a.remove(); }
URL.revokeObjectURL(url);
} const blob = await response.blob();
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = "historical_data.zip";
document.body.appendChild(a);
a.click();
a.remove();
URL.revokeObjectURL(url);
})(),
{
loading: "Downloading data...",
success: "Download complete!",
error: "Download failed. Try again.",
style: `border-radius: 5px; background: #fff; color: #000; border-color: ${$mode === "light" ? "#F9FAFB" : "#4B5563"}; font-size: 15px;`,
},
);
} else if (tickers?.length === 0) { } else if (tickers?.length === 0) {
toast.error("Add tickers first to your watchlist", { toast.error("Add tickers first to your watchlist", {
style: `border-radius: 5px; background: #fff; color: #000; border-color: ${$mode === "light" ? "#F9FAFB" : "#4B5563"}; font-size: 15px;`, style: `border-radius: 5px; background: #fff; color: #000; border-color: ${$mode === "light" ? "#F9FAFB" : "#4B5563"}; font-size: 15px;`,