From 6f1ae88cb41eb985832114a7f5520e0f1318889d Mon Sep 17 00:00:00 2001 From: Ruidy Date: Mon, 5 Apr 2021 10:49:08 +0200 Subject: [PATCH] get meal with context --- src/containers/Meal/index.tsx | 31 +++++++++---------------------- src/store/meal/async.ts | 25 +++++++++++++++++++++++++ src/store/meal/reducer.ts | 7 ++++++- 3 files changed, 40 insertions(+), 23 deletions(-) create mode 100644 src/store/meal/async.ts diff --git a/src/containers/Meal/index.tsx b/src/containers/Meal/index.tsx index 853bc12..c7b1a7c 100644 --- a/src/containers/Meal/index.tsx +++ b/src/containers/Meal/index.tsx @@ -1,8 +1,8 @@ -import React, { FC, useEffect, useState } from "react"; +import { FC, useEffect, useState } from "react"; import { useParams } from "react-router-dom"; -import { getData } from "../../services/api"; import { useFirebase } from "../../services/Firebase"; -import { MealApi as MealType } from "../../types/meal"; +import { useMeal } from "../../store/meal"; +import { fetchMeal, fetchRandomMeal } from "../../store/meal/async"; import { useAuth0 } from "../../utils/auth0-spa"; import { NotFound } from "../NotFound"; import { MealPage } from "./components/MealPage"; @@ -12,31 +12,18 @@ export const Meal: FC = () => { // hooks const { user, isAuthenticated } = useAuth0(); const { id } = useParams<{ id: string }>(); + const { state, dispatch } = useMeal(); const fb = useFirebase(); // local state - const [meal, setMeal] = useState({ meals: [] as MealType[] }); const [isFav, setIsFav] = useState(); // variables - const mealItem = meal?.meals?.[0]; - - const getMeal = ( - id: string, - setMeal: React.Dispatch> - ) => { - getData(id, setMeal, "lookup"); - }; - - const getRandomMeal = ( - setMeal: React.Dispatch> - ) => { - getData("random", setMeal); - }; - + const mealItem = state.meals?.[0]; // effects /** Fetch meal from db */ useEffect(() => { - !id ? getRandomMeal(setMeal) : getMeal(id, setMeal); - }, [id]); + !id ? fetchRandomMeal(dispatch) : fetchMeal(dispatch, id); + }, [id, dispatch]); + /** Updates fav status in db */ useEffect(() => { if (isAuthenticated) { @@ -63,7 +50,7 @@ export const Meal: FC = () => { const item = buildMealProps(mealItem, isFav!); const ingredients = buildIngredientList(mealItem); - return !!meal?.meals ? ( + return !!state.meals ? ( { + // const meal = await getData("random") + //TODO: refactor to use Meal client + const URI = createURI("random"); + + const meal = await fetch(URI) + .then((response) => response.json()) + .catch((error) => console.warn(error + "url:" + URI)); + + dispatch({ type: "setMeal", payload: meal.meals }); +}; + +export const fetchMeal = async (dispatch: Dispatch, id: string) => { + //TODO: refactor to use Meal client + const URI = createURI(id, "lookup"); + + const meal = await fetch(URI) + .then((response) => response.json()) + .catch((error) => console.warn(error + "url:" + URI)); + + dispatch({ type: "setMeal", payload: meal.meals }); +}; diff --git a/src/store/meal/reducer.ts b/src/store/meal/reducer.ts index 324bf21..9f3174e 100644 --- a/src/store/meal/reducer.ts +++ b/src/store/meal/reducer.ts @@ -2,6 +2,8 @@ import { AppState } from "./index"; export const appReducer = (state: AppState, action: Action) => { switch (action.type) { + case "setMeal": + return { meals: action.payload }; case "fetchMeal": return { meals: state.meals }; case "fetchRandomMeal": @@ -14,5 +16,8 @@ export const appReducer = (state: AppState, action: Action) => { } }; -export type Action = { type: "fetchMeal" | "fetchRandomMeal" | "toggleFav" }; +export type Action = { + payload?: any; + type: "setMeal" | "fetchMeal" | "fetchRandomMeal" | "toggleFav"; +}; export type Dispatch = (action: Action) => void;