Skip to content

Commit

Permalink
fix: Avoid loading logos without a filtering defined (#742)
Browse files Browse the repository at this point in the history
Co-authored-by: Pierre Slamich <[email protected]>
  • Loading branch information
alexfauquette and teolemon authored Oct 4, 2023
1 parent e59ac10 commit 9a86c3f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
1 change: 0 additions & 1 deletion src/components/LogoSearchForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ const LogoSearchForm = (props) => {
return (
<Stack direction="column" spacing={{ xs: 1, sm: 2, md: 4 }} {...other}>
<Stack direction={{ xs: "column", sm: "row" }} spacing={1} wrap="wrap">
<p>{innerType}</p>
<TextField
fullWidth
value={innerType}
Expand Down
11 changes: 10 additions & 1 deletion src/pages/logos/LogoSearch.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const request = async ({ barcode, value, type, count }) => {
export default function LogoSearch() {
const { t } = useTranslation();

const [isLoading, setIsLoading] = React.useState(true);
const [isLoading, setIsLoading] = React.useState(false);
const [searchState, setSearchState] = useUrlParams(
{
type: "",
Expand All @@ -61,7 +61,16 @@ export default function LogoSearch() {
[setSearchState]
);

const filterStateHasValue =
(searchState.type &&
(TYPE_WITHOUT_VALUE.includes(searchState.type) || searchState.value)) ||
searchState.barcode;

React.useEffect(() => {
if (!filterStateHasValue) {
// Avoid fetching data if no value to filter
return () => {};
}
let isValidRequest = true;
setIsLoading(true);
setResult({ logos: [], count: undefined });
Expand Down
19 changes: 17 additions & 2 deletions src/pages/logos/ProductLogoAnnotations.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ const OFF_2_ROBOTOFF = {
labels: "label",
packaging: "packaging",
};
const ROBOTOFF_2_OFF = {
category: "categories",
label: "labels",
packaging: "packaging",
};

const fetchProducts = async ({ page, filter }) => {
try {
const {
Expand Down Expand Up @@ -87,6 +93,12 @@ const useLogoFetching = (filter) => {
}, [filter]);

React.useEffect(() => {
const filterStateIsIncomplet = !filter.tagtype || !filter.tag;
if (filterStateIsIncomplet) {
// Avoid fetching data if no value to filter
return () => {};
}

let isValid = true;
setIsLoading(true);
setCanLoadMore(false);
Expand Down Expand Up @@ -181,7 +193,7 @@ export default function AnnotateLogosFromProducts() {
{
tagtype: "labels",
tag_contains: "contains",
tag: "en:eg-oko-verordnung",
tag: "",
},
{
tag: ["valueTag", "value_tag", "value"],
Expand Down Expand Up @@ -228,7 +240,10 @@ export default function AnnotateLogosFromProducts() {
sx={{ minWidth: 200 }}
>
{logoTypeOptions.map(({ value: typeValue, labelKey }) => (
<MenuItem key={typeValue} value={typeValue}>
<MenuItem
key={typeValue}
value={ROBOTOFF_2_OFF[typeValue] ?? typeValue}
>
{t(labelKey)}
</MenuItem>
))}
Expand Down

0 comments on commit 9a86c3f

Please sign in to comment.