refactor meal client

This commit is contained in:
Ruidy 2021-04-05 11:55:08 +02:00
parent c19bb6ac32
commit 2852aa94f5
6 changed files with 24 additions and 45 deletions

View file

@ -1,2 +1,3 @@
export const apiRoot = "https://www.themealdb.com/api/json/v1/1/";
export const buttonURL = "/random"; export const buttonURL = "/random";
export const links = ["categories", "contact"]; export const links = ["categories", "contact"];

View file

@ -7,7 +7,7 @@ export const Categories = () => {
const [categories, setCategories] = useState({ categories: [] }); const [categories, setCategories] = useState({ categories: [] });
const getCategories = () => { const getCategories = () => {
getData("categories", setCategories); getData("categories").then((data) => setCategories(data));
}; };
useEffect(() => { useEffect(() => {

View file

@ -8,8 +8,8 @@ export const Category: FC = () => {
const [meals, setMeals] = useState({ meals: [] }); const [meals, setMeals] = useState({ meals: [] });
useEffect(() => { useEffect(() => {
const getMeals = () => getData(strCategory, setMeals, "filter"); const getMeals = () => getData(strCategory, "filter");
getMeals(); getMeals().then((data) => setMeals(data));
}, [strCategory]); }, [strCategory]);
return !meals.meals ? ( return !meals.meals ? (

View file

@ -1,20 +1,21 @@
import React from "react"; import { apiRoot } from "../constants";
export const createURI = (keyword: string, option?: string) => { type Option = "filter" | "lookup" | "search";
const ROOT = "https://www.themealdb.com/api/json/v1/1/";
const createURI = (keyword: string, option?: Option) => {
if (!option) { if (!option) {
return `${ROOT}${keyword}.php`; return `${apiRoot}${keyword}.php`;
} }
switch (option) { switch (option) {
case "filter": { case "filter": {
return `${ROOT}${option}.php?c=${keyword}`; return `${apiRoot}${option}.php?c=${keyword}`;
} }
case "lookup": { case "lookup": {
return `${ROOT}${option}.php?i=${keyword}`; return `${apiRoot}${option}.php?i=${keyword}`;
} }
case "search": { case "search": {
return `${ROOT}${option}.php?s=${keyword}`; return `${apiRoot}${option}.php?s=${keyword}`;
} }
default: { default: {
throw Error("Unexpected URI"); throw Error("Unexpected URI");
@ -22,15 +23,10 @@ export const createURI = (keyword: string, option?: string) => {
} }
}; };
export const getData = ( export const getData = (keyword: string, option?: Option) => {
keyword: string,
set: React.Dispatch<React.SetStateAction<any>>,
option?: string
) => {
const URI = createURI(keyword, option); const URI = createURI(keyword, option);
fetch(URI) return fetch(URI)
.then((response) => response.json()) .then((response) => response.json())
.catch((error) => console.warn(error + "url:" + URI)) .catch((error) => console.warn(error + "url:" + URI));
.then((data) => set(data));
}; };

View file

@ -1,42 +1,23 @@
import { createURI } from "../../services/api"; import { getData } from "../../services/api";
import { Dispatch } from "./reducer"; import { Dispatch } from "./reducer";
export const fetchRandomMeal = async (dispatch: Dispatch) => { export const fetchRandomMeal = async (dispatch: Dispatch) => {
// const meal = await getData("random") const meal = await getData("random");
//TODO: refactor to use Meal client dispatch({ type: "setMeal", payload: meal?.meals });
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) => { export const fetchMeal = async (dispatch: Dispatch, id: string) => {
//TODO: refactor to use Meal client const meal = await getData(id, "lookup");
const URI = createURI(id, "lookup"); dispatch({ type: "setMeal", payload: meal?.meals });
const meal = await fetch(URI)
.then((response) => response.json())
.catch((error) => console.warn(error + "url:" + URI));
dispatch({ type: "setMeal", payload: meal.meals });
}; };
export const fetchSearchResults = async ( export const fetchSearchResults = async (
dispatch: Dispatch, dispatch: Dispatch,
searchString: string searchString: string
) => { ) => {
//TODO: refactor to use Meal client const meals = await getData(searchString, "search");
const URI = createURI(searchString, "search");
const meals = await fetch(URI)
.then((response) => response.json())
.catch((error) => console.warn(error + "url:" + URI));
dispatch({ dispatch({
type: "setSearchResults", type: "setSearchResults",
payload: { search: meals.meals, searchString }, payload: { search: meals?.meals, searchString },
}); });
}; };

View file

@ -1,3 +1,4 @@
import { MealSummary } from "../../types/meal";
import { AppState } from "./index"; import { AppState } from "./index";
export const appReducer = (state: AppState, action: Action) => { export const appReducer = (state: AppState, action: Action) => {
@ -11,7 +12,7 @@ export const appReducer = (state: AppState, action: Action) => {
searchString: action.payload.searchString, searchString: action.payload.searchString,
}; };
case "clearSearchResults": case "clearSearchResults":
return { ...state, search: [] }; return { ...state, search: [] as MealSummary[] };
default: { default: {
throw new Error(`Unhandled action type: ${action.type}`); throw new Error(`Unhandled action type: ${action.type}`);
} }