-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* adds a button to the search page that downloads search results in csv format * refactors fetch function service and helpers to be more flexible in order to allow a non-json response * updates some flaky e2e tests
- Loading branch information
1 parent
e63aa7d
commit e475e7c
Showing
31 changed files
with
608 additions
and
293 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
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,36 @@ | ||
import { downloadOpportunities } from "src/services/fetch/fetchers/searchFetcher"; | ||
import { convertSearchParamsToProperTypes } from "src/utils/search/convertSearchParamsToProperTypes"; | ||
|
||
import { NextRequest, NextResponse } from "next/server"; | ||
|
||
export const revalidate = 0; | ||
|
||
/* | ||
the data flow here goes like: | ||
ExportSearchResultsButton click -> | ||
/export route -> | ||
downloadOpportunities -> | ||
fetchOpportunitySearch -> | ||
ExportSearchResultsButton (handle response by blobbing it to the location) -> user's file system | ||
*/ | ||
|
||
export async function GET(request: NextRequest) { | ||
try { | ||
const searchParams = convertSearchParamsToProperTypes( | ||
Object.fromEntries(request.nextUrl.searchParams.entries().toArray()), | ||
); | ||
const apiResponseBody = await downloadOpportunities(searchParams); | ||
return new NextResponse(apiResponseBody, { | ||
headers: { | ||
"Content-Type": "text/csv", | ||
"Content-Disposition": | ||
"attachment; filename=simpler-grants-search-results.csv", | ||
}, | ||
}); | ||
} catch (e) { | ||
console.error("Error downloading search results", e); | ||
throw e; | ||
} | ||
} |
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
39 changes: 39 additions & 0 deletions
39
frontend/src/components/search/ExportSearchResultsButton.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,39 @@ | ||
"use client"; | ||
|
||
import { downloadSearchResultsCSV } from "src/services/fetch/fetchers/clientSearchResultsDownloadFetcher"; | ||
|
||
import { useTranslations } from "next-intl"; | ||
import { useSearchParams } from "next/navigation"; | ||
import { useCallback } from "react"; | ||
import { Button } from "@trussworks/react-uswds"; | ||
|
||
import { USWDSIcon } from "src/components/USWDSIcon"; | ||
|
||
export function ExportSearchResultsButton() { | ||
const t = useTranslations("Search.exportButton"); | ||
const searchParams = useSearchParams(); | ||
|
||
const downloadSearchResults = useCallback(() => { | ||
// catch included here to satisfy linter | ||
downloadSearchResultsCSV(searchParams).catch((e) => { | ||
throw e; | ||
}); | ||
}, [searchParams]); | ||
|
||
return ( | ||
<div | ||
className="desktop:grid-col-4 desktop:display-flex flex-align-self-center" | ||
data-testid="search-download-button-container" | ||
> | ||
<Button | ||
outline={true} | ||
type={"submit"} | ||
className="width-auto margin-top-2 tablet:width-100 tablet-lg:margin-top-0" | ||
onClick={downloadSearchResults} | ||
> | ||
<USWDSIcon name="file_download" className="usa-icon--size-3" /> | ||
{t("title")} | ||
</Button> | ||
</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
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
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
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
34 changes: 34 additions & 0 deletions
34
frontend/src/services/fetch/fetchers/clientSearchResultsDownloadFetcher.ts
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,34 @@ | ||
import { getConfiguredDayJs } from "src/utils/dateUtil"; | ||
|
||
import { ReadonlyURLSearchParams } from "next/navigation"; | ||
|
||
// downloads csv, then blobs it out to allow browser to download it | ||
// note that this could be handled by just pointing the browser location at the URL | ||
// but we'd lose any ability for graceful error handling that way | ||
export const downloadSearchResultsCSV = async ( | ||
searchParams: ReadonlyURLSearchParams, | ||
) => { | ||
try { | ||
const response = await fetch( | ||
`/api/search/export?${searchParams.toString()}`, | ||
); | ||
|
||
if (!response.ok) { | ||
throw new Error(`Unsuccessful csv download. ${response.status}`); | ||
} | ||
const csvBlob = await response.blob(); | ||
location.assign( | ||
URL.createObjectURL( | ||
new File( | ||
[csvBlob], | ||
`grants-search-${getConfiguredDayJs()(new Date()).format("YYYYMMDDHHmm")}.csv`, | ||
{ | ||
type: "data:text/csv", | ||
}, | ||
), | ||
), | ||
); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
}; |
Oops, something went wrong.