Skip to content

Commit

Permalink
Merge pull request #8 from concord-consortium/bug-fixes
Browse files Browse the repository at this point in the history
Fix bugs that were introduced by previous work.
  • Loading branch information
lublagg authored Sep 27, 2023
2 parents 05e8e19 + 177b9bc commit 8b908e4
Show file tree
Hide file tree
Showing 11 changed files with 181 additions and 143 deletions.
4 changes: 2 additions & 2 deletions src/components/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ function App() {
}
}, [selectedOptions]);

const handleSetSelectedOptions = (option: string, value: string | string[]) => {
const newSelectedOptions = {...selectedOptions, [option]: value};
const handleSetSelectedOptions = (newState: Partial<IStateOptions>) => {
const newSelectedOptions = {...selectedOptions, ...newState};
setSelectedOptions(newSelectedOptions);
};

Expand Down
2 changes: 1 addition & 1 deletion src/components/attribute-options.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { IStateOptions } from "../constants/types";
import css from "./options.scss";

interface IProps {
handleSetSelectedOptions: (option: string, value: string|string[]) => void;
handleSetSelectedOptions: (newState: Partial<IStateOptions>) => void;
selectedOptions: IStateOptions;
}

Expand Down
3 changes: 2 additions & 1 deletion src/components/dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import { YearsOptions } from "./years-options";
import { Summary } from "./summary";

import css from "./dropdown.scss";
import { IStateOptions } from "../constants/types";

interface IProps {
category: string
sectionAltText: string
handleSetSelectedOptions: (option: string, value: string|string[]) => void
handleSetSelectedOptions: (newState: Partial<IStateOptions>) => void
selectedOptions: typeof defaultSelectedOptions;
}

Expand Down
28 changes: 18 additions & 10 deletions src/components/options.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export interface IOptions {
options: string[],
selectedOptions: IStateOptions,
inputType: "radio" | "checkbox",
handleSetSelectedOptions: (option: string, value: string|string[]) => void,
handleSetSelectedOptions: (newState: Partial<IStateOptions>) => void
optionKey: OptionKey
}

Expand All @@ -25,20 +25,21 @@ export const Options: React.FC<IOptions> = (props) => {

const handleSelectState = (e: React.ChangeEvent<HTMLInputElement>) => {
if (Array.isArray(selectedOptions[optionKey])) {
let newArray = [...selectedOptions[optionKey]];
const newArray = [...selectedOptions[optionKey]];
const newSelection: any = {[optionKey]: newArray};
if (e.currentTarget.checked) {
newArray.push(e.target.value);
newSelection[optionKey].push(e.target.value);
// If user selects "Age", "Gender", or "Race", auto-select "Total Farmers" as well
if (optionKey === "farmerDemographics" && !newArray.includes("Total Farmers")) {
newArray.push("Total Farmers");
newSelection.farmDemographics.push("Total Farmers");
}
// If user selects a state, de-select "All States"
if (optionKey === "states" && newArray.includes("All States")) {
newArray = newArray.filter((state) => state !== "All States");
newSelection.states = newArray.filter((state) => state !== "All States");
}
newArray.sort();
if (optionKey === "years") {
newArray.reverse();
newSelection.years.reverse();
}
} else {
if (isOptionSelected(e.target.value)) {
Expand All @@ -47,16 +48,23 @@ export const Options: React.FC<IOptions> = (props) => {
if (optionKey === "farmerDemographics" && e.target.value === "Total Farmers") {
const shouldFilter = !includes("Race") && !includes("Gender") && !includes("Age");
if (shouldFilter) {
newArray = newArray.filter((o) => o !== e.target.value);
newSelection[optionKey] = newArray.filter((o) => o !== e.target.value);
}
} else if (selectedOptions.crops.includes(e.target.value)) {
// If user is de-selecting a crop, check that there are crops still selected -
// Otherwise deselect crop units, too
newSelection.crops = newArray.filter((o) => o !== e.target.value);
if (newSelection.crops.length === 0) {
newSelection.cropUnits = "";
}
} else {
newArray = newArray.filter((o) => o !== e.target.value);
newSelection[optionKey] = newArray.filter((o) => o !== e.target.value);
}
}
}
handleSetSelectedOptions(optionKey, newArray);
handleSetSelectedOptions(newSelection);
} else if (optionKey === "geographicLevel" || optionKey === "cropUnits") {
handleSetSelectedOptions(optionKey, e.target.value);
handleSetSelectedOptions({[optionKey]: e.target.value});
}
};

Expand Down
4 changes: 2 additions & 2 deletions src/components/place-options.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Options } from "./options";
import css from "./options.scss";

interface IProps {
handleSetSelectedOptions: (option: string, value: string|string[]) => void;
handleSetSelectedOptions: (newState: Partial<IStateOptions>) => void;
selectedOptions: IStateOptions;
}

Expand All @@ -18,7 +18,7 @@ export const PlaceOptions: React.FC<IProps> = (props) => {
};

const handleSelectAllStates = () => {
handleSetSelectedOptions("states", ["All States"]);
handleSetSelectedOptions({states: ["All States"]});
};

return (
Expand Down
10 changes: 3 additions & 7 deletions src/components/years-options.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import classnames from "classnames";
import css from "./options.scss";

interface IProps {
handleSetSelectedOptions: (option: string, value: string|string[]) => void;
handleSetSelectedOptions: (newState: Partial<IStateOptions>) => void;
selectedOptions: IStateOptions;
}

Expand Down Expand Up @@ -46,15 +46,11 @@ export const YearsOptions: React.FC<IProps> = (props) => {
selectionsNotAvailable.forEach((year) => {
newSelectedYears.splice(newSelectedYears.indexOf(year), 1);
});
handleSetSelectedOptions("years", newSelectedYears);
handleSetSelectedOptions({years: newSelectedYears});
}

}, [selectedOptions, handleSetSelectedOptions]);

const handleSelectYear = (yearKey: string, years: string|string[]) => {
handleSetSelectedOptions(yearKey, years);
};

return (
<div className={classnames(css.checkOptions, css.years)}>
{availableYearOptions.length === 0 ?
Expand All @@ -65,7 +61,7 @@ export const YearsOptions: React.FC<IProps> = (props) => {
optionKey={yearsOptions.key}
inputType={"checkbox"}
selectedOptions={selectedOptions}
handleSetSelectedOptions={handleSelectYear}
handleSetSelectedOptions={handleSetSelectedOptions}
/>}
</div>
);
Expand Down
6 changes: 3 additions & 3 deletions src/constants/codapMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export const attrToCODAPColumnName: IAttrToCodapColumn = {
"attributeNameInCodapTable": "Corn Area Harvested",
"unitInCodapTable": "Acres Harvested"
},
"COTTON, - YIELD, MEASURED IN LB / ACRE": {
"COTTON - YIELD, MEASURED IN LB / ACRE": {
"attributeNameInCodapTable": "Cotton Yield",
"unitInCodapTable": "LB/Acre"
},
Expand All @@ -164,15 +164,15 @@ export const attrToCODAPColumnName: IAttrToCodapColumn = {
"attributeNameInCodapTable": "Oats Area Harvested",
"unitInCodapTable": "Acres Harvested"
},
"SOYBEANS - YIELD MEASURED IN BU / ACRE": {
"SOYBEANS - YIELD, MEASURED IN BU / ACRE": {
"attributeNameInCodapTable": "Soybeans Yield",
"unitInCodapTable": "BU/Acre"
},
"SOYBEANS - ACRES HARVESTED": {
"attributeNameInCodapTable": "Soybeans Area Harvested",
"unitInCodapTable": "Acres Harvested"
},
"WHEAT - YIELD MEASURED IN BU / ACRE": {
"WHEAT - YIELD, MEASURED IN BU / ACRE": {
"attributeNameInCodapTable": "Wheat Yield",
"unitInCodapTable": "BU/Acre"
},
Expand Down
Loading

0 comments on commit 8b908e4

Please sign in to comment.