Skip to content

Commit

Permalink
Show warning if user selects years unavailable for all attrs.
Browse files Browse the repository at this point in the history
  • Loading branch information
lublagg committed Oct 4, 2023
1 parent 9b9fe8b commit 85bab1c
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ module.exports = {
"import/no-extraneous-dependencies": "warn",
"import/no-useless-path-segments": "warn",
"jsx-quotes": ["error", "prefer-double"],
"max-len": ["warn", { code: 120, ignoreUrls: true }],
"max-len": ["off"],
"no-bitwise": "error",
"no-debugger": "off",
"no-duplicate-imports": "error",
Expand Down
46 changes: 39 additions & 7 deletions src/components/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import { connect } from "../scripts/connect";
import ProgressIndicator from "../assets/progress-indicator.svg";
import Checkmark from "../assets/done.svg";
import Error from "../assets/warning.svg";
import { flatten } from "../scripts/utils";
import { queryData } from "../constants/queryHeaders";
import { strings } from "../constants/strings";

import css from "./app.scss";

Expand All @@ -20,6 +23,7 @@ function App() {
const [statusMessage, setStatusMessage] = useState<string>("");
const [statusGraphic, setStatusGraphic] = useState<React.ReactElement>();
const [showWarning, setShowWarning] = useState<boolean>(false);
const [warningMessage, setWarningMessage] = useState<string>("");

useEffect(() => {
const init = async () => {
Expand Down Expand Up @@ -49,28 +53,51 @@ function App() {
};

const getData = async () => {
setStatusMessage("Fetching data...");
setStatusMessage(strings.fetchingMsg);
setStatusGraphic(<ProgressIndicator/>);
const res = await createTableFromSelections(selectedOptions);
if (res !== "success") {
setStatusMessage("Fetch Error. Please retry.");
setStatusMessage(strings.fetchSuccess);
setStatusGraphic(<Error/>);
} else {
setStatusMessage("Fetched data.");
setStatusMessage(strings.fetchError);
setStatusGraphic(<Checkmark/>);
}
};

const handleGetData = async () => {
const numberOfRows = getNumberOfItems(selectedOptions);

const attrKeys = attributeOptions.filter((attr) => attr.key !== "cropUnits").map((attr) => attr.key);
const selectedAttrKeys = attrKeys.filter((key) => selectedOptions[key].length > 0);
const allSelectedAttrs = flatten(selectedAttrKeys.map((key) => selectedOptions[key]));
const selectedYears = selectedOptions.years;

allSelectedAttrs.map((attr) => {
const attrInfo = queryData.find((q) => q.plugInAttribute === attr);
if (attrInfo) {
const availableYears = attrInfo.years[selectedOptions.geographicLevel];
for (let i = 0; i < selectedYears.length; i ++) {
const y = selectedYears[i];
if (!availableYears.includes(y)) {
setWarningMessage(strings.yearsWarning);
setShowWarning(true);
break;
}
}
}
});

if (numberOfRows > 4000) {
setWarningMessage(strings.rowsWarning);
setShowWarning(true);
} else {
await getData();
}
};

const handleCloseWarning = async (getDataAnyway: boolean) => {
setWarningMessage("");
setShowWarning(false);
if (getDataAnyway) {
await getData();
Expand All @@ -85,10 +112,10 @@ function App() {
<div className={css.introSection}>
<div className={css.sectionHeaderLine}>
<span className={css.sectionHeaderText}>
Retrieve data on U.S. agricultural statistics at the state or county level.
{strings.appDescription}
</span>
<span
title="Further information about this CODAP plugin"
title={strings.infoTitle}
className={classnames(css.infoButton, {[css.disabled]: showInfo})}
onClick={handleInfoClick}
/>
Expand All @@ -112,10 +139,15 @@ function App() {
<div>{statusGraphic}</div>
<div>{statusMessage}</div>
</div>
<button className={css.getDataButton} disabled={getDataDisabled} onClick={handleGetData}>Get Data</button>
<button className={css.getDataButton} disabled={getDataDisabled} onClick={handleGetData}>
{strings.getData}
</button>
</div>
{ showWarning &&
<Warning handleCloseWarning={handleCloseWarning}/>
<Warning
handleCloseWarning={handleCloseWarning}
message={warningMessage}
/>
}
</div>

Expand Down
8 changes: 4 additions & 4 deletions src/components/warning.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import React from "react";
import WarningIcon from "../assets/warning.svg";
import css from "./warning.scss";

interface IProps {
handleCloseWarning: (getDataAnyway: boolean) => void
handleCloseWarning: (getDataAnyway: boolean) => void;
message: string;
}

export const Warning: React.FC<IProps> = (props) => {
const {handleCloseWarning} = props;
const {handleCloseWarning, message} = props;
return (
<div className={css.popUp}>
<div className={css.popUpContent}>
Expand All @@ -16,7 +16,7 @@ export const Warning: React.FC<IProps> = (props) => {
</div>
<div className={css.popUpBody}>
<div className={css.text}>
The number of attributes you have selected is over 4000 rows and may lead to the program running slowly.
{message}
</div>
</div>
<div className={css.popUpFooter}>
Expand Down
10 changes: 10 additions & 0 deletions src/constants/strings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const strings = {
appDescription: "Retrieve data on U.S. agricultural statistics at the state or county level.",
infoTitle: "Further information about this CODAP plugin",
getData: "Get Data",
yearsWarning: "Some of the attributes you selected are not available for all the years you selected.",
rowsWarning: "The number of attributes you have selected is over 4000 rows and may lead to the program running slowly.",
fetchingMsg: "Fetching data...",
fetchError: "Fetch Error. Please retry.",
fetchSuccess: "Fetched data."
};

0 comments on commit 85bab1c

Please sign in to comment.