Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make world an empty country code #1100

Merged
merged 5 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion src/pages/nutrition/NutrimentCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export const NutrimentCell = (props: NutrimentCellProps) => {
{isValidUnit(unit) ? (
<select
style={{ width: 55 }}
value={unit}
value={unit || ""}
tabIndex={tabIndex}
onChange={(event) => {
setValues((p) => ({
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 @@ -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([]);
Expand Down Expand Up @@ -215,7 +218,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
Loading