From 18483aa8204e634fafaac6fb63817f3d3cfc7059 Mon Sep 17 00:00:00 2001 From: Alexandre Fauquette <45398769+alexfauquette@users.noreply.github.com> Date: Mon, 22 Jul 2024 20:34:32 +0200 Subject: [PATCH] Fix robotoff API call for countries (#1002) * ingredients fix * use new robotoff sort option * prettier * updat countries * fix lower case --- .github/labeler.yml | 36 +++++++++++++++--------------- src/pages/ingredients/index.tsx | 1 + src/robotoff.ts | 39 +++++++++++++++++++++------------ 3 files changed, 44 insertions(+), 32 deletions(-) diff --git a/.github/labeler.yml b/.github/labeler.yml index 861b8a0b1..5551e0646 100644 --- a/.github/labeler.yml +++ b/.github/labeler.yml @@ -1,32 +1,32 @@ # Add labels to any any pull request with changes to the specified paths Github Actions: -- changed-files: - - any-glob-to-any-file: '.github/**/*' + - changed-files: + - any-glob-to-any-file: ".github/**/*" Questions game: -- changed-files: - - any-glob-to-any-file: 'src/pages/questions/**/*' + - changed-files: + - any-glob-to-any-file: "src/pages/questions/**/*" 🖼️ Logo game: -- changed-files: - - any-glob-to-any-file: 'src/pages/logos/**/*' - - any-glob-to-any-file: 'src/components/LogoForm.jsx' - - any-glob-to-any-file: 'src/pages/logos/LogoAnnotation.jsx' - - any-glob-to-any-file: 'src/pages/logos/LogoDeepSearch.jsx' + - changed-files: + - any-glob-to-any-file: "src/pages/logos/**/*" + - any-glob-to-any-file: "src/components/LogoForm.jsx" + - any-glob-to-any-file: "src/pages/logos/LogoAnnotation.jsx" + - any-glob-to-any-file: "src/pages/logos/LogoDeepSearch.jsx" Insights: -- changed-files: - - any-glob-to-any-file: 'src/pages/insights/**/*' + - changed-files: + - any-glob-to-any-file: "src/pages/insights/**/*" 🌱 Eco-Score game: -- changed-files: - - any-glob-to-any-file: 'src/pages/eco-score/**/*' + - changed-files: + - any-glob-to-any-file: "src/pages/eco-score/**/*" i18n: -- changed-files: - - any-glob-to-any-file: 'src/i18n/**/*' + - changed-files: + - any-glob-to-any-file: "src/i18n/**/*" Nutrition game: -- changed-files: - - any-glob-to-any-file: 'src/pages/nutrition/**/*' - - any-glob-to-any-file: 'src/pages/nutrition/index.jsx' + - changed-files: + - any-glob-to-any-file: "src/pages/nutrition/**/*" + - any-glob-to-any-file: "src/pages/nutrition/index.jsx" diff --git a/src/pages/ingredients/index.tsx b/src/pages/ingredients/index.tsx index 36dd6a730..d52209e3e 100644 --- a/src/pages/ingredients/index.tsx +++ b/src/pages/ingredients/index.tsx @@ -23,6 +23,7 @@ import { useSearchParams } from "react-router-dom"; import { localSettings } from "../../localeStorageManager"; import countryNames from "../../assets/countries.json"; import { getCountryId } from "../../utils/getCountryId"; +import { OFF_URL } from "../../const"; function ProductInterface(props) { const { product, next } = props; diff --git a/src/robotoff.ts b/src/robotoff.ts index 5c5a22aff..08b5b0c84 100644 --- a/src/robotoff.ts +++ b/src/robotoff.ts @@ -1,6 +1,7 @@ import axios from "axios"; import { ROBOTOFF_API_URL, IS_DEVELOPMENT_MODE, OFF_IMAGE_URL } from "./const"; import { getLang } from "./localeStorageManager"; +import COUNTRIES from "./assets/countries.json"; import { reformatValueTag, removeEmptyKeys } from "./utils"; export interface QuestionInterface { @@ -16,6 +17,17 @@ export interface QuestionInterface { type GetQuestionsResponse = { count: number; questions: QuestionInterface[] }; +function countryId2countryCode(id: string | null) { + if (id === null) { + return null; + } + const code = COUNTRIES.find((c) => c.id === id)?.countryCode; + if (code) { + return code.toLowerCase(); + } + return code; +} + const robotoff = { annotate(insightId: string, annotation) { if (IS_DEVELOPMENT_MODE) { @@ -64,26 +76,24 @@ const robotoff = { insight_types: insightType, value_tag: valueTag, brands: reformatValueTag(brandFilter), - country: countryFilter !== "en:world" ? countryFilter : null, + countries: countryId2countryCode( + countryFilter !== "en:world" ? countryFilter : null, + ), campaign, predictor, + order_by: sortByPopularity ? "popularity" : "random", }; const lang = getLang(); - return axios.get( - `${ROBOTOFF_API_URL}/questions/${ - sortByPopularity ? "popular" : "random" - }`, - { - params: removeEmptyKeys({ - ...searchParams, - lang, - count, - page, - }), - }, - ); + return axios.get(`${ROBOTOFF_API_URL}/questions/`, { + params: removeEmptyKeys({ + ...searchParams, + lang, + count, + page, + }), + }); }, insightDetail(insight_id) { @@ -231,6 +241,7 @@ const robotoff = { return axios.get( `${ROBOTOFF_API_URL}/questions/unanswered/?${Object.keys({ ...params, + countries: countryId2countryCode(params.country), page, }) .filter((key) => params[key] !== undefined)