From ada6361e14da6ff3046997d4fc5e9e367f78b99a Mon Sep 17 00:00:00 2001 From: alex Date: Thu, 12 Dec 2024 13:47:18 +0100 Subject: [PATCH 1/5] make world an empty country code --- src/assets/countries.json | 2 +- src/contexts/CountryProvider/CountryProvider.tsx | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/assets/countries.json b/src/assets/countries.json index 4987938ec..91e69b3d6 100644 --- a/src/assets/countries.json +++ b/src/assets/countries.json @@ -1485,7 +1485,7 @@ "id": "en:world", "label": "World", "languageCode": "en", - "countryCode": "world" + "countryCode": "" }, { "id": "en:yemen", diff --git a/src/contexts/CountryProvider/CountryProvider.tsx b/src/contexts/CountryProvider/CountryProvider.tsx index dcffa8a0e..d76d9f917 100644 --- a/src/contexts/CountryProvider/CountryProvider.tsx +++ b/src/contexts/CountryProvider/CountryProvider.tsx @@ -7,7 +7,7 @@ import countries from "../../assets/countries.json"; const ValidCountryCodes = new Set(countries.map((c) => c.countryCode)); export function CountryProvider({ children }) { - const [country, setCountry] = useLocalStorageState("country", "world"); + const [country, setCountry] = useLocalStorageState("country", ""); const [searchParams, setSearchParams] = useSearchParams(); const searchParamsCountry = searchParams.get("country")?.toLowerCase(); From fd9638168d6f8c4fdcd29ef1bbb3f0ae356b6d97 Mon Sep 17 00:00:00 2001 From: alex Date: Fri, 13 Dec 2024 00:11:06 +0100 Subject: [PATCH 2/5] update country management --- .../CountryProvider/CountryProvider.tsx | 13 +++++++-- update-countries.js | 28 ++++++++++++------- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/src/contexts/CountryProvider/CountryProvider.tsx b/src/contexts/CountryProvider/CountryProvider.tsx index d76d9f917..2d8a4a6ea 100644 --- a/src/contexts/CountryProvider/CountryProvider.tsx +++ b/src/contexts/CountryProvider/CountryProvider.tsx @@ -26,9 +26,16 @@ export function CountryProvider({ children }) { ); const value = React.useMemo(() => { - const lowercasedCountry = ( - ValidCountryCodes.has(searchParamsCountry) ? searchParamsCountry : country - )?.toLocaleLowerCase(); + // Try from: + // - searchParams + // - localStorage + // - empty + + const lowercasedCountry = ValidCountryCodes.has(searchParamsCountry) + ? searchParamsCountry + : ValidCountryCodes.has(country?.toLocaleLowerCase()) + ? country?.toLocaleLowerCase() + : ""; return { country: lowercasedCountry, diff --git a/update-countries.js b/update-countries.js index a1a75c1f2..0d386a96e 100644 --- a/update-countries.js +++ b/update-countries.js @@ -7,20 +7,28 @@ axios("https://static.openfoodfacts.org/data/taxonomies/countries.json") "./src/assets/countries.json", JSON.stringify( Object.entries(data) - .map(([key, value]) => ({ - id: key, - label: value.name.en, - languageCode: + .map(([key, value]) => { + let countryCode = + value.country_code_2 === undefined + ? undefined + : value.country_code_2.en.toLowerCase(); + if (countryCode === "world") { + countryCode = ""; + } + + const languageCode = value.languages === undefined ? "en" : value.languages.en === undefined ? undefined - : value.languages.en.split(",")[0], - countryCode: - value.country_code_2 === undefined - ? undefined - : value.country_code_2.en.toLowerCase(), - })) + : value.languages.en.split(",")[0]; + return { + id: key, + label: value.name.en, + languageCode, + countryCode, + }; + }) .filter((country) => country.countryCode !== undefined) .sort((a, b) => a.label.localeCompare(b.label)), ), From 72e48c63953b93d9c4f4b5beebb749a995b96b2b Mon Sep 17 00:00:00 2001 From: alex Date: Fri, 13 Dec 2024 00:11:26 +0100 Subject: [PATCH 3/5] fix console errors --- src/pages/nutrition/NutrimentCell.tsx | 2 +- src/pages/nutrition/index.tsx | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/pages/nutrition/NutrimentCell.tsx b/src/pages/nutrition/NutrimentCell.tsx index 696a37261..06b26697b 100644 --- a/src/pages/nutrition/NutrimentCell.tsx +++ b/src/pages/nutrition/NutrimentCell.tsx @@ -74,7 +74,7 @@ export const NutrimentCell = (props: NutrimentCellProps) => { {isValidUnit(unit) ? ( setCountry(event.target.value, "global")} + value={country || "world"} + onChange={(event) => + setCountry( + event.target.value === "world" ? "" : event.target.value, + "global", + ) + } variant="outlined" sx={{ fieldset: { border: "none" } }} > - {countryNames - .filter((country) => country.countryCode) - .map((country) => ( - - {country.label} ({country.countryCode}) - - ))} + {countryNames.map(({ label, countryCode }) => ( + + {label} + {countryCode && ` (${countryCode})`} + + ))} Date: Fri, 13 Dec 2024 00:28:16 +0100 Subject: [PATCH 5/5] show off data by default --- src/pages/nutrition/index.tsx | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/pages/nutrition/index.tsx b/src/pages/nutrition/index.tsx index 0840c0778..788387915 100644 --- a/src/pages/nutrition/index.tsx +++ b/src/pages/nutrition/index.tsx @@ -20,7 +20,10 @@ import { NUTRIMENTS_ORDER } from "./config"; export default function Nutrition() { const [partiallyFilled, setPartiallyFilled] = React.useState(false); const [displayOFFValue, setDisplayOFFValue] = React.useState(false); - const handlePartiallyFilled = (_, checked) => setPartiallyFilled(checked); + const handlePartiallyFilled = (_, checked) => { + setPartiallyFilled(checked); + setDisplayOFFValue(checked); + }; const handleDisplayOFFValue = (_, checked) => setDisplayOFFValue(checked); const [additionalIds, setAdditionalIds] = React.useState([]);