Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show warning if user selects years unavailable for selected attributes (PT-185951219) #13

Merged
merged 4 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
53 changes: 45 additions & 8 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<JSX.Element>(<p/>);

useEffect(() => {
const init = async () => {
Expand Down Expand Up @@ -49,28 +53,56 @@ 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);
if (numberOfRows > 4000) {
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;
let showYearsWarning = false;

allSelectedAttrs.forEach((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)) {
showYearsWarning = true;
break;
}
}
}
});

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want to exit early here if there is a warning set on line 83? Otherwise if might be overwritten on line 92.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Doug, I slightly changed up the approach in case both warnings are needed at the same time and I've re-requested your review :)

if (showYearsWarning || numberOfRows > 4000) {
const warningMsg = showYearsWarning && numberOfRows > 4000 ?
<div>
<p>{strings.rowsWarning}</p>
<p>{strings.yearsWarning}</p>
</div> : showYearsWarning ? <p>{strings.yearsWarning}</p> :
<p>{strings.rowsWarning}</p>;
setWarningMessage(warningMsg);
setShowWarning(true);
} else {
await getData();
}
};

const handleCloseWarning = async (getDataAnyway: boolean) => {
setWarningMessage(<p/>);
setShowWarning(false);
if (getDataAnyway) {
await getData();
Expand All @@ -85,10 +117,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 +144,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: JSX.Element;
}

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
3 changes: 1 addition & 2 deletions src/components/years-options.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ export const YearsOptions: React.FC<IProps> = (props) => {
return years;
}, new Set());
const newSet: string[] = Array.from(newAvailableYears);
const sorted = newSet.sort().reverse();
setAvailableYearOptions(sorted);
setAvailableYearOptions(newSet);

// if selected years includes years not in available options, remove them from selection
const selectionsNotAvailable = selectedOptions.years.filter(year => !newSet.includes(year));
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."
};
Loading