dynamic categories
This commit is contained in:
parent
ba98c4d859
commit
a16420b932
|
|
@ -24,12 +24,24 @@ const headers: string[] = [
|
||||||
];
|
];
|
||||||
|
|
||||||
const Posts = () => {
|
const Posts = () => {
|
||||||
|
const [categories, setCategories] = useState<string[]>(["All"]);
|
||||||
const { posts, setPosts } = useContext<any>(PostContext);
|
const { posts, setPosts } = useContext<any>(PostContext);
|
||||||
const [category, setCategory] = useState<string>("All");
|
const [category, setCategory] = useState<string>("All");
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
getPosts(setPosts);
|
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 (
|
return (
|
||||||
<main className="posts">
|
<main className="posts">
|
||||||
<div className="container">
|
<div className="container">
|
||||||
|
|
@ -45,11 +57,21 @@ const Posts = () => {
|
||||||
setCategory(e.target.value);
|
setCategory(e.target.value);
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<option value="All" defaultChecked>
|
{categories.map((category) => {
|
||||||
All
|
if (category === "All") {
|
||||||
</option>
|
return (
|
||||||
<option value="TurkmenPortal">TurkmenPortal</option>
|
<option key={uuidv4()} value={category} defaultChecked>
|
||||||
<option value="Orient">Orient</option>
|
{category}
|
||||||
|
</option>
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
return (
|
||||||
|
<option key={uuidv4()} value={category}>
|
||||||
|
{category}
|
||||||
|
</option>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
})}
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
<table className="posts__table">
|
<table className="posts__table">
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue