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

Fix ingredient game #1037

Draft
wants to merge 1 commit 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
16 changes: 7 additions & 9 deletions src/pages/ingredients/IngeredientDisplay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,19 @@ type ParsedIngredientsType = {
text: string;
vegan: BooleanEstimation;
vegetarian: BooleanEstimation;
is_in_taxonomy: 1 | 0
};

function getColor(ingredient: ParsedIngredientsType) {
if (ingredient.ciqual_proxy_food_code !== undefined) return "green";
if (ingredient.vegetarian !== undefined) return "lightgreen";
if (ingredient.ingredients !== undefined) return "blue";
return "orange";
if (ingredient.is_in_taxonomy) { return 'green' }
return 'red'

}

function getTitle(ingredient: ParsedIngredientsType) {
if (ingredient.ciqual_proxy_food_code !== undefined)
return "This ingredient has CIQUAL id";
if (ingredient.vegetarian !== undefined) return "recognised as a vegetarian";
if (ingredient.ingredients !== undefined) return "contains sub ingredients";
return `unknown ingredient: ${ingredient.text}"`;
if (ingredient.is_in_taxonomy) { return 'Ingredient dans la taxonomie' }
return 'Ingrédient inconnu'

}
function ColorText({
text,
Expand Down
12 changes: 6 additions & 6 deletions src/pages/ingredients/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export default function IngredientsPage() {
return country.countryCode;
}, [selectedCountry]);

const [data, removeHead, isLoading] = useData(selectedCountryCode);
const [data, removeHead, isLoading, hasError] = useData(selectedCountryCode);

return (
<React.Suspense fallback={<Loader />}>
Expand Down Expand Up @@ -193,11 +193,11 @@ export default function IngredientsPage() {
{/* <IngeredientDisplay /> */}
{isLoading ? (
"loading..."
) : data && data.length === 0 ? (
"No data"
) : (
<ProductInterface product={data[0]} next={removeHead} />
)}
) : data && data.length === 0 ?
"No data found" : hasError ? "An error occured"
: (
<ProductInterface product={data[0]} next={removeHead} />
)}

{/* <pre>{JSON.stringify(data, null, 2)}</pre> */}
</React.Suspense>
Expand Down
12 changes: 6 additions & 6 deletions src/pages/ingredients/useData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,14 @@ const formatData = (product) => {
const {
code,
lang,
image_ingredients_url,
product_name,
ingredient,
images,
scans_n,
...other
} = product;

const baseImageUrl = image_ingredients_url.replace(/ingredients.*/, "");
const baseImageUrl = `https://images.openfoodfacts.org/images/products/${off.getFormatedBarcode(code)}/`

const selectedImages = Object.keys(images)
.filter((key) => key.startsWith("ingredients"))
Expand Down Expand Up @@ -80,7 +79,6 @@ const formatData = (product) => {
code,
lang,
selectedImages,
image_ingredients_url,
product_name,
ingredient,
scans_n,
Expand All @@ -89,10 +87,11 @@ const formatData = (product) => {
};
};

export default function useData(countryCode): [any[], () => void, boolean] {
export default function useData(countryCode): [any[], () => void, boolean, boolean] {
const [data, setData] = React.useState([]);
const prevCountry = React.useRef(countryCode);
const [isLoading, setIsLoading] = React.useState(true);
const [hasError, setHasError] = React.useState(false);
const [page, setPage] = React.useState(() => {
return 0;
// Seems that API fails for large page number
Expand Down Expand Up @@ -137,6 +136,7 @@ export default function useData(countryCode): [any[], () => void, boolean] {
setIsLoading(false);
}
} catch (error) {
setHasError(true)
console.log(error);
}
};
Expand All @@ -154,9 +154,9 @@ export default function useData(countryCode): [any[], () => void, boolean] {
React.useEffect(() => {
// This is dummy but will be ok for a PoC
if (data.length < 5 && !isLoading) {
setPage((p) => p + 1);
// setPage((p) => p + 1);
}
}, [data, isLoading]);

return [data, removeHead, isLoading];
return [data, removeHead, isLoading, hasError];
}