Skip to content

Commit

Permalink
Merge branch 'master' into restrain-nutriments
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfauquette authored Dec 12, 2024
2 parents 57185d1 + dc9c601 commit 2eb0d24
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/assets/countries.json
Original file line number Diff line number Diff line change
Expand Up @@ -1485,7 +1485,7 @@
"id": "en:world",
"label": "World",
"languageCode": "en",
"countryCode": "world"
"countryCode": ""
},
{
"id": "en:yemen",
Expand Down
25 changes: 13 additions & 12 deletions src/components/ResponsiveAppBar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -413,21 +413,22 @@ const ResponsiveAppBar = () => {
}}
>
<Select
value={country}
onChange={(event) => 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) => (
<MenuItem
value={country.countryCode}
key={country.countryCode}
>
{country.label} ({country.countryCode})
</MenuItem>
))}
{countryNames.map(({ label, countryCode }) => (
<MenuItem value={countryCode || "world"} key={countryCode}>
{label}
{countryCode && ` (${countryCode})`}
</MenuItem>
))}
</Select>
<IconButton
color="inherit"
Expand Down
15 changes: 11 additions & 4 deletions src/contexts/CountryProvider/CountryProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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,
Expand Down
7 changes: 5 additions & 2 deletions src/pages/nutrition/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ import Instructions from "./Instructions";
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([]);
Expand Down Expand Up @@ -217,7 +220,7 @@ export default function Nutrition() {
setAdditionalIds((p) => [...p, event.target.value]);
}}
>
<option disabled selected value="">
<option disabled value="">
-- add nutriment --
</option>
{notUsedNutriments.map((nutriId) => (
Expand Down
28 changes: 18 additions & 10 deletions update-countries.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
),
Expand Down

0 comments on commit 2eb0d24

Please sign in to comment.