From 6eec4e0c38bb32179b5570c74f262d14c0086c46 Mon Sep 17 00:00:00 2001 From: TZGyn Date: Thu, 31 Aug 2023 02:23:55 +0800 Subject: [PATCH] New feature: dropdown from navbar to change tab --- app/providers.tsx | 46 ++++++++++++++++++++++++++++++------------- components/navbar.tsx | 28 ++++++++++++++++++++++++++ components/tabs.tsx | 12 +++++++++-- 3 files changed, 70 insertions(+), 16 deletions(-) diff --git a/app/providers.tsx b/app/providers.tsx index 0c234cf..ff753ba 100644 --- a/app/providers.tsx +++ b/app/providers.tsx @@ -42,6 +42,17 @@ export const BookmarkContext = React.createContext({ setBookmark: () => {}, }) +export type BookmarkCategoryContextContent = { + selectedCategory: string + setSelectedCategory: (category: string) => void +} + +export const BookmarkCategoryContext = + React.createContext({ + selectedCategory: '', + setSelectedCategory: () => {}, + }) + export function Providers({ children, themeProps }: ProvidersProps) { // const [isEditBookmark, setIsEditBookmark] = React.useState(false) const { @@ -57,24 +68,31 @@ export function Providers({ children, themeProps }: ProvidersProps) { url: '', category_id: 2, }) + const [selectedCategory, setSelectedCategory] = React.useState('') return ( - - - - - {children} - - - - + + + + {children} + + + + + ) } diff --git a/components/navbar.tsx b/components/navbar.tsx index 075c4e2..39ef47e 100644 --- a/components/navbar.tsx +++ b/components/navbar.tsx @@ -31,6 +31,8 @@ import { Icon } from '@iconify/react' import NewBookmarkForm from '@/components/newBookmarkForm' import { usePathname, useRouter } from 'next/navigation' import { BookmarkCategory, User } from '@/types' +import { useContext } from 'react' +import { BookmarkCategoryContext } from '@/app/providers' export const Navbar = ({ user, @@ -45,6 +47,8 @@ export const Navbar = ({ return <> } + const { setSelectedCategory } = useContext(BookmarkCategoryContext) + return ( ))} + + + Category + + {categories.length > 0 ? ( + categories.map((category) => ( + + setSelectedCategory( + category.id.toString() + ) + }> + {category.name} + + )) + ) : ( + + Nothing Here + + )} + + + diff --git a/components/tabs.tsx b/components/tabs.tsx index b3a3a0d..aa1e4b1 100644 --- a/components/tabs.tsx +++ b/components/tabs.tsx @@ -3,20 +3,28 @@ import { BookmarkCategoryWithBookmarks } from '@/types' import { Tabs, Tab } from '@nextui-org/tabs' import { BookmarkCard } from './bookmarkCard' +import { useContext, useState } from 'react' +import { BookmarkCategoryContext } from '@/app/providers' export default function BookmarkTabs({ data, }: { data: BookmarkCategoryWithBookmarks[] }) { + const { selectedCategory, setSelectedCategory } = useContext( + BookmarkCategoryContext + ) + return (
setSelectedCategory(key.toString())} color='primary'> - {data.map((bookmarkCategory, index) => ( + {data.map((bookmarkCategory) => (
{bookmarkCategory.bookmark.map((data, index) => (