bugfixing watchlist

This commit is contained in:
MuslemRahimi 2024-12-02 19:29:22 +01:00
parent b9bf1a39b2
commit bde8718cf2

View File

@ -25,6 +25,7 @@
let editMode = false; let editMode = false;
let numberOfChecked = 0; let numberOfChecked = 0;
let activeIdx = 0; let activeIdx = 0;
let rawTabData = [];
let deleteTickerList = []; let deleteTickerList = [];
@ -34,6 +35,7 @@
let earnings = []; let earnings = [];
let groupedNews = []; let groupedNews = [];
let groupedEarnings = []; let groupedEarnings = [];
let displayList = [];
let checkedItems; let checkedItems;
let socket; let socket;
@ -253,6 +255,7 @@
groupedEarnings = []; groupedEarnings = [];
groupedNews = []; groupedNews = [];
} }
changeTab(0);
} }
async function createWatchList(event) { async function createWatchList(event) {
@ -291,6 +294,17 @@
body: JSON.stringify(postData), body: JSON.stringify(postData),
}); // make a POST request to the server with the FormData object }); // make a POST request to the server with the FormData object
if (response?.ok) { if (response?.ok) {
const output = await response.json();
try {
// Save the version along with the rules
localStorage?.setItem(
"last-watchlist-id",
JSON?.stringify(output?.id),
);
} catch (e) {
console.log("Failed saving indicator rules: ", e);
}
toast.success("Watchlist created successfully!", { toast.success("Watchlist created successfully!", {
style: "border-radius: 200px; background: #333; color: #fff;", style: "border-radius: 200px; background: #333; color: #fff;",
}); });
@ -438,6 +452,15 @@
} }
} }
function changeTab(i) {
activeIdx = i;
if (activeIdx === 0) {
rawTabData = groupedNews;
} else {
rawTabData = groupedEarnings;
}
displayList = rawTabData?.slice(0, 8);
}
async function handleAddTicker(event, ticker) { async function handleAddTicker(event, ticker) {
// Ensure inputValue is reset // Ensure inputValue is reset
@ -524,6 +547,15 @@
function changeWatchList(newWatchList) { function changeWatchList(newWatchList) {
displayWatchList = newWatchList; displayWatchList = newWatchList;
switchWatchlist = true; switchWatchlist = true;
try {
// Save the version along with the rules
localStorage?.setItem(
"last-watchlist-id",
JSON?.stringify(displayWatchList?.id),
);
} catch (e) {
console.log("Failed saving indicator rules: ", e);
}
} }
function saveRules() { function saveRules() {
@ -538,9 +570,20 @@
} }
} }
async function handleScroll() {
const scrollThreshold = document.body.offsetHeight * 0.8; // 80% of the website height
const isBottom = window.innerHeight + window.scrollY >= scrollThreshold;
if (isBottom && displayList?.length !== rawTabData?.length) {
const nextIndex = displayList?.length;
const filteredItem = rawTabData?.slice(nextIndex, nextIndex + 8);
displayList = [...displayList, ...filteredItem];
}
}
onMount(async () => { onMount(async () => {
try { try {
const savedRules = localStorage?.getItem("watchlist-ruleOfList"); const savedRules = localStorage?.getItem("watchlist-ruleOfList");
const savedLastWatchlistId = localStorage?.getItem("last-watchlist-id");
if (savedRules) { if (savedRules) {
const parsedRules = JSON.parse(savedRules); const parsedRules = JSON.parse(savedRules);
@ -578,11 +621,17 @@
checkedItems = new Set(ruleOfList.map((item) => item.name)); checkedItems = new Set(ruleOfList.map((item) => item.name));
allRows = sortIndicatorCheckMarks(allRows); allRows = sortIndicatorCheckMarks(allRows);
// Display the first watchlist if available if (savedLastWatchlistId) {
if (allList?.length !== 0) { displayWatchList = allList
displayWatchList = allList?.at(0); ?.filter((item) => item?.id === JSON.parse(savedLastWatchlistId))
?.at(0);
} else { } else {
displayWatchList = ""; // Display the first watchlist if available
if (allList?.length !== 0) {
displayWatchList = allList?.at(0);
} else {
displayWatchList = "";
}
} }
await getWatchlistData(); await getWatchlistData();
@ -602,10 +651,16 @@
if ($isOpen) { if ($isOpen) {
await websocketRealtimeData(); await websocketRealtimeData();
} }
window.addEventListener("scroll", handleScroll);
return () => {
window.removeEventListener("scroll", handleScroll);
};
}); });
let previousList = []; let previousList = [];
let reconnectionTimeout; let reconnectionTimeout;
afterUpdate(async () => { afterUpdate(async () => {
// Compare only the symbols to detect changes // Compare only the symbols to detect changes
const currentSymbols = watchList?.map((item) => item?.symbol).sort(); const currentSymbols = watchList?.map((item) => item?.symbol).sort();
@ -1404,7 +1459,7 @@
</button> </button>
{:else} {:else}
<button <button
on:click={() => (activeIdx = i)} on:click={() => changeTab(i)}
class="group relative z-[1] rounded-full w-1/2 min-w-24 md:w-auto px-5 py-1 {activeIdx === class="group relative z-[1] rounded-full w-1/2 min-w-24 md:w-auto px-5 py-1 {activeIdx ===
i i
? 'z-0' ? 'z-0'
@ -1430,7 +1485,7 @@
</div> </div>
{#if activeIdx === 0} {#if activeIdx === 0}
{#if groupedNews?.length > 0} {#if groupedNews?.length > 0}
{#each groupedNews as [date, titleGroups]} {#each displayList as [date, titleGroups]}
<h3 class="mb-1.5 mt-3 font-semibold text-faded"> <h3 class="mb-1.5 mt-3 font-semibold text-faded">
{date} {date}
</h3> </h3>
@ -1502,7 +1557,7 @@
</span> </span>
{/if} {/if}
{:else if groupedEarnings?.length > 0} {:else if groupedEarnings?.length > 0}
{#each groupedEarnings as [date, titleGroups]} {#each displayList as [date, titleGroups]}
<h3 class="mb-1.5 mt-3 font-semibold text-faded"> <h3 class="mb-1.5 mt-3 font-semibold text-faded">
{date} {date}
</h3> </h3>