Skip to content

Commit

Permalink
Convert us-state-choices.json to TSX. (#1265)
Browse files Browse the repository at this point in the history
This uses the commondatabuilder (introduced in #501) to convert our raw JSON state choices into a TypeScript file, which will allow us to do more dynamic things with them, such as using the module's exported `getUSStateChoiceLabels()` function to easily look up a state's full name from its two-letter abbreviation.
  • Loading branch information
toolness authored Apr 23, 2020
1 parent 1a2d65d commit 812be3d
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 3 deletions.
5 changes: 5 additions & 0 deletions common-data/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ const config: DjangoChoicesTypescriptConfig = {
typeName: "SiteChoice",
exportLabels: false,
},
{
jsonFilename: "us-state-choices.json",
typeName: "USStateChoice",
exportLabels: true,
},
],
};

Expand Down
134 changes: 134 additions & 0 deletions common-data/us-state-choices.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
// This file was auto-generated by commondatabuilder.
// Please don't edit it.

export type USStateChoice = "AK"|"AL"|"AS"|"AZ"|"AR"|"CA"|"CO"|"CT"|"DE"|"DC"|"FL"|"GA"|"GU"|"HI"|"ID"|"IL"|"IN"|"IA"|"KS"|"KY"|"LA"|"ME"|"MD"|"MA"|"MI"|"MN"|"MP"|"MS"|"MO"|"MT"|"NE"|"NV"|"NH"|"NJ"|"NM"|"NY"|"NC"|"ND"|"OH"|"OK"|"OR"|"PA"|"PR"|"RI"|"SC"|"SD"|"TN"|"TX"|"UT"|"VT"|"VA"|"VI"|"WA"|"WV"|"WI"|"WY";

export const USStateChoices: USStateChoice[] = [
"AK",
"AL",
"AS",
"AZ",
"AR",
"CA",
"CO",
"CT",
"DE",
"DC",
"FL",
"GA",
"GU",
"HI",
"ID",
"IL",
"IN",
"IA",
"KS",
"KY",
"LA",
"ME",
"MD",
"MA",
"MI",
"MN",
"MP",
"MS",
"MO",
"MT",
"NE",
"NV",
"NH",
"NJ",
"NM",
"NY",
"NC",
"ND",
"OH",
"OK",
"OR",
"PA",
"PR",
"RI",
"SC",
"SD",
"TN",
"TX",
"UT",
"VT",
"VA",
"VI",
"WA",
"WV",
"WI",
"WY"
];

const USStateChoiceSet: Set<String> = new Set(USStateChoices);

export function isUSStateChoice(choice: string): choice is USStateChoice {
return USStateChoiceSet.has(choice);
}

export type USStateChoiceLabels = {
[k in USStateChoice]: string;
};

export function getUSStateChoiceLabels(): USStateChoiceLabels {
return {
AK: "Alaska",
AL: "Alabama",
AS: "American Samoa",
AZ: "Arizona",
AR: "Arkansas",
CA: "California",
CO: "Colorado",
CT: "Connecticut",
DE: "Delaware",
DC: "District of Columbia",
FL: "Florida",
GA: "Georgia",
GU: "Guam",
HI: "Hawaii",
ID: "Idaho",
IL: "Illinois",
IN: "Indiana",
IA: "Iowa",
KS: "Kansas",
KY: "Kentucky",
LA: "Louisiana",
ME: "Maine",
MD: "Maryland",
MA: "Massachusetts",
MI: "Michigan",
MN: "Minnesota",
MP: "Northern Mariana Islands",
MS: "Mississippi",
MO: "Missouri",
MT: "Montana",
NE: "Nebraska",
NV: "Nevada",
NH: "New Hampshire",
NJ: "New Jersey",
NM: "New Mexico",
NY: "New York",
NC: "North Carolina",
ND: "North Dakota",
OH: "Ohio",
OK: "Oklahoma",
OR: "Oregon",
PA: "Pennsylvania",
PR: "Puerto Rico",
RI: "Rhode Island",
SC: "South Carolina",
SD: "South Dakota",
TN: "Tennessee",
TX: "Texas",
UT: "Utah",
VT: "Vermont",
VA: "Virginia",
VI: "Virgin Islands",
WA: "Washington",
WV: "West Virginia",
WI: "Wisconsin",
WY: "Wyoming",
};
}
9 changes: 6 additions & 3 deletions frontend/lib/forms/mailing-address-fields.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import React from "react";
import { SelectFormField, ChoiceFormFieldProps } from "./form-fields";
import US_STATE_CHOICES from "../../../common-data/us-state-choices.json";
import { DjangoChoices } from "../common-data";
import { toDjangoChoices } from "../common-data";
import {
USStateChoices,
getUSStateChoiceLabels,
} from "../../../common-data/us-state-choices";

export const USStateFormField: React.FC<Omit<
ChoiceFormFieldProps,
"label" | "choices"
>> = (props) => (
<SelectFormField
{...props}
choices={US_STATE_CHOICES as DjangoChoices}
choices={toDjangoChoices(USStateChoices, getUSStateChoiceLabels())}
label="State"
/>
);

0 comments on commit 812be3d

Please sign in to comment.