Skip to content

Commit

Permalink
fix: Avoid duplicated nutriments
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfauquette committed Dec 13, 2024
1 parent ad494f6 commit 5a8156f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
19 changes: 13 additions & 6 deletions src/pages/nutrition/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ export default function Nutrition() {
};
const handleDisplayOFFValue = (_, checked) => setDisplayOFFValue(checked);

const [additionalIds, setAdditionalIds] = React.useState([]);

const { isLoading, insight, nextItem, count, product } =
useRobotoffPredictions(partiallyFilled);

Expand All @@ -38,7 +36,6 @@ export default function Nutrition() {
const apiRef = React.useRef<ReactZoomPanPinchRef>();

React.useEffect(() => {
setAdditionalIds([]);
if (!insight || typeof insight === "string") {
setValues({});
return;
Expand Down Expand Up @@ -70,8 +67,8 @@ export default function Nutrition() {
}, [insight]);

const nutrimentsDisplayed = React.useMemo(
() => structurePredictions(values, product, additionalIds),
[values, product, additionalIds],
() => structurePredictions(values, product),
[values, product],
);

const notUsedNutriments = React.useMemo(
Expand Down Expand Up @@ -217,7 +214,17 @@ export default function Nutrition() {
value=""
tabIndex={2}
onChange={(event) => {
setAdditionalIds((p) => [...p, event.target.value]);
setValues((prev) => ({
...prev,
[`${event.target.value}_100g`]: {
value: "",
unit: "g",
},
[`${event.target.value}_serving`]: {
value: "",
unit: "g",
},
}));
}}
>
<option disabled value="">
Expand Down
3 changes: 1 addition & 2 deletions src/pages/nutrition/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export function isValidUnit(unit: string | null) {
export function structurePredictions(
predictions: Record<string, Pick<NutrimentPrediction, "value" | "unit">>,
productValue?: { nutriments?: Record<string, string | number> },
additionalIds?: string[],
) {
const nurimentsIds = Object.keys(NUTRIMENTS);

Expand All @@ -51,7 +50,7 @@ export function structurePredictions(
}
});

return [...nurimentsIds, ...additionalIds];
return nurimentsIds;
}

interface PostRobotoffParams {
Expand Down

0 comments on commit 5a8156f

Please sign in to comment.