Skip to content

Commit

Permalink
Merge pull request #14 from concord-consortium/188233122-incomplete-d…
Browse files Browse the repository at this point in the history
…ata-warning

Show warning when some data are not available
  • Loading branch information
pjanik authored Sep 10, 2024
2 parents ae9ccd3 + a8863af commit 24388a2
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/components/app.scss
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
}
}

&.done {
&.done, &.incomplete {
font-weight: 600;
}
}
Expand Down
7 changes: 6 additions & 1 deletion src/components/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ import { IAttribute, ICountry, IYear } from "../types";
import { InfoModal } from "./info-modal";
import { requestData } from "../data/request";
import { attributes } from "../data/selectors";
import { isDataComplete } from "../data/utils";

import InfoIcon from "../assets/info.svg";
import ProgressIndicator from "../assets/progress-indicator.svg";
import DoneIcon from "../assets/done.svg";
import WarningIcon from "../assets/warning.svg";

import "./app.scss";

Expand Down Expand Up @@ -61,7 +63,7 @@ export const App = () => {
});
await createItems(kDataContextName, cases);

setDataStatus("retrieved");
setDataStatus(isDataComplete(cases, tableAttributes) ? "retrieved" : "incomplete");
};

return (
Expand Down Expand Up @@ -93,6 +95,9 @@ export const App = () => {
{
dataStatus === "retrieved" && <div className="done"><DoneIcon /> Retrieved data</div>
}
{
dataStatus === "incomplete" && <div className="incomplete"><WarningIcon /> Some data requested are not available</div>
}
</div>
<button onClick={handleCreateData} disabled={getDataDisabled}>Get Data</button>
</div>
Expand Down
47 changes: 28 additions & 19 deletions src/data/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ const regionMap = makeMap(regions);
const countryMap = makeMap(countries);
const yearMap = makeMap(years);

const allCountryIds = countries.map(c => c.id);
const allYearIds = years.map(y => y.id);

let rawDataValueRows: IRawDataValueRow[] | undefined = undefined;

const loadJSON = async (): Promise<IRawDataValueRow[]> => {
Expand Down Expand Up @@ -48,29 +51,35 @@ export const requestData = async (options: IRequestDataOptions): Promise<ICaseVa
return match;
});

const cases = filteredRows
.reduce<Record<string, ICaseValue>>((acc, [attributeId, countryId, yearId, value]) => {
const attribute = attributeMap[attributeId];
const _countryIds = countryIds.length > 0 ? countryIds : allCountryIds;
const _yearIds = yearIds.length > 0 ? yearIds : allYearIds;

const countryYearMap: Record<string, ICaseValue> = {};
_countryIds.forEach(countryId => {
_yearIds.forEach(yearId => {
const country = countryMap[countryId];
const region = regionMap[country.regionId];
const year = yearMap[yearId];

// Create the key for grouping by year and country
const yearCountryKey = `${year.name}-${country.name}`;
// Initialize the group if it doesn't exist yet
if (!acc[yearCountryKey]) {
acc[yearCountryKey] = {
Year: year.name,
Country: country.name,
Region: region.name
};
}

// Add the attribute and its value
acc[yearCountryKey][attribute.name] = value;
countryYearMap[`${countryId}-${yearId}`] = {
Country: country.name,
Region: region.name,
Year: year.name,
};
});
});

return acc;
}, {});
filteredRows.reduce<Record<string, ICaseValue>>((acc, [attributeId, countryId, yearId, value]) => {
// Create the key for grouping by year and country
const countryYearKey = `${countryId}-${yearId}`;
// Add the attribute and its value
if (acc[countryYearKey]) {
acc[countryYearKey][attributeMap[attributeId].name] = value;
} else {
console.warn("Incorrect country-year map");
}
return acc;
}, countryYearMap);

return Object.values(cases);
return Object.values(countryYearMap);
};
6 changes: 6 additions & 0 deletions src/data/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { IAttribute, ICaseValue } from "../types";

interface IAttrLike {
id: number;
}
Expand All @@ -16,3 +18,7 @@ export const makeMap = <T>(list: (T & {id: number})[]): Record<number, T> => {
return acc;
}, {});
};

export const isDataComplete = (cases: ICaseValue[], selectedAttributes: IAttribute[]) => {
return cases.every(c => selectedAttributes.every(a => c[a.name] !== undefined));
};

0 comments on commit 24388a2

Please sign in to comment.