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

Refacto search params management #1121

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
34 changes: 34 additions & 0 deletions src/hooks/useSearchParamsState.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import * as React from "react";
import { useSearchParams } from "react-router-dom";

export function useSearchParamsState<T>(
searchName: string,
defaultValue: T,
parser: (search: string) => T = (v: string) => v as T): [T, React.Dispatch<React.SetStateAction<T>>] {

const [searchParams, setSearchParams] = useSearchParams();
const [state, setState] = React.useState(() => {
const param = searchParams.get(searchName);
return param === null ? defaultValue : parser(param)
});

const updateState = React.useCallback(
(update: T) => {
setState(prev => {
const nextVal = typeof update === 'function' ? update(prev) : update;

if (nextVal === prev) {
return prev
}

setSearchParams((prevSearch) => {
prevSearch.set(searchName, nextVal);
return prevSearch;
});
return nextVal
})
},
[setSearchParams],
);
return [state, updateState]
}
7 changes: 4 additions & 3 deletions src/i18n/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"view": "View",
"edit": "Edit",
"remaining": "Remaining: {{count}}",
"remaining_other": "Remaining: >100",
"hide_images": "Hide images (faster loading)",
"display_images": "Display images",
"annotations": "Annotations",
Expand Down Expand Up @@ -325,7 +326,7 @@
"invalid_image": "Invalid image",
"pictures": {
"picture_date": "Photo uploaded",
"selected_as_nutrients": "Selected as nutriment image ✅",
"selected_as_nutrients": "Selected as nutriment image ✅",
"not_selected_as_nutrients": "Not selected as nutriment ❌"
},
"instructions": {
Expand All @@ -334,7 +335,7 @@
"chose_between_empty_and_partially_filled": "You can chose between products for which OpenFoodFacts (OFF) has no data about the nutrients. Or product with partially filled nutrient table.",
"picture_date_is_display": "The date of the picture is displayed on top of it. Allowing you to check if it's a recent one.",
"nutriments_from_off_are_displayed": "You can display data that are already present in the OFF database. They will appear under the inputs:",
"green_for_same": "With <1>green</1> for those matching with the input value.",
"green_for_same": "With <1>green</1> for those matching with the input value.",
"orange_for_empty": "With <1>orange</1> for those with empty input value.",
"red_for_different": "With <1>red</1> for those with different input value.",
"indicate_not_provided_value": "Empty inputs are ignored. If fibers are not present in the table, you can put \"-\" to specify the value is not available.",
Expand Down Expand Up @@ -394,4 +395,4 @@
"subtitle": "Download"
}
}
}
}
7 changes: 4 additions & 3 deletions src/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"view": "View",
"edit": "Edit",
"remaining": "Remaining: {{count}}",
"remaining_other": "Remaining: >100",
"hide_images": "Hide images (faster loading)",
"display_images": "Display images",
"annotations": "Annotations",
Expand Down Expand Up @@ -325,7 +326,7 @@
"invalid_image": "Invalid image",
"pictures": {
"picture_date": "Photo uploaded",
"selected_as_nutrients": "Selected as nutriment image ✅",
"selected_as_nutrients": "Selected as nutriment image ✅",
"not_selected_as_nutrients": "Not selected as nutriment ❌"
},
"instructions": {
Expand All @@ -334,7 +335,7 @@
"chose_between_empty_and_partially_filled": "You can chose between products for which OpenFoodFacts (OFF) has no data about the nutrients. Or product with partially filled nutrient table.",
"picture_date_is_display": "The date of the picture is displayed on top of it. Allowing you to check if it's a recent one.",
"nutriments_from_off_are_displayed": "You can display data that are already present in the OFF database. They will appear under the inputs:",
"green_for_same": "With <1>green</1> for those matching with the input value.",
"green_for_same": "With <1>green</1> for those matching with the input value.",
"orange_for_empty": "With <1>orange</1> for those with empty input value.",
"red_for_different": "With <1>red</1> for those with different input value.",
"indicate_not_provided_value": "Empty inputs are ignored. If fibers are not present in the table, you can put \"-\" to specify the value is not available.",
Expand Down Expand Up @@ -394,4 +395,4 @@
"subtitle": "Download"
}
}
}
}
3 changes: 0 additions & 3 deletions src/pages/nutrition/NutrimentCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,6 @@ function getLegendColor(productValue, value, productUnit, unit) {
return 'green'
}
return 'red'

// Should never reach that part.
return undefined;
}

export const NutrimentCell = (props: NutrimentCellProps) => {
Expand Down
6 changes: 4 additions & 2 deletions src/pages/nutrition/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ import Instructions from "./Instructions";
import useNutrimentTranslations from "./useNutrimentTranslations";
import { useCountry } from "../../contexts/CountryProvider";
import { useTranslation } from "react-i18next";
import { useSearchParamsState } from "../../hooks/useSearchParamsState";

export default function Nutrition() {
const { t } = useTranslation();
const [partiallyFilled, setPartiallyFilled] = React.useState(false);
const [displayOFFValue, setDisplayOFFValue] = React.useState(false);
const [partiallyFilled, setPartiallyFilled] = useSearchParamsState('partiallyFilled', false, Boolean);
const [displayOFFValue, setDisplayOFFValue] = useSearchParamsState('displayValue', false, Boolean);

const handlePartiallyFilled = (_, checked) => {
setPartiallyFilled(checked);
setDisplayOFFValue(checked);
Expand Down
2 changes: 1 addition & 1 deletion src/pages/nutrition/useRobotoffPredictions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export function useRobotoffPredictions(partiallyFilled: boolean) {

const nextItem = React.useCallback(() => {
setInsightIndex((p) => p + 1);
setCount((p) => p - 1);
setCount((p) => p === 100 ? p : p - 1);
}, []);

const insight = insights.data[insightIndex];
Expand Down
Loading