From 695b42b0992c313ee4d14ef5b5c0c3d46a73f33a Mon Sep 17 00:00:00 2001 From: MuslemRahimi Date: Fri, 29 Nov 2024 12:04:45 +0100 Subject: [PATCH] bugfixing --- src/lib/utils.ts | 78 +++++----- src/routes/watchlist/stocks/+page.svelte | 186 +++++++++++++---------- 2 files changed, 149 insertions(+), 115 deletions(-) diff --git a/src/lib/utils.ts b/src/lib/utils.ts index 84c111fd..79f8a695 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -19,44 +19,50 @@ type FlyAndScaleParams = { }; -export const groupNews = (news, watchList) => { - return Object.entries( - news - ?.map(item => { - // Add 'type' based on watchList - const match = watchList?.find(w => w?.symbol === item?.symbol); - return match ? { ...item, type: match.type } : { ...item }; - }) - ?.reduce((acc, item) => { - const dateKey = new Intl.DateTimeFormat('en-US', { - day: '2-digit', - month: 'short', - year: 'numeric', - }).format(new Date(item?.publishedDate)); - - const titleKey = item.title; - - if (!acc[dateKey]) acc[dateKey] = {}; - if (!acc[dateKey][titleKey]) acc[dateKey][titleKey] = []; - - acc[dateKey][titleKey]?.push(item); - - return acc; - }, {}) - )?.map(([date, titleGroup]) => [ +export const groupNews = (news, watchList) => { + return Object.entries( + news + ?.map(item => { + // Add 'type' based on watchList + const match = watchList?.find(w => w?.symbol === item?.symbol); + return match ? { ...item, type: match.type } : { ...item }; + }) + ?.reduce((acc, item) => { + const dateKey = new Intl.DateTimeFormat('en-US', { + day: '2-digit', + month: 'short', + year: 'numeric', + }).format(new Date(item?.publishedDate)); + const titleKey = item.title; + if (!acc[dateKey]) acc[dateKey] = {}; + if (!acc[dateKey][titleKey]) acc[dateKey][titleKey] = []; + acc[dateKey][titleKey]?.push(item); + return acc; + }, {}) + ) + // Sort the grouped dates in descending order to have the latest date first + ?.sort(([dateA], [dateB]) => new Date(dateB) - new Date(dateA)) + ?.map(([date, titleGroup]) => [ date, - Object?.entries(titleGroup)?.map(([title, items]) => { - // Sort by time for each group - items?.sort((a, b) => new Date(b?.publishedDate) - new Date(a?.publishedDate)); - - // Get the unique symbols - const symbols = [...new Set(items?.map(item => item.symbol))]; - - // Return the group with symbols and items - return { title, items, symbols }; - }), + Object.entries(titleGroup) + // Sort titles by the latest time of their items + .sort(([, itemsA], [, itemsB]) => { + const latestTimeA = new Date(Math.max(...itemsA.map(item => new Date(item.publishedDate)))); + const latestTimeB = new Date(Math.max(...itemsB.map(item => new Date(item.publishedDate)))); + return latestTimeB - latestTimeA; + }) + .map(([title, items]) => { + // Sort items within each title group by latest time + items.sort((a, b) => new Date(b.publishedDate) - new Date(a.publishedDate)); + + // Get the unique symbols + const symbols = [...new Set(items.map(item => item.symbol))]; + + // Return the group with symbols and items + return { title, items, symbols }; + }), ]); - } +}; export const calculateChange = (oldList?: any[], newList?: any[]) => { diff --git a/src/routes/watchlist/stocks/+page.svelte b/src/routes/watchlist/stocks/+page.svelte index 35695d88..ca03a567 100644 --- a/src/routes/watchlist/stocks/+page.svelte +++ b/src/routes/watchlist/stocks/+page.svelte @@ -16,15 +16,26 @@ let switchWatchlist = false; let editMode = false; let numberOfChecked = 0; + let activeIdx = 0; + let deleteTickerList = []; let watchList: any[] = []; let news = []; - let groupedNews = {}; + let groupedNews = []; let checkedItems; let socket; + const tabs = [ + { + title: "News", + }, + { + title: "Earnings", + }, + ]; + let allRows = [ { name: "Volume", rule: "volume", type: "int" }, { name: "Avg. Volume", rule: "avgVolume", type: "int" }, @@ -219,6 +230,7 @@ return match ? { ...item, type: match?.type } : { ...item }; }); groupedNews = groupNews(news, watchList); + console.log(groupedNews); } async function createWatchList(event) { @@ -1333,92 +1345,108 @@ >
-
-

+
- News -

- + {#each tabs as item, i} + + {/each} +
- {#each groupedNews as [date, titleGroups]} -

- {date} -

-
- {#each titleGroups as { title, items, symbols }} -
- -
- -

- {title} -

-
+ {#if groupedNews?.length > 0} + {#each groupedNews as [date, titleGroups]} +

+ {date} +

+
+ {#each titleGroups as { title, items, symbols }} +
-
- {new Date( - items[0].publishedDate, - ).toLocaleTimeString("en-US", { - hour: "2-digit", - minute: "2-digit", - hour12: true, - })} -
-
{items[0].site}
- · -
- {#each symbols as symbol} - - {symbol} - - {/each} + {new Date( + items[0].publishedDate, + ).toLocaleTimeString("en-US", { + hour: "2-digit", + minute: "2-digit", + hour12: true, + })} +
+
+ +

+ {title} +

+
+
+
+ {new Date( + items[0].publishedDate, + ).toLocaleTimeString("en-US", { + hour: "2-digit", + minute: "2-digit", + hour12: true, + })} +
+
{items[0].site}
+ · +
+ {#each symbols as symbol} + + {symbol} + + {/each} +
-
- {/each} -
- {/each} + {/each} +
+ {/each} + {:else} + + No news yet. Add some stocks to the watchlist to see the + latest news. + + {/if}
{:else}