Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix project summary search all #1005

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 36 additions & 13 deletions src/frontend/src/views/Home.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import ProjectListMap from '../components/home/ProjectListMap';

const Home = () => {
const [searchQuery, setSearchQuery] = useState('');
const [debouncedSearch, setDebouncedSearch] = useState('');
const [paginationPage, setPaginationPage] = useState(1);

const defaultTheme = CoreModules.useAppSelector((state) => state.theme.hotTheme);
const showMapStatus = CoreModules.useAppSelector((state) => state.home.showMapStatus);
Expand All @@ -27,26 +29,51 @@ const Home = () => {

const stateHome = CoreModules.useAppSelector((state) => state.home);
//we use use selector from redux to get all state of home from home slice
const filteredProjectCards = stateHome.homeProjectSummary;

let cardsPerRow = new Array(
type == 'xl' ? 7 : type == 'lg' ? 5 : type == 'md' ? 4 : type == 'sm' ? 3 : type == 's' ? 2 : 1,
).fill(0);
//calculating number of cards to to display per row in order to fit our window dimension respectively and then convert it into dummy array

const theme = CoreModules.useAppSelector((state) => state.theme.hotTheme);
useEffect(() => {
// dispatch(HomeSummaryService(`${import.meta.env.VITE_API_URL}/projects/summaries?skip=0&limit=100`));
dispatch(HomeSummaryService(`${import.meta.env.VITE_API_URL}/projects/summaries?page=1&results_per_page=12`));
//creating a manual thunk that will make an API call then autamatically perform state mutation whenever we navigate to home page
}, []);

const handleSearch = (query) => {
setSearchQuery(query);
};

const filteredProjectCards = stateHome.homeProjectSummary.filter((value) =>
value.title.toLowerCase().includes(searchQuery.toLowerCase()),
);
useEffect(() => {
const timeoutId = setTimeout(() => {
setDebouncedSearch(searchQuery);
}, 500);
return () => clearTimeout(timeoutId);
}, [searchQuery, 500]);

useEffect(() => {
if (debouncedSearch) {
dispatch(
HomeSummaryService(
`${
import.meta.env.VITE_API_URL
}/projects/search_projects?page=${paginationPage}&results_per_page=12&search=${debouncedSearch}`,
),
);
}
}, [debouncedSearch, paginationPage]);

useEffect(() => {
setPaginationPage(1);
}, [debouncedSearch]);

useEffect(() => {
if (!debouncedSearch) {
dispatch(
HomeSummaryService(
`${import.meta.env.VITE_API_URL}/projects/summaries?page=${paginationPage}&results_per_page=12`,
),
);
}
}, [paginationPage, debouncedSearch]);

return (
<div
Expand Down Expand Up @@ -90,11 +117,7 @@ const Home = () => {
},
}}
onChange={(e, page) => {
dispatch(
HomeSummaryService(
`${import.meta.env.VITE_API_URL}/projects/summaries?page=${page}&results_per_page=12`,
),
);
setPaginationPage(page);
}}
/>
</div>
Expand Down