Skip to content

Commit

Permalink
fixed forum filter bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
vanesssalai committed Aug 6, 2024
1 parent 7d3693c commit fc9bf60
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
30 changes: 18 additions & 12 deletions src/pages/forum/Forum.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import ForumList from '../../components/forumcards/ForumList';
import './Forum.css';

function ForumPage() {
const { currentUser } = useAuth();
const [forumPosts, setForumPosts] = useState([]);
const [filteredPosts, setFilteredPosts] = useState([]);
const [sortOrder, setSortOrder] = useState('');
const [searchQuery, setSearchQuery] = useState('');

const fetchForumPosts = async () => {
try {
Expand Down Expand Up @@ -59,20 +60,25 @@ function ForumPage() {
return new Date(now.setDate(diff)).setHours(0, 0, 0, 0);
};

const handleFilterChange = useCallback(({ tags, sortOrder, searchQuery }) => {
const calculateFeaturedScore = (product) => {
const { weeklyClicks, likes, comments } = product;
return (weeklyClicks * 0.5) + (likes * 0.3) + (comments * 0.2);
};

const filterAndSortPosts = useCallback(({ tags, sortOrder, searchQuery }) => {
let filtered = forumPosts;

if (tags.length > 0) {
if (tags && tags.length > 0) {
filtered = filtered.filter(post =>
tags.some(tag => post.tags.includes(tag))
);
}

if (searchQuery) {
const lowerCaseQuery = searchQuery.toLowerCase();
const lowerQuery = searchQuery.toLowerCase();
filtered = filtered.filter(post =>
post.title.toLowerCase().includes(lowerCaseQuery) ||
post.description.toLowerCase().includes(lowerCaseQuery)
post.title.toLowerCase().includes(lowerQuery) ||
post.description.toLowerCase().includes(lowerQuery)
);
}

Expand All @@ -83,17 +89,17 @@ function ForumPage() {
}

setFilteredPosts(filtered);
setSortOrder(sortOrder);
setSearchQuery(searchQuery);
}, [forumPosts]);

const calculateFeaturedScore = (product) => {
const { weeklyClicks, likes, comments } = product;
return (weeklyClicks * 0.5) + (likes * 0.3) + (comments * 0.2);
};

return (
<Format content={
<div className='forum'>
<ForumFilter onFilterChange={handleFilterChange} />
<ForumFilter
onFilterChange={filterAndSortPosts}
onSortChange={(order) => filterAndSortPosts({ tags: [], sortOrder: order, searchQuery })}
/>
<div className='forum-main'>
<ForumList heading="Forum" forums={filteredPosts} />
</div>
Expand Down
13 changes: 5 additions & 8 deletions src/pages/forum/filter/ForumFilter.jsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import React, { useState, useEffect } from 'react';
import { useNavigate } from 'react-router-dom';
import { useAuth } from '../../../Auth';

import SearchBar from '../../../components/searchbar/Searchbar';
import './ForumFilter.css';

function ForumFilter({ onFilterChange }) {
const navigate = useNavigate();
const { currentUser } = useAuth();
function ForumFilter({ onFilterChange, onSortChange }) {
const [selectedTags, setSelectedTags] = useState([]);
const [sortOrder, setSortOrder] = useState('');
const [query, setQuery] = useState('');
Expand All @@ -24,11 +21,11 @@ function ForumFilter({ onFilterChange }) {

const handleSortChange = (e) => {
setSortOrder(e.target.value);
onSortChange(e.target.value);
};

const handleInputChange = (e) => {
const value = e.target.value;
setQuery(value);
setQuery(e.target.value);
};

const handleSearch = (e) => {
Expand All @@ -38,7 +35,7 @@ function ForumFilter({ onFilterChange }) {

useEffect(() => {
onFilterChange({ tags: selectedTags, sortOrder, searchQuery: query });
}, [selectedTags, sortOrder, query]);
}, [selectedTags, sortOrder, query, onFilterChange]);

return (
<div className='forum-filter'>
Expand Down Expand Up @@ -68,7 +65,7 @@ function ForumFilter({ onFilterChange }) {
</select>
</div>
</div>
<div className='forum-searchbar'>
<div className='forum-searchbar'>
<SearchBar
placeholder={"Search Forum Posts..."}
handleSearch={handleSearch}
Expand Down

0 comments on commit fc9bf60

Please sign in to comment.