Skip to content

Commit

Permalink
Add loading message when fetching data.
Browse files Browse the repository at this point in the history
  • Loading branch information
lublagg committed Sep 20, 2023
1 parent dc9048b commit cf66a33
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 52 deletions.
6 changes: 6 additions & 0 deletions src/assets/done.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions src/assets/progress-indicator.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions src/assets/warning.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 5 additions & 14 deletions src/components/app.scss
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ button:focus {
display: flex;
align-items: baseline;
justify-content: space-between;
.status {
display: flex;
align-items: center;
gap: 10px;
}
}

.introSection {
Expand Down Expand Up @@ -104,17 +109,3 @@ a {
}
}
}

.summary {
font-family: 'icomoon' !important;
font-size: 20px;
font-style: normal;
font-weight: normal;
font-variant: normal;
text-transform: none;
line-height: 1;

/* Better Font Rendering =========== */
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
22 changes: 19 additions & 3 deletions src/components/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@ import { attributeOptions, categories, defaultSelectedOptions } from "../constan
import { IStateOptions } from "../constants/types";
import { createTableFromSelections } from "../scripts/api";
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 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 [statusMessage, setStatusMessage] = useState<string>("");
const [statusGraphic, setStatusGraphic] = useState<React.ReactElement>();

useEffect(() => {
const init = async () => {
Expand Down Expand Up @@ -43,7 +47,16 @@ function App() {
};

const handleGetData = async () => {
await createTableFromSelections(selectedOptions);
setStatusMessage("Fetching data...");
setStatusGraphic(<ProgressIndicator/>);
const res = await createTableFromSelections(selectedOptions);
if (res !== "success") {
setStatusMessage("Fetch Error. Please retry.");
setStatusGraphic(<Error/>)

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

View workflow job for this annotation

GitHub Actions / Build and Run Jest Tests

Missing semicolon

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

View workflow job for this annotation

GitHub Actions / Build and Run Jest Tests

Missing semicolon

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

View workflow job for this annotation

GitHub Actions / S3 Deploy

Missing semicolon

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

View workflow job for this annotation

GitHub Actions / S3 Deploy

Missing semicolon
} else {
setStatusMessage("Fetched data.");
setStatusGraphic(<Checkmark/>);
}
};

return (
Expand Down Expand Up @@ -77,7 +90,10 @@ function App() {
})}
</div>
<div className={css.summary}>
<span className={css.statusGraphic}></span>
<div className={css.status}>
<div>{statusGraphic}</div>
<div>{statusMessage}</div>
</div>
<button className={css.getDataButton} disabled={getDataDisabled} onClick={handleGetData}>Get Data</button>
</div>
</div>
Expand Down
4 changes: 2 additions & 2 deletions src/constants/queryHeaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ export const queryData: Array<IQueryHeaders> = [
domain_desc: "Economic Class",
geographicAreas: ["State"],
years: {
"County": allYears.filter(y => Number(y) >= 1987),
"State": allYears.filter(y => Number(y) >= 1987)
"County": allYears.filter(y => Number(y) >= 1998),
"State": allYears.filter(y => Number(y) >= 1998)
}
},
{
Expand Down
60 changes: 27 additions & 33 deletions src/scripts/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,6 @@ interface IGetAttrDataParams {
state?: string
}

// export const fetchData = async (req: string) => {
// try {
// const response = await fetchJsonp(req, {timeout: 10000});
// const json = await response.json();
// return json;
// } catch (error) {
// console.log("parsing failed", error);
// throw error;
// }
// };

export const fetchDataWithRetry = async (req: string, maxRetries = 3) => {
let retries = 0;
while (retries < maxRetries) {
Expand Down Expand Up @@ -103,36 +92,41 @@ export const createRequest = ({attribute, geographicLevel, location, year, cropC

export const createTableFromSelections = async (selectedOptions: IStateOptions) => {
const {geographicLevel, states, cropUnits, years, ...subOptions} = selectedOptions;

await connect.getNewDataContext();
await connect.createTopCollection(geographicLevel);

const allAttrs: Array<string|ICropDataItem> = ["Year"];

for (const key in subOptions) {
const selections = subOptions[key as keyof typeof subOptions];
for (const attribute of selections) {
const queryParams = getQueryParams(attribute);
if (!queryParams) {
throw new Error("Invalid attribute");
}
const {short_desc} = queryParams;
if (Array.isArray(short_desc)) {
for (const desc of short_desc) {
console.log({desc});
const codapColumnName = attrToCODAPColumnName[desc].attributeNameInCodapTable;
allAttrs.push(codapColumnName);
try {
for (const key in subOptions) {
const selections = subOptions[key as keyof typeof subOptions];
for (const attribute of selections) {
const queryParams = getQueryParams(attribute);
if (!queryParams) {
throw new Error("Invalid attribute");
}
const {short_desc} = queryParams;
if (Array.isArray(short_desc)) {
for (const desc of short_desc) {
console.log({desc});

Check warning on line 111 in src/scripts/api.ts

View workflow job for this annotation

GitHub Actions / Build and Run Jest Tests

Unexpected console statement

Check warning on line 111 in src/scripts/api.ts

View workflow job for this annotation

GitHub Actions / S3 Deploy

Unexpected console statement
const codapColumnName = attrToCODAPColumnName[desc].attributeNameInCodapTable;
allAttrs.push(codapColumnName);
}
} else if (typeof short_desc === "object" && cropUnits) {
const attr = short_desc[cropUnits as keyof ICropDataItem][0];
allAttrs.push(attrToCODAPColumnName[attr].attributeNameInCodapTable);
}
} else if (typeof short_desc === "object" && cropUnits) {
const attr = short_desc[cropUnits as keyof ICropDataItem][0];
allAttrs.push(attrToCODAPColumnName[attr].attributeNameInCodapTable);
}
}
}

await connect.createSubCollection(geographicLevel, allAttrs);
const items = await getItems(selectedOptions);
await connect.createItems(items);
await connect.makeCaseTableAppear();
await connect.createSubCollection(geographicLevel, allAttrs);
const items = await getItems(selectedOptions);
await connect.createItems(items);
await connect.makeCaseTableAppear();
return "success";
} catch (error) {
return error;
}
};

const getItems = async (selectedOptions: IStateOptions) => {
Expand Down

0 comments on commit cf66a33

Please sign in to comment.