This repository has been archived by the owner on Sep 18, 2024. It is now read-only.
forked from HHS/simpler-grants-gov
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add search result header into suspense
- Loading branch information
Showing
10 changed files
with
181 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,63 @@ | ||
"use server"; | ||
import { getSearchFetcher } from "src/services/search/searchfetcher/SearchFetcherUtil"; | ||
import { QueryParamData } from "src/services/search/searchfetcher/SearchFetcher"; | ||
import SearchPaginationItem from "./SearchPaginationItem"; | ||
"use client"; | ||
import { Pagination } from "@trussworks/react-uswds"; | ||
import { QueryContext } from "src/app/[locale]/search/QueryProvider"; | ||
import { useSearchParamUpdater } from "src/hooks/useSearchParamUpdater"; | ||
import { useContext } from "react"; | ||
|
||
export enum PaginationPosition { | ||
Top = "topPagination", | ||
Bottom = "bottomPagination", | ||
} | ||
|
||
interface SearchPaginationProps { | ||
searchParams: QueryParamData; | ||
scroll: boolean; | ||
page: number; | ||
query: string | null | undefined; | ||
total?: number | null; | ||
scroll?: boolean; | ||
totalResults?: string; | ||
loading?: boolean; | ||
} | ||
|
||
export default async function SearchPagination({ | ||
searchParams, | ||
scroll, | ||
const MAX_SLOTS = 7; | ||
|
||
export default function SearchPagination({ | ||
page, | ||
query, | ||
total = null, | ||
scroll = false, | ||
totalResults = "", | ||
loading = false, | ||
}: SearchPaginationProps) { | ||
const searchFetcher = getSearchFetcher(); | ||
const searchResults = await searchFetcher.fetchOpportunities(searchParams); | ||
const totalPages = searchResults.pagination_info?.total_pages; | ||
const totalResults = searchResults.pagination_info?.total_records; | ||
const { updateQueryParams } = useSearchParamUpdater(); | ||
const { updateTotalPages, updateTotalResults } = useContext(QueryContext); | ||
const { totalPages } = useContext(QueryContext); | ||
// Shows total pages from the query context before it is re-fetched from the API. | ||
const pages = total || Number(totalPages); | ||
|
||
const updatePage = (page: number) => { | ||
updateTotalPages(String(total)); | ||
updateTotalResults(totalResults); | ||
updateQueryParams(String(page), "page", query, scroll); | ||
}; | ||
|
||
return ( | ||
<> | ||
<SearchPaginationItem | ||
total={totalPages} | ||
page={searchParams.page} | ||
query={searchParams.query} | ||
scroll={scroll} | ||
totalResults={String(totalResults)} | ||
<div | ||
style={{ | ||
pointerEvents: loading ? "none" : "fill", | ||
opacity: loading ? 0.5 : 1, | ||
}} | ||
> | ||
<Pagination | ||
pathname="/search" | ||
totalPages={pages} | ||
currentPage={page} | ||
maxSlots={MAX_SLOTS} | ||
onClickNext={() => updatePage(page + 1)} | ||
onClickPrevious={() => updatePage(page > 1 ? page - 1 : 0)} | ||
onClickPageNumber={(event: React.MouseEvent, page: number) => | ||
updatePage(page) | ||
} | ||
/> | ||
</> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
"use server"; | ||
import { getSearchFetcher } from "src/services/search/searchfetcher/SearchFetcherUtil"; | ||
import { QueryParamData } from "src/services/search/searchfetcher/SearchFetcher"; | ||
import SearchPagination from "./SearchPagination"; | ||
|
||
interface SearchPaginationProps { | ||
searchParams: QueryParamData; | ||
// Determines whether clicking on pager items causes a scroll to the top of the search | ||
// results. Created so the bottom pager can scroll. | ||
scroll: boolean; | ||
} | ||
|
||
export default async function SearchPaginationFetch({ | ||
searchParams, | ||
scroll, | ||
}: SearchPaginationProps) { | ||
const searchFetcher = getSearchFetcher(); | ||
const searchResults = await searchFetcher.fetchOpportunities(searchParams); | ||
const totalPages = searchResults.pagination_info?.total_pages; | ||
const totalResults = searchResults.pagination_info?.total_records; | ||
|
||
return ( | ||
<> | ||
<SearchPagination | ||
total={totalPages} | ||
page={searchParams.page} | ||
query={searchParams.query} | ||
scroll={scroll} | ||
totalResults={String(totalResults)} | ||
/> | ||
</> | ||
); | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
frontend/src/components/search/SearchResultsHeaderFetch.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
"use server"; | ||
import { getSearchFetcher } from "src/services/search/searchfetcher/SearchFetcherUtil"; | ||
import { QueryParamData } from "src/services/search/searchfetcher/SearchFetcher"; | ||
import SearchResultsHeader from "./SearchResultsHeader"; | ||
|
||
export default async function SearchResultsHeaderFetch({ | ||
searchParams, | ||
sortby, | ||
queryTerm, | ||
}: { | ||
searchParams: QueryParamData; | ||
sortby: string | null; | ||
queryTerm: string | null | undefined; | ||
}) { | ||
const searchFetcher = getSearchFetcher(); | ||
const searchResults = await searchFetcher.fetchOpportunities(searchParams); | ||
const totalResults = searchResults.pagination_info?.total_records; | ||
|
||
return ( | ||
<SearchResultsHeader | ||
queryTerm={queryTerm} | ||
sortby={sortby} | ||
totalFetchedResults={String(totalResults)} | ||
/> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.