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

Filter inactive projects on API level #610

Closed
Closed
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion frontend/__tests__/src/pages/Contribute.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ describe('Contribute Component', () => {
fireEvent.change(searchInput, { target: { value: '' } })
})

expect(fetchAlgoliaData).toHaveBeenCalledWith('issues', '', 1)
expect(fetchAlgoliaData).toHaveBeenCalledWith('issues', '', 1, undefined, false)
})

test('handles error states in card rendering', async () => {
Expand Down
12 changes: 9 additions & 3 deletions frontend/src/api/fetchAlgoliaData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const fetchAlgoliaData = async <T>(
query = '',
currentPage = 0,
filterKey?: string,
activeOnly = false,
hitsPerPage = 25
): Promise<AlgoliaResponseType<T>> => {
if (!client) {
Expand All @@ -28,9 +29,14 @@ export const fetchAlgoliaData = async <T>(
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],
Expand Down
10 changes: 7 additions & 3 deletions frontend/src/hooks/useSearchPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ interface UseSearchPageOptions {
pageTitle: string
defaultSortBy?: string
defaultOrder?: string
activeOnly?: boolean
}

interface UseSearchPageReturn<T> {
Expand All @@ -25,11 +26,12 @@ interface UseSearchPageReturn<T> {
handleOrderChange: (order: string) => void
}

export function useSearchPage<T>({
export function useSearchPage<T extends object>({
indexName,
pageTitle,
defaultSortBy = '',
defaultOrder = '',
activeOnly = false,
}: UseSearchPageOptions): UseSearchPageReturn<T> {
const navigate = useNavigate()
const [searchParams, setSearchParams] = useSearchParams()
Expand Down Expand Up @@ -79,7 +81,9 @@ export function useSearchPage<T>({
? `${indexName}_${sortBy}${order && order !== '' ? `_${order}` : ''}`
: indexName,
searchQuery,
currentPage
currentPage,
undefined,
activeOnly
)
setItems(data.hits)
setTotalPages(data.totalPages)
Expand All @@ -90,7 +94,7 @@ export function useSearchPage<T>({
}

fetchData()
}, [currentPage, searchQuery, order, sortBy, indexName, pageTitle, navigate])
}, [currentPage, searchQuery, order, sortBy, indexName, pageTitle, navigate, activeOnly])

const handleSearch = (query: string) => {
setSearchQuery(query)
Expand Down
3 changes: 3 additions & 0 deletions frontend/src/pages/Chapters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const ChaptersPage = () => {
} = useSearchPage<ChapterTypeAlgolia>({
indexName: 'chapters',
pageTitle: 'OWASP Chapters',
activeOnly: true,
})

useEffect(() => {
Expand All @@ -34,13 +35,15 @@ const ChaptersPage = () => {
query: '',
currentPage: 1,
filterKey: '',
activeOnly: true,
hitsPerPage: 1000,
}
const data: AlgoliaResponseType<ChapterTypeAlgolia> = await fetchAlgoliaData(
searchParams.indexName,
searchParams.query,
searchParams.currentPage,
searchParams.filterKey,
searchParams.activeOnly,
searchParams.hitsPerPage
)
setGeoLocData(data.hits)
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/pages/Projects.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const ProjectsPage = () => {
pageTitle: 'OWASP Projects',
defaultSortBy: 'default',
defaultOrder: 'asc',
activeOnly: true,
})

const navigate = useNavigate()
Expand Down Expand Up @@ -80,7 +81,7 @@ const ProjectsPage = () => {
}
totalPages={totalPages}
>
{projects && projects.filter((project) => project.is_active).map(renderProjectCard)}
{projects && projects.map(renderProjectCard)}
</SearchPageLayout>
</MetadataManager>
)
Expand Down
Loading