Skip to content

Commit

Permalink
feat: Support 2 letters code four countries (#938)
Browse files Browse the repository at this point in the history
* feat: Support 2 letters code four countries

* fix
  • Loading branch information
alexfauquette authored Apr 12, 2024
1 parent 2ef368a commit 091e1df
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/pages/eco-score/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import Loader from "../loader";
import { useSearchParams } from "react-router-dom";
import { localSettings } from "../../localeStorageManager";
import countryNames from "../../assets/countries.json";
import { getCountryId } from "../../utils/getCountryId";

const ecoScoreCards = [
{
Expand Down Expand Up @@ -154,7 +155,7 @@ export default function EcoScore() {
const localData = localSettings.fetch();
const [searchParams, setSearchParams] = useSearchParams();
const [selectedCountry, setSelectedCountry] = React.useState(
searchParams.get("cc") || localData["country"] || "en:france",
getCountryId(searchParams.get("cc")) || localData["country"] || "en:france",
);

React.useEffect(() => {
Expand Down
3 changes: 2 additions & 1 deletion src/pages/ingredients/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import ImageAnnotation from "./ImageAnnotation";
import { useSearchParams } from "react-router-dom";
import { localSettings } from "../../localeStorageManager";
import countryNames from "../../assets/countries.json";
import { getCountryId } from "../../utils/getCountryId";

function ProductInterface(props) {
const { product, next } = props;
Expand Down Expand Up @@ -143,7 +144,7 @@ export default function IngredientsPage() {
const localData = localSettings.fetch();
const [searchParams, setSearchParams] = useSearchParams();
const [selectedCountry, setSelectedCountry] = React.useState(
searchParams.get("cc") || localData["country"] || "en:france",
getCountryId(searchParams.get("cc")) || localData["country"] || "en:france",
);

React.useEffect(() => {
Expand Down
20 changes: 20 additions & 0 deletions src/utils/getCountryId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import countries from "../assets/countries.json";

/**
* Get the country id base on the two letter id
*/
export function getCountryId(country: string | null) {
if (!country || country.length !== 2) {
return country;
}

const upperCountry = country.toUpperCase();
const countryObject = countries.find(
({ countryCode }) => countryCode === upperCountry,
);

if (!countryObject || !countryObject.id) {
return country;
}
return countryObject.id;
}

0 comments on commit 091e1df

Please sign in to comment.