Skip to content

Commit 622ab41

Browse files
Merge pull request #58 from MeetDOD/issue-38
feat: Added Dynamic search Bar in the posts section successfully issue 38
2 parents dde72e6 + 3c7cb52 commit 622ab41

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

frontend/src/pages/Posts.tsx

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const Posts = () => {
1212
const [tagInput, setTagInput] = useState('');
1313
const [filterTags, setFilterTags] = useState<string[]>([]);
1414
const filterRef = useRef<HTMLDivElement>(null);
15+
const [searchQuery, setSearchQuery] = useState('');
1516

1617
useEffect(() => {
1718
const fetchPosts = async () => {
@@ -64,11 +65,14 @@ const Posts = () => {
6465
};
6566

6667
const filteredPosts = posts.filter(post =>
67-
filterTags.every(tag => post.tags.map(t => t.toLowerCase()).includes(tag))
68+
filterTags.every(tag => post.tags.map(t => t.toLowerCase()).includes(tag)) &&
69+
(post.title.toLowerCase().includes(searchQuery.toLowerCase()) ||
70+
post.description.toLowerCase().includes(searchQuery.toLowerCase()) ||
71+
post.author.username.toLowerCase().includes(searchQuery.toLowerCase()))
6872
);
6973

7074
if (loading) {
71-
return <Loader/>;
75+
return <Loader />;
7276
}
7377

7478
if (error) {
@@ -126,10 +130,17 @@ const Posts = () => {
126130
</div>
127131
</div>
128132
)}
133+
<input
134+
type="text"
135+
value={searchQuery}
136+
onChange={(e) => setSearchQuery(e.target.value)}
137+
placeholder="🔍 Search anything"
138+
className="p-2 w-full max-w-xs rounded-md bg-gray-700 text-white focus:outline-none focus:ring-2 focus:ring-blue-500"
139+
/>
129140
</div>
130141
<div className="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 gap-4 w-full">
131-
{filteredPosts.map((post) => (
132-
<PostCard post={post} />
142+
{filteredPosts.map((post, index) => (
143+
<PostCard key={index} post={post} />
133144
))}
134145
</div>
135146
</div>

0 commit comments

Comments
 (0)