mirror of
https://github.com/rjNemo/meal_planner
synced 2026-06-12 13:26:45 +00:00
refactor
This commit is contained in:
parent
d5668eb537
commit
9f7743c8f1
3 changed files with 7 additions and 13 deletions
|
|
@ -1,2 +0,0 @@
|
||||||
export type Action = { type: "fetchMeal" | "fetchRandomMeal" | "toggleFav" };
|
|
||||||
export type Dispatch = (action: Action) => void;
|
|
||||||
|
|
@ -2,18 +2,12 @@
|
||||||
|
|
||||||
import { createContext, FC, useContext, useReducer } from "react";
|
import { createContext, FC, useContext, useReducer } from "react";
|
||||||
import { MealApi } from "../../types/meal";
|
import { MealApi } from "../../types/meal";
|
||||||
import { Dispatch } from "./actions";
|
import { appReducer, Dispatch } from "./reducer";
|
||||||
import { appReducer } from "./reducer";
|
|
||||||
|
|
||||||
const AppContext = createContext<
|
|
||||||
| {
|
|
||||||
state: AppState;
|
|
||||||
dispatch: Dispatch;
|
|
||||||
}
|
|
||||||
| undefined
|
|
||||||
>(undefined);
|
|
||||||
|
|
||||||
export type AppState = { meals: MealApi[] };
|
export type AppState = { meals: MealApi[] };
|
||||||
|
type ContextType = { state: AppState; dispatch: Dispatch } | undefined;
|
||||||
|
|
||||||
|
const AppContext = createContext<ContextType>(undefined);
|
||||||
|
|
||||||
export const useMeal = () => {
|
export const useMeal = () => {
|
||||||
const context = useContext(AppContext);
|
const context = useContext(AppContext);
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,3 @@
|
||||||
import { Action } from "./actions";
|
|
||||||
import { AppState } from "./index";
|
import { AppState } from "./index";
|
||||||
|
|
||||||
export const appReducer = (state: AppState, action: Action) => {
|
export const appReducer = (state: AppState, action: Action) => {
|
||||||
|
|
@ -14,3 +13,6 @@ export const appReducer = (state: AppState, action: Action) => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type Action = { type: "fetchMeal" | "fetchRandomMeal" | "toggleFav" };
|
||||||
|
export type Dispatch = (action: Action) => void;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue