From 2440bd4a39849ee336aabf7507572f2b604f78d8 Mon Sep 17 00:00:00 2001 From: pjanik Date: Mon, 9 Sep 2024 20:04:40 +0200 Subject: [PATCH 1/2] feat: show warning when some data are not available [PT-188233122] --- src/components/app.scss | 2 +- src/components/app.tsx | 7 ++++++- src/data/request.ts | 44 +++++++++++++++++++++++------------------ src/data/utils.ts | 6 ++++++ 4 files changed, 38 insertions(+), 21 deletions(-) diff --git a/src/components/app.scss b/src/components/app.scss index e45a93e..e186172 100755 --- a/src/components/app.scss +++ b/src/components/app.scss @@ -64,7 +64,7 @@ } } - &.done { + &.done, &.incomplete { font-weight: 600; } } diff --git a/src/components/app.tsx b/src/components/app.tsx index 6aed7e3..22cf6d3 100755 --- a/src/components/app.tsx +++ b/src/components/app.tsx @@ -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"; @@ -61,7 +63,7 @@ export const App = () => { }); await createItems(kDataContextName, cases); - setDataStatus("retrieved"); + setDataStatus(isDataComplete(cases, tableAttributes) ? "retrieved" : "incomplete"); }; return ( @@ -93,6 +95,9 @@ export const App = () => { { dataStatus === "retrieved" &&
Retrieved data
} + { + dataStatus === "incomplete" &&
Some data requested are not available
+ } diff --git a/src/data/request.ts b/src/data/request.ts index cb4a564..7e24e2e 100644 --- a/src/data/request.ts +++ b/src/data/request.ts @@ -48,29 +48,35 @@ export const requestData = async (options: IRequestDataOptions): Promise>((acc, [attributeId, countryId, yearId, value]) => { - const attribute = attributeMap[attributeId]; + const _countryIds = countryIds.length > 0 ? countryIds : countries.map(c => c.id); + const _yearIds = yearIds.length > 0 ? yearIds : years.map(y => y.id); + + const countryYearMap: Record = {}; + _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>((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); }; diff --git a/src/data/utils.ts b/src/data/utils.ts index f7b43ba..07bf5e8 100644 --- a/src/data/utils.ts +++ b/src/data/utils.ts @@ -1,3 +1,5 @@ +import { IAttribute, ICaseValue } from "../types"; + interface IAttrLike { id: number; } @@ -16,3 +18,7 @@ export const makeMap = (list: (T & {id: number})[]): Record => { return acc; }, {}); }; + +export const isDataComplete = (cases: ICaseValue[], selectedAttributes: IAttribute[]) => { + return cases.every(c => selectedAttributes.every(a => c[a.name] !== undefined)); +}; From a8863af19796f796ed54f97438fb25d3ea8e6ae5 Mon Sep 17 00:00:00 2001 From: pjanik Date: Tue, 10 Sep 2024 13:29:34 +0200 Subject: [PATCH 2/2] chore: address PR feedback --- src/data/request.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/data/request.ts b/src/data/request.ts index 7e24e2e..6027358 100644 --- a/src/data/request.ts +++ b/src/data/request.ts @@ -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 => { @@ -48,8 +51,8 @@ export const requestData = async (options: IRequestDataOptions): Promise 0 ? countryIds : countries.map(c => c.id); - const _yearIds = yearIds.length > 0 ? yearIds : years.map(y => y.id); + const _countryIds = countryIds.length > 0 ? countryIds : allCountryIds; + const _yearIds = yearIds.length > 0 ? yearIds : allYearIds; const countryYearMap: Record = {}; _countryIds.forEach(countryId => {