From 6897fa4c480f87482af8c8ec6ea3a05635be40cc Mon Sep 17 00:00:00 2001 From: MuslemRahimi Date: Sun, 13 Oct 2024 22:17:45 +0200 Subject: [PATCH] bugfixing --- src/routes/watchlist/stocks/+page.svelte | 58 ++++++++++++------------ 1 file changed, 30 insertions(+), 28 deletions(-) diff --git a/src/routes/watchlist/stocks/+page.svelte b/src/routes/watchlist/stocks/+page.svelte index 736f248c..83bef523 100644 --- a/src/routes/watchlist/stocks/+page.svelte +++ b/src/routes/watchlist/stocks/+page.svelte @@ -17,6 +17,7 @@ let deleteTickerList = []; let watchList: any[] = []; let news = []; +let checkedItems; let allRows = [ @@ -354,6 +355,8 @@ onMount(async () => { if (savedRules) { ruleOfList = JSON.parse(savedRules); } + checkedItems = new Set(ruleOfList.map(item => item.name)) + allRows = sortIndicatorCheckMarks(allRows) if(allList?.length !== 0) { @@ -371,7 +374,6 @@ if(allList?.length !== 0) } */ - checkedItems = new Set(ruleOfList.map(item => item.name)) isLoaded = true; }); @@ -405,12 +407,36 @@ function handleInput(event) { }, 50); } -let checkedItems; - function isChecked(item) { return checkedItems?.has(item); } +function sortIndicatorCheckMarks(allRows) { + return allRows.sort((a, b) => { + const isAChecked = checkedItems.has(a.name); + const isBChecked = checkedItems.has(b.name); + + // Sort checked items first + if (isAChecked !== isBChecked) return isAChecked ? -1 : 1; + + // Check if the user is not Pro + if (data?.user?.tier !== 'Pro') { + const isAPriority = priorityItems.has(a.name); + const isBPriority = priorityItems.has(b.name); + + // If both are priority items or both are not, sort alphabetically + if (isAPriority === isBPriority) return a.name.localeCompare(b.name); + + // Move priority items to the bottom for non-Pro users + return isAPriority ? 1 : -1; + } + + // If the user is Pro, sort alphabetically + return a.name.localeCompare(b.name); + }); +} + + async function handleChangeValue(value) { if (checkedItems.has(value)) { checkedItems.delete(value); // Remove the value if it's already in the Set @@ -424,31 +450,7 @@ async function handleChangeValue(value) { const priorityItems = new Set(['AI Score', 'Revenue', 'Net Income', 'Free Cash Flow']); -allRows = allRows - ?.filter(item => checkedItems.has(item.name) || !checkedItems.has(item.name)) - ?.sort((a, b) => { - const isAChecked = checkedItems.has(a.name); - const isBChecked = checkedItems.has(b.name); - - // Sort checked items first - if (isAChecked !== isBChecked) return isAChecked ? -1 : 1; - - // Check if the user is not Pro - if (data?.user?.tier !== 'Pro') { - // Check if both items are priority items - const isAPriority = priorityItems.has(a.name); - const isBPriority = priorityItems.has(b.name); - - // If both are priority items or both are not, sort alphabetically - if (isAPriority === isBPriority) return a.name.localeCompare(b.name); - - // Move priority items to the bottom - return isAPriority ? 1 : -1; - } - - // If the user is Pro, sort alphabetically - return a.name.localeCompare(b.name); - }); +allRows = sortIndicatorCheckMarks(allRows) saveRules() }