Skip to content

Commit

Permalink
Improve ingredients (#894)
Browse files Browse the repository at this point in the history
fixes
  • Loading branch information
alexfauquette committed Feb 21, 2024
1 parent 5f92e6a commit 7d53a96
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 40 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-i18next": "^12.3.1",
"react-map-interaction": "^2.1.0",
"react-redux": "^8.1.3",
"react-router-dom": "^6.18.0",
"react-zoom-pan-pinch": "^2.5.0",
Expand Down
20 changes: 6 additions & 14 deletions src/off.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
OFF_API_URL_V2,
OFF_IMAGE_URL,
OFF_SEARCH,
OFF_URL,
} from "./const";
import axios from "axios";
import combineURLs from "axios/lib/helpers/combineURLs";
Expand Down Expand Up @@ -164,11 +163,12 @@ const offService = {
console.error("setIngedrient: Missing text");
}

const urlParams = new URLSearchParams({
code,
[`ingredients_text${lang ? `_${lang}` : ""}`]: text,
});
return `${OFF_URL}/cgi/product_jqm2.pl?${urlParams.toString()}`;
return axios.patch(
`https://world.openfoodfacts.org/api/v3/product/${code}`,
{
product: { [`ingredients_text${lang ? `_${lang}` : ""}`]: text },
},
);
},

async getIngedrientParsing(editionParams: { text: string; lang: string }) {
Expand All @@ -190,11 +190,3 @@ const offService = {
};

export default offService;

// Fetching products to annotate:

// https://world.openfoodfacts.org/cgi/search.pl?page=0&page_size=25&json=true&action=process&fields=code,lang,image_ingredients_url,product_name,ingredient,images&tagtype_0=states&tag_contains_0=contains&tag_0=en%3Aingredients-to-be-completed&tagtype_1=states&tag_contains_1=contains&tag_1=en%3Aingredients-photo-selected

// Getting prediction:
// https://robotoff.openfoodfacts.org/api/v1/predict/ingredient_list?ocr_url=https://images.openfoodfacts.org/images/products/505/382/713/9229/41.json
// https://images.openfoodfacts.org/images/products/505/382/713/9229/41.json
4 changes: 1 addition & 3 deletions src/pages/ingredients/IngeredientDisplay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,7 @@ export function IngredientAnotation(props) {
{t("ingredients.parsing")}
</LoadingButton>
<Button
component="a"
target="_blank"
href={text && off.setIngedrient({ code, lang, text })}
onClick={() => off.setIngedrient({ code, lang, text })}
variant="contained"
disabled={!text}
color="success"
Expand Down
86 changes: 64 additions & 22 deletions src/pages/ingredients/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import Tab from "@mui/material/Tab";
import TabContext from "@mui/lab/TabContext";
import TabList from "@mui/lab/TabList";
import TabPanel from "@mui/lab/TabPanel";
import Link from "@mui/material/Link";

import { MapInteractionCSS } from "react-map-interaction";

import Loader from "../loader";
import off from "../../off";
Expand Down Expand Up @@ -55,28 +58,67 @@ function ProductInterface(props) {
})}
</TabList>
</Box>
{selectedImages.map(({ countryCode, imageUrl, fetchDataUrl }) => {
return (
<TabPanel value={countryCode} key={`${code}-${countryCode}`}>
<Stack direction="row">
<img
src={imageUrl}
style={{
width: "50%",
objectFit: "contain",
maxHeight: "60vh",
}}
/>
<ImageAnnotation
fetchDataUrl={fetchDataUrl}
code={code as string}
imageLang={countryCode}
offText={product[`ingredients_text_${countryCode}`] ?? ""}
/>
</Stack>
</TabPanel>
);
})}
{selectedImages.map(
({
countryCode,
imageUrl,
fetchDataUrl,
uploaded_t,
uploader,
}) => {
return (
<TabPanel value={countryCode} key={`${code}-${countryCode}`}>
<Stack direction="row">
<Box sx={{ width: "50%", height: "60vh" }}>
<MapInteractionCSS
showControls
minScale={0.5}
translationBounds={{
xMax: 100,
yMax: 100,
}}
>
<img
src={imageUrl}
style={{
width: "50%",
objectFit: "contain",
}}
/>
</MapInteractionCSS>

<Typography sx={{ textAlign: "center" }}>
<Link
href={`https://world.openfoodfacts.org/contributor/${uploader}`}
target="_blank"
rel="noreferrer"
>
{uploader}
</Link>{" "}
{uploaded_t &&
new Date(uploaded_t * 1000).toLocaleDateString(
undefined,
{
year: "numeric",
month: "short",
day: "numeric",
},
)}
</Typography>
</Box>
<ImageAnnotation
fetchDataUrl={fetchDataUrl}
code={code as string}
imageLang={countryCode}
offText={
product[`ingredients_text_${countryCode}`] ?? ""
}
/>
</Stack>
</TabPanel>
);
},
)}
</TabContext>
)}
</Stack>
Expand Down
6 changes: 5 additions & 1 deletion src/pages/ingredients/useData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,17 @@ const formatData = (product) => {
? key.slice("ingredients_".length)
: "";

const { uploaded_t, uploader } = images[imageData.imgid];
return {
imgId: imageData.imgid,
countryCode,
imageUrl: getImageUrl(baseImageUrl, imageData.imgid, "400"),
imageUrl: getImageUrl(baseImageUrl, imageData.imgid, "full"),
fetchDataUrl: getIngredientExtractionUrl(
baseImageUrl.replace("images.", "static."),
imageData.imgid,
),
uploaded_t,
uploader,
x: Number.parseFloat(x),
y: Number.parseFloat(y),
w: Number.parseFloat(imageData.sizes.full.w),
Expand Down Expand Up @@ -107,6 +110,7 @@ export default function useData(): [any[], () => void, boolean] {
pageSize: 25,
filters: imagesToRead,
fields: "all",
countryCode: "ch",
});
if (isValid) {
const rep = products
Expand Down
11 changes: 11 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3033,6 +3033,7 @@ __metadata:
react: ^18.2.0
react-dom: ^18.2.0
react-i18next: ^12.3.1
react-map-interaction: ^2.1.0
react-redux: ^8.1.3
react-router-dom: ^6.18.0
react-zoom-pan-pinch: ^2.5.0
Expand Down Expand Up @@ -4343,6 +4344,16 @@ __metadata:
languageName: node
linkType: hard

"react-map-interaction@npm:^2.1.0":
version: 2.1.0
resolution: "react-map-interaction@npm:2.1.0"
peerDependencies:
prop-types: ">=15.0.0"
react: ">=16.3.0"
checksum: 5bf77b57776a26b175dc7f21afe6b8e5a79b0979820bb9e671aa5fd4e65c9438f9fc77ceb9d58ff54812083f1b60230b957757af4674f2ac0e8ad865aeadf792
languageName: node
linkType: hard

"react-redux@npm:^8.1.3":
version: 8.1.3
resolution: "react-redux@npm:8.1.3"
Expand Down

0 comments on commit 7d53a96

Please sign in to comment.