-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Tags and Categories frontend filtering
- Loading branch information
1 parent
f46379c
commit 510553b
Showing
4 changed files
with
74 additions
and
7 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
frontend/src/pages/link/components/rightPanel/CategoryItem.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import React from "react"; | ||
import { useSearchParams } from "react-router-dom"; | ||
|
||
export default function CategoryItem({ category }) { | ||
const [searchParams, setSearchParams] = useSearchParams(); | ||
let selected = searchParams.get("category") === category.name; | ||
|
||
if (searchParams.has("category")) { | ||
if (searchParams.get("category") === category.name) { | ||
searchParams.delete("category"); | ||
} else { | ||
searchParams.set("category", category.name); | ||
} | ||
} else { | ||
searchParams.append("category", category.name); | ||
} | ||
|
||
return ( | ||
<div | ||
onClick={() => setSearchParams(searchParams)} | ||
className={`hover:underline hover:text-cyan-700 cursor-pointer capitalize px-4 py-1 rounded ${ | ||
selected && "bg-cyan-400 text-white font-medium" | ||
}`} | ||
> | ||
{category.name} | ||
</div> | ||
); | ||
} |
22 changes: 22 additions & 0 deletions
22
frontend/src/pages/link/components/rightPanel/CategoryList.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import React, { useEffect } from "react"; | ||
import { useCategoriesList } from "../../../../context/categoriesList"; | ||
import CategoryItem from "./CategoryItem"; | ||
|
||
export default function CategoryList() { | ||
const { categories, fetchCategories, abort } = useCategoriesList(); | ||
|
||
useEffect(() => { | ||
fetchCategories(); | ||
return () => abort(); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, []); | ||
|
||
if (categories.length === 0) return <div>No categories</div>; | ||
return ( | ||
<div className="flex flex-col"> | ||
{categories.map((category) => ( | ||
<CategoryItem key={category.id} category={category} /> | ||
))} | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,28 @@ | ||
import React from "react"; | ||
import { Link, useSearchParams } from "react-router-dom"; | ||
import { useSearchParams } from "react-router-dom"; | ||
|
||
export default function TagItem({ tag }) { | ||
const [searchParams, setSearchParams] = useSearchParams(); | ||
let searchTag = searchParams.get("tag"); | ||
let selected = searchParams.get("tag") === tag.name; | ||
|
||
if (searchParams.has("tag")) { | ||
if (searchParams.get("tag") === tag.name) { | ||
searchParams.delete("tag"); | ||
} else { | ||
searchParams.set("tag", tag.name); | ||
} | ||
} else { | ||
searchParams.append("tag", tag.name); | ||
} | ||
|
||
return ( | ||
<Link | ||
to={`/links?tag=${tag.name}`} | ||
className={`hover:underline hover:text-cyan-700 capitalize px-4 py-1 rounded ${ | ||
tag.name === searchTag && "bg-cyan-400 text-white font-medium" | ||
<div | ||
onClick={() => setSearchParams(searchParams)} | ||
className={`hover:underline hover:text-cyan-700 cursor-pointer capitalize px-4 py-1 rounded ${ | ||
selected && "bg-cyan-400 text-white font-medium" | ||
}`} | ||
> | ||
#{tag.name} | ||
</Link> | ||
</div> | ||
); | ||
} |