From a16420b93273ec1c895ab0ca5baf67899dca3554 Mon Sep 17 00:00:00 2001 From: VividTruthKeeper Date: Sat, 10 Sep 2022 16:57:22 +0500 Subject: [PATCH] dynamic categories --- src/pages/Posts.tsx | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/src/pages/Posts.tsx b/src/pages/Posts.tsx index b7f8e89..590e03a 100644 --- a/src/pages/Posts.tsx +++ b/src/pages/Posts.tsx @@ -24,12 +24,24 @@ const headers: string[] = [ ]; const Posts = () => { + const [categories, setCategories] = useState(["All"]); const { posts, setPosts } = useContext(PostContext); const [category, setCategory] = useState("All"); useEffect(() => { getPosts(setPosts); }, []); + useEffect(() => { + const categoriesTemp: string[] = categories; + if (posts[0].id !== -1) { + posts.map((post: PostType) => categoriesTemp.push(post.category)); + let categoriesTempUnique = categoriesTemp.filter((element, index) => { + return categoriesTemp.indexOf(element) === index; + }); + setCategories(categoriesTempUnique); + } + }, [posts]); + return (
@@ -45,11 +57,21 @@ const Posts = () => { setCategory(e.target.value); }} > - - - + {categories.map((category) => { + if (category === "All") { + return ( + + ); + } else { + return ( + + ); + } + })}