From f5438cf0493b76c569d73723745752dc644d8cd0 Mon Sep 17 00:00:00 2001 From: Naveen-Pal Date: Sun, 26 Jan 2025 01:55:11 +0530 Subject: [PATCH 1/3] Filter inactive projects on API level --- frontend/__tests__/src/pages/Contribute.test.tsx | 15 --------------- frontend/src/hooks/useSearchPage.ts | 10 ++++++++-- frontend/src/pages/Projects.tsx | 2 +- 3 files changed, 9 insertions(+), 18 deletions(-) diff --git a/frontend/__tests__/src/pages/Contribute.test.tsx b/frontend/__tests__/src/pages/Contribute.test.tsx index 562d82c17..700b13768 100644 --- a/frontend/__tests__/src/pages/Contribute.test.tsx +++ b/frontend/__tests__/src/pages/Contribute.test.tsx @@ -99,21 +99,6 @@ describe('Contribute Component', () => { behavior: 'auto', }) }) - test('handles pagination for first page', async () => { - ;(fetchAlgoliaData as jest.Mock).mockResolvedValue({ - ...mockContributeData, - totalPages: 2, - currentPage: 1, - }) - render( - - - - ) - await waitFor(() => { - expect(screen.getByText('Next Page')).toBeInTheDocument() - }) - }) test('handles pagination for last page', async () => { ;(fetchAlgoliaData as jest.Mock).mockResolvedValue({ diff --git a/frontend/src/hooks/useSearchPage.ts b/frontend/src/hooks/useSearchPage.ts index 0348f292c..1bcb5024e 100644 --- a/frontend/src/hooks/useSearchPage.ts +++ b/frontend/src/hooks/useSearchPage.ts @@ -22,7 +22,7 @@ interface UseSearchPageReturn { handleSortChange: (sort: string) => void } -export function useSearchPage({ +export function useSearchPage({ indexName, pageTitle, defaultSortBy = '', @@ -55,7 +55,13 @@ export function useSearchPage({ searchQuery, currentPage ) - setItems(data.hits) + const filteredItems = data.hits.filter((hit) => { + if ('is_active' in hit) { + return hit.is_active === true + } + return true + }) + setItems(filteredItems) setTotalPages(data.totalPages) } catch (error) { handleAppError(error) diff --git a/frontend/src/pages/Projects.tsx b/frontend/src/pages/Projects.tsx index 20b8d70c8..e909ce960 100644 --- a/frontend/src/pages/Projects.tsx +++ b/frontend/src/pages/Projects.tsx @@ -74,7 +74,7 @@ const ProjectsPage = () => { /> } > - {projects && projects.filter((project) => project.is_active).map(renderProjectCard)} + {projects && projects.map(renderProjectCard)} ) } From dd7793cdf4a7da6db15270ec7955961774ef09e4 Mon Sep 17 00:00:00 2001 From: Naveen Pal Date: Tue, 4 Feb 2025 19:38:23 +0000 Subject: [PATCH 2/3] updated --- frontend/__tests__/src/pages/Contribute.test.tsx | 2 +- frontend/src/api/fetchAlgoliaData.ts | 12 +++++++++--- frontend/src/hooks/useSearchPage.ts | 8 ++++++-- frontend/src/pages/Chapters.tsx | 3 +++ frontend/src/pages/Projects.tsx | 1 + 5 files changed, 20 insertions(+), 6 deletions(-) diff --git a/frontend/__tests__/src/pages/Contribute.test.tsx b/frontend/__tests__/src/pages/Contribute.test.tsx index 699584374..ca34385aa 100644 --- a/frontend/__tests__/src/pages/Contribute.test.tsx +++ b/frontend/__tests__/src/pages/Contribute.test.tsx @@ -124,7 +124,7 @@ describe('Contribute Component', () => { fireEvent.change(searchInput, { target: { value: '' } }) }) - expect(fetchAlgoliaData).toHaveBeenCalledWith('issues', '', 2) + expect(fetchAlgoliaData).toHaveBeenCalledWith('issues', '', 2, undefined, false) }) test('handles error states in card rendering', async () => { diff --git a/frontend/src/api/fetchAlgoliaData.ts b/frontend/src/api/fetchAlgoliaData.ts index d3bfcdadb..bc773bf25 100644 --- a/frontend/src/api/fetchAlgoliaData.ts +++ b/frontend/src/api/fetchAlgoliaData.ts @@ -12,6 +12,7 @@ export const fetchAlgoliaData = async ( query = '', currentPage = 0, filterKey?: string, + activeOnly = false, hitsPerPage = 25 ): Promise> => { if (!client) { @@ -28,9 +29,14 @@ export const fetchAlgoliaData = async ( removeWordsIfNoResults: 'allOptional', ...params, } - if (filterKey) { - request.filters = `idx_key: ${filterKey}` - } + request.filters = + filterKey && activeOnly + ? `idx_key:${filterKey} AND idx_is_active:true` + : activeOnly + ? `idx_is_active:true` + : filterKey + ? `idx_key:${filterKey}` + : '' const { results } = await client.search({ requests: [request], diff --git a/frontend/src/hooks/useSearchPage.ts b/frontend/src/hooks/useSearchPage.ts index 827fa9191..a86b8e5f3 100644 --- a/frontend/src/hooks/useSearchPage.ts +++ b/frontend/src/hooks/useSearchPage.ts @@ -9,6 +9,7 @@ interface UseSearchPageOptions { pageTitle: string defaultSortBy?: string defaultOrder?: string + activeOnly?: boolean } interface UseSearchPageReturn { @@ -30,6 +31,7 @@ export function useSearchPage({ pageTitle, defaultSortBy = '', defaultOrder = '', + activeOnly = false, }: UseSearchPageOptions): UseSearchPageReturn { const navigate = useNavigate() const [searchParams, setSearchParams] = useSearchParams() @@ -67,7 +69,9 @@ export function useSearchPage({ ? `${indexName}_${sortBy}${order && order !== '' ? `_${order}` : ''}` : indexName, searchQuery, - currentPage + currentPage, + undefined, + activeOnly ) const filteredItems = data.hits.filter((hit) => { if ('is_active' in hit) { @@ -84,7 +88,7 @@ export function useSearchPage({ } fetchData() - }, [currentPage, searchQuery, order, sortBy, indexName, pageTitle, navigate]) + }, [currentPage, searchQuery, order, sortBy, indexName, pageTitle, navigate, activeOnly]) const handleSearch = (query: string) => { setSearchQuery(query) diff --git a/frontend/src/pages/Chapters.tsx b/frontend/src/pages/Chapters.tsx index a54a99591..6eb8d354a 100644 --- a/frontend/src/pages/Chapters.tsx +++ b/frontend/src/pages/Chapters.tsx @@ -23,6 +23,7 @@ const ChaptersPage = () => { } = useSearchPage({ indexName: 'chapters', pageTitle: 'OWASP Chapters', + activeOnly: true, }) useEffect(() => { @@ -32,6 +33,7 @@ const ChaptersPage = () => { query: '', currentPage: 1, filterKey: '', + activeOnly: true, hitsPerPage: 1000, } const data: AlgoliaResponseType = await fetchAlgoliaData( @@ -39,6 +41,7 @@ const ChaptersPage = () => { searchParams.query, searchParams.currentPage, searchParams.filterKey, + searchParams.activeOnly, searchParams.hitsPerPage ) setGeoLocData(data.hits) diff --git a/frontend/src/pages/Projects.tsx b/frontend/src/pages/Projects.tsx index f327880ff..cea95d218 100644 --- a/frontend/src/pages/Projects.tsx +++ b/frontend/src/pages/Projects.tsx @@ -26,6 +26,7 @@ const ProjectsPage = () => { pageTitle: 'OWASP Projects', defaultSortBy: 'default', defaultOrder: 'asc', + activeOnly: true, }) const navigate = useNavigate() From f4c5c518740c27a9cff9f2c36c74c45ae0b1d828 Mon Sep 17 00:00:00 2001 From: Naveen Pal Date: Tue, 4 Feb 2025 20:05:01 +0000 Subject: [PATCH 3/3] fix --- frontend/src/hooks/useSearchPage.ts | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/frontend/src/hooks/useSearchPage.ts b/frontend/src/hooks/useSearchPage.ts index a86b8e5f3..28a141d48 100644 --- a/frontend/src/hooks/useSearchPage.ts +++ b/frontend/src/hooks/useSearchPage.ts @@ -73,13 +73,7 @@ export function useSearchPage({ undefined, activeOnly ) - const filteredItems = data.hits.filter((hit) => { - if ('is_active' in hit) { - return hit.is_active === true - } - return true - }) - setItems(filteredItems) + setItems(data.hits) setTotalPages(data.totalPages) } catch (error) { handleAppError(error)