Skip to content

Commit

Permalink
Limit years options and "get data" button availability.
Browse files Browse the repository at this point in the history
  • Loading branch information
lublagg committed Sep 15, 2023
1 parent a24f45a commit b5d6406
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 51 deletions.
2 changes: 1 addition & 1 deletion src/components/app.scss
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ button {
font-family: 'Montserrat', sans-serif;
font-size: 12px;
font-weight: 500;
&:hover {
&:hover:not(:disabled) {
cursor: pointer;
background: $teal-light-12;
}
Expand Down
17 changes: 15 additions & 2 deletions src/components/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, {useEffect, useState} from "react";
import { Dropdown } from "./dropdown";
import classnames from "classnames";
import { Information } from "./information";
import { categories, defaultSelectedOptions } from "./constants";
import { attributeOptions, categories, defaultSelectedOptions, yearsOptions } from "./constants";

Check warning on line 5 in src/components/app.tsx

View workflow job for this annotation

GitHub Actions / Build and Run Jest Tests

'yearsOptions' is defined but never used

Check warning on line 5 in src/components/app.tsx

View workflow job for this annotation

GitHub Actions / S3 Deploy

'yearsOptions' is defined but never used
import { IStateOptions } from "./types";
import { createQueryFromSelections } from "../scripts/api";
import { connect } from "../scripts/connect";
Expand All @@ -13,6 +13,8 @@ import css from "./app.scss";
function App() {
const [showInfo, setShowInfo] = useState<boolean>(false);
const [selectedOptions, setSelectedOptions] = useState<IStateOptions>(defaultSelectedOptions);
const [getDataDisabled, setGetDataDisabled] = useState<boolean>(true);
const {farmerDemographics, farmDemographics, crops, economicsAndWages} = selectedOptions;

Check warning on line 17 in src/components/app.tsx

View workflow job for this annotation

GitHub Actions / Build and Run Jest Tests

'farmerDemographics' is assigned a value but never used

Check warning on line 17 in src/components/app.tsx

View workflow job for this annotation

GitHub Actions / Build and Run Jest Tests

'farmDemographics' is assigned a value but never used

Check warning on line 17 in src/components/app.tsx

View workflow job for this annotation

GitHub Actions / Build and Run Jest Tests

'crops' is assigned a value but never used

Check warning on line 17 in src/components/app.tsx

View workflow job for this annotation

GitHub Actions / Build and Run Jest Tests

'economicsAndWages' is assigned a value but never used

Check warning on line 17 in src/components/app.tsx

View workflow job for this annotation

GitHub Actions / S3 Deploy

'farmerDemographics' is assigned a value but never used

Check warning on line 17 in src/components/app.tsx

View workflow job for this annotation

GitHub Actions / S3 Deploy

'farmDemographics' is assigned a value but never used

Check warning on line 17 in src/components/app.tsx

View workflow job for this annotation

GitHub Actions / S3 Deploy

'crops' is assigned a value but never used

Check warning on line 17 in src/components/app.tsx

View workflow job for this annotation

GitHub Actions / S3 Deploy

'economicsAndWages' is assigned a value but never used

useEffect(() => {
const init = async () => {
Expand All @@ -21,6 +23,17 @@ function App() {
init();
}, []);

useEffect(() => {
const {geographicLevel, states, years} = selectedOptions;
const attrKeys = attributeOptions.filter((attr) => attr.key !== "cropUnits").map((attr) => attr.key);
const selectedAttrKeys = attrKeys.filter((key) => selectedOptions[key].length > 0);
if (selectedAttrKeys.length && geographicLevel && states.length && years.length) {
setGetDataDisabled(false);
} else {
setGetDataDisabled(true);
}
}, [selectedOptions]);

const handleSetSelectedOptions = (option: string, value: string | string[]) => {
const newSelectedOptions = {...selectedOptions, [option]: value};
setSelectedOptions(newSelectedOptions);
Expand Down Expand Up @@ -74,7 +87,7 @@ function App() {
</div>
<div className={css.summary}>
<span className={css.statusGraphic}></span>
<button className={css.getDataButton} onClick={handleGetData}>Get Data</button>
<button className={css.getDataButton} disabled={getDataDisabled} onClick={handleGetData}>Get Data</button>
</div>
</div>

Expand Down
16 changes: 8 additions & 8 deletions src/components/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,12 @@ export const categories = [
];

export const defaultSelectedOptions: IStateOptions = {
"geographicLevel": "State",
"states": ["All States"],
"farmerDemographics": [],
"farmDemographics": [],
"economicsAndWages": [],
"cropUnits": "",
"crops": [],
"years": []
geographicLevel: "State",
states: ["All States"],
farmerDemographics: [],
farmDemographics: [],
economicsAndWages: [],
cropUnits: "",
crops: [],
years: []
};
16 changes: 8 additions & 8 deletions src/components/types.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
export interface IStateOptions {
"geographicLevel": string,
"states": string[]
"farmerDemographics": string[],
"farmDemographics": string[],
"economicsAndWages": string[],
"cropUnits": string,
"crops": string[]
"years": string[]
geographicLevel: string,
states: string[]
farmerDemographics: string[],
farmDemographics: string[],
economicsAndWages: string[],
cropUnits: string,
crops: string[]
years: string[]
}

export type OptionKey = keyof IStateOptions;
Expand Down
4 changes: 4 additions & 0 deletions src/components/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const flatten = (arr: any[]): any[] => {
return arr.reduce((acc: any[], val: any) =>
Array.isArray(val) ? acc.concat(flatten(val)) : acc.concat(val), []);
}

Check warning on line 4 in src/components/utils.ts

View workflow job for this annotation

GitHub Actions / Build and Run Jest Tests

Missing semicolon

Check warning on line 4 in src/components/utils.ts

View workflow job for this annotation

GitHub Actions / S3 Deploy

Missing semicolon
65 changes: 34 additions & 31 deletions src/components/years-options.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import React from "react";
import React, { useEffect, useState } from "react";
import { attributeOptions, yearsOptions } from "./constants";
import { IStateOptions } from "./types";
import { Options } from "./options";
import { queryData } from "../scripts/query-headers";
import { flatten } from "./utils";

import css from "./options.scss";
import { queryData } from "../scripts/query-headers";

interface IProps {
handleSetSelectedOptions: (option: string, value: string|string[]) => void;
Expand All @@ -13,47 +14,49 @@ interface IProps {

export const YearsOptions: React.FC<IProps> = (props) => {
const {handleSetSelectedOptions, selectedOptions} = props;
const [availableYearOptions, setAvailableYearOptions] = useState<string[]>([]);
const {farmerDemographics, farmDemographics, crops, economicsAndWages} = selectedOptions;

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

// check if any attributeOption keys have values in selectedOptions
const attrKeys = attributeOptions.map((attr) => attr.key);
useEffect(() => {
const attrKeys = attributeOptions.filter((attr) => attr.key !== "cropUnits").map((attr) => attr.key);
const selectedAttrKeys = attrKeys.filter((key) => selectedOptions[key].length > 0);
const areAnyAttrsSelected = selectedAttrKeys.length > 0;
// if any attributes are selected, check that selected year data is available for that selected attribute
if (areAnyAttrsSelected) {
const selectedYears = Array.isArray(years) ? years : [years];
const selectedAttrs = selectedAttrKeys.map((key) => selectedOptions[key]);
selectedAttrs.forEach((attr) => {
if (Array.isArray(attr)) {
attr.forEach((subAttr) => {
const subAttrData = queryData.find((d) => d.plugInAttribute === subAttr);
const yearKeyToUse = selectedOptions.geographicLevel === "county" ? "county" : "state";
const availableYears = subAttrData?.years[yearKeyToUse];
if (availableYears) {
// check -- are selectedYears included in queryParams?.years[yearKeyTouse] ?
const areYearsValid = selectedYears.map((y) => availableYears.indexOf(y) > -1).indexOf(false) < 0;
console.log({areYearsValid});
console.log({availableYears});
console.log({selectedYears});
// do something if years are not valid
}
});
}
});

if (!selectedAttrKeys.length) {
return;
}

const yearKeyToUse = selectedOptions.geographicLevel === "County" ? "county" : "state";
const allSelectedAttrs = flatten(selectedAttrKeys.map((key) => selectedOptions[key]));
const newAvailableYears = allSelectedAttrs.reduce((years, attr) => {
const subAttrData = queryData.find((d) => d.plugInAttribute === attr);
const availableYears = subAttrData?.years[yearKeyToUse];
if (availableYears) {
availableYears.forEach((y) => {
years.add(y);
});
}
return years;
}, new Set());

setAvailableYearOptions(Array.from(newAvailableYears));
}, [farmerDemographics, farmDemographics, crops, economicsAndWages])

Check warning on line 42 in src/components/years-options.tsx

View workflow job for this annotation

GitHub Actions / Build and Run Jest Tests

React Hook useEffect has a missing dependency: 'selectedOptions'. Either include it or remove the dependency array

Check warning on line 42 in src/components/years-options.tsx

View workflow job for this annotation

GitHub Actions / S3 Deploy

React Hook useEffect has a missing dependency: 'selectedOptions'. Either include it or remove the dependency array

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

return (
<div className={css.checkOptions}>
{availableYearOptions.length === 0 ?
<div>Please select attributes to see available years.</div>
:
<Options
options={yearsOptions.options}
options={availableYearOptions}
optionKey={yearsOptions.key}
inputType={"checkbox"}
selectedOptions={selectedOptions}
handleSetSelectedOptions={handleSelectYear}
/>
/>}
</div>
);
};
2 changes: 1 addition & 1 deletion src/scripts/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const connect = {
},

createNewDataset: async function (geoLevel) {
const geoLabel = geoLevel === "State" ? "States" : "Counties";
const geoLabel = geoLevel === "State" ? states : "Counties";
return codapInterface.sendRequest({
action: 'create',
resource: 'dataContext',
Expand Down

0 comments on commit b5d6406

Please sign in to comment.