diff --git a/functions/api-controllers/foodDatabaseAPI-controllers.js b/functions/api-controllers/foodDatabaseAPI-controllers.js index da65943..ba36e9e 100644 --- a/functions/api-controllers/foodDatabaseAPI-controllers.js +++ b/functions/api-controllers/foodDatabaseAPI-controllers.js @@ -45,7 +45,7 @@ const parseQuery = async (req, res, next) => { res.status(200).json(result); } catch (err) { console.error(err); - const error = HttpError('Could not find any results', 404); + const error = new HttpError('Could not find any results', 404); return next(error); } } @@ -77,7 +77,7 @@ const findNutrients = async (req, res, next) => { res.status(200).json(result); } catch (err) { console.error(err); - const error = new HttpError('Could not fetch nutrients', 404); + const error = new HttpError('Could analyze food, wait 1 munit and try again', 404); return next(error); } } diff --git a/src/app/components/cards/food-cards/open-foodcard.tsx b/src/app/components/cards/food-cards/open-foodcard.tsx index f3f317f..76d6cf6 100644 --- a/src/app/components/cards/food-cards/open-foodcard.tsx +++ b/src/app/components/cards/food-cards/open-foodcard.tsx @@ -1,4 +1,4 @@ -import { useEffect, useState } from 'react'; +import { useContext, useEffect, useState } from 'react'; import BigNutrientsCard from '../../analysis_cards/bignutrients_card'; import CompositionCard from '../../analysis_cards/composition_card'; import DailyValueCard from '../../analysis_cards/dailyvalue_card'; @@ -6,8 +6,10 @@ import FatsCard from '../../analysis_cards/fats_card'; import FoodHeaderCard from './header-foodcard'; import MineralsCard from '../../analysis_cards/minerals_card'; import VitaminsCard from '../../analysis_cards/vitamins_card'; +import { CardOpenContext } from '@/app/context/card-context'; +import { StatusContext } from '@/app/context/status-context'; import { useHttpClient } from '@/app/hooks/http-hook'; -import { Food, Nutrients } from '@/app/types/types'; +import { CardState, Food, Nutrients, StatusType } from '@/app/types/types'; import styles from '../card.module.css'; interface OpenFoodCardProps { @@ -17,7 +19,8 @@ interface OpenFoodCardProps { const OpenFoodCard = ({ food }: OpenFoodCardProps): JSX.Element => { const { sendRequest } = useHttpClient(); - + const { setStatus, setMessage } = useContext(StatusContext); + const { setCardOpen } = useContext(CardOpenContext); const [content, setContent] = useState(null); const [quantity, setQuantity] = useState(1); const [selectedOption, setSelectedOption] = useState('Value pre 100g'); @@ -53,11 +56,14 @@ const OpenFoodCard = ({ food }: OpenFoodCardProps): JSX.Element => { measure: gramUri, quantity: 100 }), - { 'Content-Type': 'application/json' } + { 'Content-Type': 'application/json' }, + true, false ); setContent(nutrients); return; - } catch (err) {} + } catch (err) { + console.log(err); + } } try { const nutrients: Nutrients = await sendRequest( @@ -78,6 +84,14 @@ const OpenFoodCard = ({ food }: OpenFoodCardProps): JSX.Element => { }, [quantity, selectedOption]); + if(!content || content.totalDaily || !content.totalNutrients) { + setCardOpen(CardState.CLOSED); + console.error('Too many request'); + setStatus(StatusType.ERROR); + setMessage('Could not analyse food, wait 1 minut and try again.'); + return <>; + } + return (
{ quantity={quantity} setQuantity={setQuantity} /> - {content && } + - {content && } - {content && } - {content && } - {content && } + + + +
); } diff --git a/src/app/context/card-context.tsx b/src/app/context/card-context.tsx index 65cdbdd..5e645ce 100644 --- a/src/app/context/card-context.tsx +++ b/src/app/context/card-context.tsx @@ -1,4 +1,4 @@ -import { createContext, useState } from "react"; +import { createContext, useState, useMemo } from "react"; import { CardState } from "../types/types"; interface CardContextProps { @@ -15,11 +15,14 @@ export const CardOpenProvider = ({ children }: any) => { const [cardOpen, setCardOpen] = useState(CardState.CLOSED); - return ( - ({ cardOpen, setCardOpen - }}> + }), [cardOpen, setCardOpen] + ) + + return ( + {children} );