Skip to content

Commit

Permalink
return when too many requests
Browse files Browse the repository at this point in the history
  • Loading branch information
jwkaterina committed Feb 28, 2024
1 parent 364c101 commit d560038
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 16 deletions.
4 changes: 2 additions & 2 deletions functions/api-controllers/foodDatabaseAPI-controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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);
}
}
Expand Down
34 changes: 24 additions & 10 deletions src/app/components/cards/food-cards/open-foodcard.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
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';
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 {
Expand All @@ -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<Nutrients | null>(null);
const [quantity, setQuantity] = useState<number>(1);
const [selectedOption, setSelectedOption] = useState<string>('Value pre 100g');
Expand Down Expand Up @@ -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(
Expand All @@ -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 (
<div className={styles.card_grid}>
<FoodHeaderCard
Expand All @@ -88,16 +102,16 @@ const OpenFoodCard = ({ food }: OpenFoodCardProps): JSX.Element => {
quantity={quantity}
setQuantity={setQuantity}
/>
{content && <DailyValueCard content={content} />}
<DailyValueCard content={content} />
<CompositionCard
protein={food.food.nutrients.PROCNT}
carbs={food.food.nutrients.CHOCDF}
fat={food.food.nutrients.FAT}
/>
{content && <BigNutrientsCard content={content} />}
{content && <VitaminsCard content={content} />}
{content && <MineralsCard content={content} />}
{content && <FatsCard content={content} />}
<BigNutrientsCard content={content} />
<VitaminsCard content={content} />
<MineralsCard content={content} />
<FatsCard content={content} />
</div>
);
}
Expand Down
11 changes: 7 additions & 4 deletions src/app/context/card-context.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createContext, useState } from "react";
import { createContext, useState, useMemo } from "react";
import { CardState } from "../types/types";

interface CardContextProps {
Expand All @@ -15,11 +15,14 @@ export const CardOpenProvider = ({ children }: any) => {

const [cardOpen, setCardOpen] = useState<CardState>(CardState.CLOSED);

return (
<CardOpenContext.Provider value={{
const contextValue = useMemo(() => ({
cardOpen,
setCardOpen
}}>
}), [cardOpen, setCardOpen]
)

return (
<CardOpenContext.Provider value={contextValue}>
{children}
</CardOpenContext.Provider>
);
Expand Down

0 comments on commit d560038

Please sign in to comment.