From ecb7b6de3a6728d457df894c1cc86e440fcebf68 Mon Sep 17 00:00:00 2001 From: Jackson Harper Date: Thu, 13 Jun 2024 21:17:03 +0800 Subject: [PATCH] Initial work on action items, need to test with larger data sets --- .../web/components/nav-containers/home.tsx | 109 ++++++++++++++---- 1 file changed, 88 insertions(+), 21 deletions(-) diff --git a/packages/web/components/nav-containers/home.tsx b/packages/web/components/nav-containers/home.tsx index a20efbe0c..09f435cc9 100644 --- a/packages/web/components/nav-containers/home.tsx +++ b/packages/web/components/nav-containers/home.tsx @@ -1,7 +1,7 @@ import * as HoverCard from '@radix-ui/react-hover-card' import { styled } from '@stitches/react' import { useRouter } from 'next/router' -import { useMemo, useState } from 'react' +import { useCallback, useEffect, useMemo, useReducer, useState } from 'react' import { Button } from '../elements/Button' import { AddToLibraryActionIcon } from '../elements/icons/home/AddToLibraryActionIcon' import { ArchiveActionIcon } from '../elements/icons/home/ArchiveActionIcon' @@ -198,6 +198,41 @@ const JustAddedHomeSection = (props: HomeSectionProps): JSX.Element => { } const TopPicksHomeSection = (props: HomeSectionProps): JSX.Element => { + const listReducer = ( + state: HomeItem[], + action: { + type: string + itemId?: string + items?: HomeItem[] + } + ) => { + console.log('handling action: ', action) + switch (action.type) { + case 'RESET': + return action.items ?? [] + case 'REMOVE_ITEM': + return state.filter((item) => item.id !== action.itemId) + default: + throw new Error() + } + } + + const [items, dispatchList] = useReducer(listReducer, []) + + function handleDelete(item: HomeItem) { + dispatchList({ + type: 'REMOVE_ITEM', + itemId: item.id, + }) + } + + useEffect(() => { + dispatchList({ + type: 'RESET', + items: props.homeSection.items, + }) + }, [props]) + return ( { ( - + )} /> @@ -511,9 +550,14 @@ const JustAddedItemView = (props: HomeItemViewProps): JSX.Element => { ) } -const TopicPickHomeItemView = (props: HomeItemViewProps): JSX.Element => { - const router = useRouter() +type TopPickItemViewProps = { + dispatchList: (args: { type: string; itemId?: string }) => void +} +const TopicPickHomeItemView = ( + props: HomeItemViewProps & TopPickItemViewProps +): JSX.Element => { + const router = useRouter() return ( { - - + )} + {/* - - - + */} + + {props.homeItem.canArchive && ( + + )} + {props.homeItem.canDelete && ( + + )} + {props.homeItem.canShare && ( + + )}