mirror of
https://github.com/rjNemo/meal_planner
synced 2026-06-12 13:26:45 +00:00
get meal with context
This commit is contained in:
parent
9f7743c8f1
commit
6f1ae88cb4
3 changed files with 40 additions and 23 deletions
|
|
@ -1,8 +1,8 @@
|
||||||
import React, { FC, useEffect, useState } from "react";
|
import { FC, useEffect, useState } from "react";
|
||||||
import { useParams } from "react-router-dom";
|
import { useParams } from "react-router-dom";
|
||||||
import { getData } from "../../services/api";
|
|
||||||
import { useFirebase } from "../../services/Firebase";
|
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 { useAuth0 } from "../../utils/auth0-spa";
|
||||||
import { NotFound } from "../NotFound";
|
import { NotFound } from "../NotFound";
|
||||||
import { MealPage } from "./components/MealPage";
|
import { MealPage } from "./components/MealPage";
|
||||||
|
|
@ -12,31 +12,18 @@ export const Meal: FC = () => {
|
||||||
// hooks
|
// hooks
|
||||||
const { user, isAuthenticated } = useAuth0();
|
const { user, isAuthenticated } = useAuth0();
|
||||||
const { id } = useParams<{ id: string }>();
|
const { id } = useParams<{ id: string }>();
|
||||||
|
const { state, dispatch } = useMeal();
|
||||||
const fb = useFirebase();
|
const fb = useFirebase();
|
||||||
// local state
|
// local state
|
||||||
const [meal, setMeal] = useState({ meals: [] as MealType[] });
|
|
||||||
const [isFav, setIsFav] = useState<boolean>();
|
const [isFav, setIsFav] = useState<boolean>();
|
||||||
// variables
|
// variables
|
||||||
const mealItem = meal?.meals?.[0];
|
const mealItem = state.meals?.[0];
|
||||||
|
|
||||||
const getMeal = (
|
|
||||||
id: string,
|
|
||||||
setMeal: React.Dispatch<React.SetStateAction<{ meals: MealType[] }>>
|
|
||||||
) => {
|
|
||||||
getData(id, setMeal, "lookup");
|
|
||||||
};
|
|
||||||
|
|
||||||
const getRandomMeal = (
|
|
||||||
setMeal: React.Dispatch<React.SetStateAction<{ meals: MealType[] }>>
|
|
||||||
) => {
|
|
||||||
getData("random", setMeal);
|
|
||||||
};
|
|
||||||
|
|
||||||
// effects
|
// effects
|
||||||
/** Fetch meal from db */
|
/** Fetch meal from db */
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
!id ? getRandomMeal(setMeal) : getMeal(id, setMeal);
|
!id ? fetchRandomMeal(dispatch) : fetchMeal(dispatch, id);
|
||||||
}, [id]);
|
}, [id, dispatch]);
|
||||||
|
|
||||||
/** Updates fav status in db */
|
/** Updates fav status in db */
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (isAuthenticated) {
|
if (isAuthenticated) {
|
||||||
|
|
@ -63,7 +50,7 @@ export const Meal: FC = () => {
|
||||||
const item = buildMealProps(mealItem, isFav!);
|
const item = buildMealProps(mealItem, isFav!);
|
||||||
const ingredients = buildIngredientList(mealItem);
|
const ingredients = buildIngredientList(mealItem);
|
||||||
|
|
||||||
return !!meal?.meals ? (
|
return !!state.meals ? (
|
||||||
<MealPage
|
<MealPage
|
||||||
meal={item}
|
meal={item}
|
||||||
ingredients={ingredients}
|
ingredients={ingredients}
|
||||||
|
|
|
||||||
25
src/store/meal/async.ts
Normal file
25
src/store/meal/async.ts
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
import { createURI } from "../../services/api";
|
||||||
|
import { Dispatch } from "./reducer";
|
||||||
|
|
||||||
|
export const fetchRandomMeal = async (dispatch: Dispatch) => {
|
||||||
|
// 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 });
|
||||||
|
};
|
||||||
|
|
@ -2,6 +2,8 @@ import { AppState } from "./index";
|
||||||
|
|
||||||
export const appReducer = (state: AppState, action: Action) => {
|
export const appReducer = (state: AppState, action: Action) => {
|
||||||
switch (action.type) {
|
switch (action.type) {
|
||||||
|
case "setMeal":
|
||||||
|
return { meals: action.payload };
|
||||||
case "fetchMeal":
|
case "fetchMeal":
|
||||||
return { meals: state.meals };
|
return { meals: state.meals };
|
||||||
case "fetchRandomMeal":
|
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;
|
export type Dispatch = (action: Action) => void;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue