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
[Issue #96]: Opportunity listing page (first pass) #97
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
805b253
initial commit
rylew1 3ed6893
fix error param order
rylew1 9bc51db
patch test
rylew1 f95d3bc
remove console log
rylew1 de00b48
revert bodyParams to body
rylew1 ea45074
add typing and fix ts errors
rylew1 9a11a74
add test coverage
rylew1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { | ||
ApiResponse, | ||
Summary, | ||
} from "../../../../types/opportunity/opportunityResponseTypes"; | ||
|
||
import { Metadata } from "next"; | ||
import NotFound from "../../../not-found"; | ||
import OpportunityListingAPI from "../../../api/OpportunityListingAPI"; | ||
import { getTranslations } from "next-intl/server"; | ||
import { isSummary } from "../../../../utils/opportunity/isSummary"; | ||
|
||
export async function generateMetadata() { | ||
const t = await getTranslations({ locale: "en" }); | ||
const meta: Metadata = { | ||
title: t("OpportunityListing.page_title"), | ||
description: t("OpportunityListing.meta_description"), | ||
}; | ||
return meta; | ||
} | ||
|
||
export default async function OpportunityListing({ | ||
params, | ||
}: { | ||
params: { id: string }; | ||
}) { | ||
const id = Number(params.id); | ||
|
||
// Opportunity id needs to be a number greater than 1 | ||
if (isNaN(id) || id < 0) { | ||
return <NotFound />; | ||
} | ||
|
||
const api = new OpportunityListingAPI(); | ||
let opportunity: ApiResponse; | ||
try { | ||
opportunity = await api.getOpportunityById(id); | ||
} catch (error) { | ||
console.error("Failed to fetch opportunity:", error); | ||
return <NotFound />; | ||
} | ||
|
||
if (!opportunity.data) { | ||
return <NotFound />; | ||
} | ||
|
||
const renderSummary = (summary: Summary) => { | ||
return ( | ||
<> | ||
{Object.entries(summary).map(([summaryKey, summaryValue]) => ( | ||
<tr key={summaryKey}> | ||
<td className="word-wrap">{`summary.${summaryKey}`}</td> | ||
<td className="word-wrap">{JSON.stringify(summaryValue)}</td> | ||
</tr> | ||
))} | ||
</> | ||
); | ||
}; | ||
|
||
return ( | ||
<div className="grid-container"> | ||
<div className="grid-row margin-y-4"> | ||
<div className="usa-table-container"> | ||
<table className="usa-table usa-table--borderless margin-x-auto width-full maxw-desktop-lg"> | ||
<thead> | ||
<tr> | ||
<th>Field Name</th> | ||
<th>Data</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
{Object.entries(opportunity.data).map(([key, value]) => { | ||
if (key === "summary" && isSummary(value)) { | ||
return renderSummary(value); | ||
} else { | ||
return ( | ||
<tr key={key}> | ||
<td className="word-wrap">{key}</td> | ||
<td className="word-wrap">{JSON.stringify(value)}</td> | ||
</tr> | ||
); | ||
} | ||
})} | ||
</tbody> | ||
</table> | ||
</div> | ||
</div> | ||
</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 |
---|---|---|
@@ -1,7 +1,3 @@ | ||
// This server-only package is recommended by Next.js to ensure code is only run on the server. | ||
// It provides a build-time error if client-side code attempts to invoke the code here. | ||
// Since we're pulling in an API Auth Token here, this should be server only | ||
// https://nextjs.org/docs/app/building-your-application/rendering/composition-patterns#keeping-server-only-code-out-of-the-client-environment | ||
import "server-only"; | ||
|
||
import { | ||
|
@@ -69,7 +65,7 @@ export default abstract class BaseApi { | |
basePath: string, | ||
namespace: string, | ||
subPath: string, | ||
queryParamData: QueryParamData, | ||
queryParamData?: QueryParamData, | ||
body?: JSONRequestBody, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. all queryParamData / searchInput references need to be an optional param because they're only used for search ( |
||
options: { | ||
additionalHeaders?: HeadersDict; | ||
|
@@ -109,7 +105,7 @@ export default abstract class BaseApi { | |
private async sendRequest( | ||
url: string, | ||
fetchOptions: RequestInit, | ||
queryParamData: QueryParamData, | ||
queryParamData?: QueryParamData, | ||
) { | ||
let response: Response; | ||
let responseBody: SearchAPIResponse; | ||
|
@@ -189,19 +185,21 @@ function createRequestBody(payload?: JSONRequestBody): XMLHttpRequestBodyInit { | |
*/ | ||
export function fetchErrorToNetworkError( | ||
error: unknown, | ||
searchInputs: QueryParamData, | ||
searchInputs?: QueryParamData, | ||
) { | ||
// Request failed to send or something failed while parsing the response | ||
// Log the JS error to support troubleshooting | ||
console.error(error); | ||
return new NetworkError(error, searchInputs); | ||
return searchInputs | ||
? new NetworkError(error, searchInputs) | ||
: new NetworkError(error); | ||
} | ||
|
||
function handleNotOkResponse( | ||
response: SearchAPIResponse, | ||
message: string, | ||
status_code: number, | ||
searchInputs: QueryParamData, | ||
searchInputs?: QueryParamData, | ||
) { | ||
const { errors } = response; | ||
if (isEmpty(errors)) { | ||
|
@@ -218,7 +216,7 @@ function handleNotOkResponse( | |
const throwError = ( | ||
message: string, | ||
status_code: number, | ||
searchInputs: QueryParamData, | ||
searchInputs?: QueryParamData, | ||
firstError?: APIResponseError, | ||
) => { | ||
console.log("Throwing error: ", message, status_code, searchInputs); | ||
|
@@ -246,9 +244,9 @@ const throwError = ( | |
default: | ||
throw new ApiRequestError( | ||
error, | ||
searchInputs, | ||
"APIRequestError", | ||
status_code, | ||
searchInputs, | ||
); | ||
} | ||
}; |
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,29 @@ | ||
import "server-only"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. only ever run this code on the next server |
||
|
||
import { ApiResponse } from "../../types/opportunity/opportunityResponseTypes"; | ||
import BaseApi from "./BaseApi"; | ||
|
||
export default class OpportunityListingAPI extends BaseApi { | ||
get version(): string { | ||
return "v1"; | ||
} | ||
|
||
get basePath(): string { | ||
return process.env.API_URL || ""; | ||
} | ||
|
||
get namespace(): string { | ||
return "opportunities"; | ||
} | ||
|
||
async getOpportunityById(opportunityId: number): Promise<ApiResponse> { | ||
const subPath = `${opportunityId}`; | ||
const response = await this.request( | ||
"GET", | ||
this.basePath, | ||
this.namespace, | ||
subPath, | ||
); | ||
return response as ApiResponse; | ||
} | ||
} |
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
57 changes: 57 additions & 0 deletions
57
frontend/src/types/opportunity/opportunityResponseTypes.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,57 @@ | ||
export interface OpportunityAssistanceListing { | ||
assistance_listing_number: string; | ||
program_title: string; | ||
} | ||
|
||
export interface Summary { | ||
additional_info_url: string; | ||
additional_info_url_description: string; | ||
agency_code: string; | ||
agency_contact_description: string; | ||
agency_email_address: string; | ||
agency_email_address_description: string; | ||
agency_name: string; | ||
agency_phone_number: string; | ||
applicant_eligibility_description: string; | ||
applicant_types: string[]; | ||
archive_date: string; | ||
award_ceiling: number; | ||
award_floor: number; | ||
close_date: string; | ||
close_date_description: string; | ||
estimated_total_program_funding: number; | ||
expected_number_of_awards: number; | ||
fiscal_year: number; | ||
forecasted_award_date: string; | ||
forecasted_close_date: string; | ||
forecasted_close_date_description: string; | ||
forecasted_post_date: string; | ||
forecasted_project_start_date: string; | ||
funding_categories: string[]; | ||
funding_category_description: string; | ||
funding_instruments: string[]; | ||
is_cost_sharing: boolean; | ||
is_forecast: boolean; | ||
post_date: string; | ||
summary_description: string; | ||
} | ||
|
||
export interface Opportunity { | ||
agency: string; | ||
category: string; | ||
category_explanation: string | null; | ||
created_at: string; | ||
opportunity_assistance_listings: OpportunityAssistanceListing[]; | ||
opportunity_id: number; | ||
opportunity_number: string; | ||
opportunity_status: string; | ||
opportunity_title: string; | ||
summary: Summary; | ||
updated_at: string; | ||
} | ||
|
||
export interface ApiResponse { | ||
data: Opportunity[]; | ||
message: string; | ||
status_code: number; | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm still catching up on whether or not making a page async affects ability to statically render the page. The opportunity pages are the same for everyone and rarely update so should not be rebuilt for every single request. Statically rendering seems like the easiest way to achieve that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can punt this consideration for #85 .
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧵 here https://nava.slack.com/archives/C057K146W8H/p1718912909890579