diff --git a/src/routes/api/update-watchlist/+server.ts b/src/routes/api/update-watchlist/+server.ts index 24f125c8..95e609b2 100644 --- a/src/routes/api/update-watchlist/+server.ts +++ b/src/routes/api/update-watchlist/+server.ts @@ -5,32 +5,39 @@ export const POST = (async ({ request, locals }) => { const { user, pb } = locals; const data = await request.json(); - const ticker = data?.ticker; + const ticker = data?.ticker; // This can be a string (single ticker) or an array (list of tickers) const watchListId = data?.watchListId; let output; try { const watchList = await pb.collection("watchlist").getOne(watchListId); - if (watchList?.ticker?.includes(ticker)) { - // Remove ticker from the watchlist. + if (Array.isArray(ticker)) { + // If `ticker` is a list, update the watchlist directly with the new list of tickers. + output = await pb.collection("watchlist").update(watchListId, { + ticker: ticker, + }); + } else if (watchList?.ticker?.includes(ticker)) { + // Remove single ticker from the watchlist if it's already present. const newTickerList = watchList?.ticker.filter((item) => item !== ticker); output = await pb .collection("watchlist") .update(watchListId, { ticker: newTickerList }); } else { - // Add ticker to the watchlist. + // Add single ticker to the watchlist if it's not present. const newTickerList = [...watchList?.ticker, ticker]; output = await pb .collection("watchlist") .update(watchListId, { ticker: newTickerList }); } } catch (e) { - //console.log(e) + // If the watchlist doesn't exist, create a new one with either the single ticker or list. output = await pb.collection("watchlist").create( serialize({ user: user?.id, - ticker: JSON.stringify([ticker]), + ticker: Array.isArray(ticker) + ? JSON.stringify(ticker) + : JSON.stringify([ticker]), title: "Favorites", }) ); diff --git a/src/routes/watchlist/stocks/+page.svelte b/src/routes/watchlist/stocks/+page.svelte index df6833da..e1b2799f 100644 --- a/src/routes/watchlist/stocks/+page.svelte +++ b/src/routes/watchlist/stocks/+page.svelte @@ -10,10 +10,14 @@ import { Button } from "$lib/components/shadcn/button/index.js"; export let data; let searchQuery = ''; +let editMode = false; +let numberOfChecked = 0; +let deleteTickerList = []; let watchList: any[] = []; let news = []; + let allRows = [ { name: 'Volume', rule: 'volume' }, { name: 'Market Cap', rule: 'marketCap' }, @@ -136,6 +140,11 @@ async function createWatchList(event) { } +function handleDeleteModal(event) { + event?.preventDefault(); + const clicked = document.getElementById('deleteWatchlist'); + clicked.dispatchEvent(new MouseEvent('click')); +} async function deleteWatchlist(event) { event.preventDefault(); // prevent the default form submission behavior @@ -186,8 +195,64 @@ async function deleteWatchlist(event) { } } +function handleEditMode() { + if (editMode === true) { + deleteTickerList = []; + numberOfChecked = 0; + } + editMode = !editMode; +} + +async function handleFilter(symbol) { + + const filterSet = new Set(deleteTickerList); + + // Check if the new filter already exists in the list + if (filterSet?.has(symbol)) { + // If it exists, remove it from the list + filterSet?.delete(symbol); + } else { + // If it doesn't exist, add it to the list + filterSet?.add(symbol); + + } + deleteTickerList = Array?.from(filterSet); + numberOfChecked = deleteTickerList?.length; +} +async function handleDeleteTickers() { + + if (numberOfChecked === 0) { + toast.error(`You need to select symbols before you can delete them`, { + style: 'border-radius: 10px; background: #333; color: #fff; padding: 12px; margin-top: 10px; box-shadow: 0 25px 50px -12px rgb(0 0 0 / 0.25);', + }); + } + else { + + watchList = watchList?.filter(item => !deleteTickerList?.includes(item?.symbol)); + + deleteTickerList = [...deleteTickerList]; + editMode = false; + const postData = { + 'ticker': watchList?.map(item => item?.symbol), + 'watchListId': displayWatchList?.id + } + + const response = await fetch('/api/update-watchlist', { + method: 'POST', + headers: { + "Content-Type": "application/json" + }, + body: JSON.stringify(postData) + }); + + deleteTickerList = []; + numberOfChecked = 0; + + } + +} function changeWatchList(newWatchList) @@ -383,7 +448,7 @@ $: { {#each allList as item} changeWatchList(item)} class="{item?.id === displayWatchList?.id ? 'bg-[#27272A]' : ''} cursor-pointer sm:hover:bg-[#27272A]"> {item?.title} ({item?.ticker?.length}) - @@ -392,11 +457,27 @@ $: { -