Skip to content

Commit ec40d0b

Browse files
committed
Added logic to change text with API response
1 parent e62bcd4 commit ec40d0b

File tree

4 files changed

+67
-8
lines changed

4 files changed

+67
-8
lines changed

RecipeFinder/.env

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
PORT = 1035
2-
HOST = 0.0.0.0
2+
HOST = 0.0.0.0
3+
REACT_APP_MEALDB_API = https://www.themealdb.com/

RecipeFinder/src/components/Header.jsx

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,48 @@
1-
import { Fragment } from "react";
1+
import { Fragment, useEffect, useRef, useState } from "react";
22
import { useSelector, useDispatch } from "react-redux";
33

44
import { setLoading } from "../store/";
5+
import { setSearchResult } from "../store/searchSlice";
56
import styles from "../styles/Header.module.css";
7+
import { fetchMealByName } from "../thunk/fetchMealByName";
8+
9+
const DEFAULT_STATUS_TEXT = `Type a Dish Name to Search for it's ingredient`;
610

711
const Header = () => {
8-
const loading = useSelector(({ app }) => app);
12+
const { loading } = useSelector(({ app }) => app);
13+
const [statusText, setStatusText] = useState(null);
14+
15+
useEffect(() => {
16+
setStatusText(DEFAULT_STATUS_TEXT);
17+
}, []);
18+
19+
useEffect(() => {
20+
setStatusText((prevState) => (loading ? "Loading" : prevState));
21+
}, [loading]);
22+
23+
const searchInput = useRef();
924
const actionDispatcher = useDispatch();
1025

1126
const setLoadingTo = (status) => {
1227
if (typeof status === "boolean") actionDispatcher(setLoading(status));
1328
};
1429

30+
const searchMealAPI = async () => {
31+
try {
32+
if (searchInput.current.value.trim().length !== 0) {
33+
setLoadingTo(true);
34+
const searchResult = await fetchMealByName(searchInput.current.value);
35+
if (searchResult !== null) {
36+
actionDispatcher(setSearchResult(searchResult));
37+
setStatusText("");
38+
} else setStatusText("No data has been recieved");
39+
}
40+
} catch (error) {
41+
console.error(error);
42+
} finally {
43+
setLoadingTo(false);
44+
}
45+
};
1546
return (
1647
<Fragment>
1748
<h1 className={styles.app_heading}>Recipe Finder</h1>
@@ -20,11 +51,17 @@ const Header = () => {
2051
type="text"
2152
placeholder="Enter the Name of the Dish"
2253
className={styles.input_recipe}
54+
ref={searchInput}
2355
/>
24-
<button onClick={() => setLoadingTo(true)}>Get Ingredients</button>
25-
<p className={styles.search_comment}>
26-
Type a Dish Name to Search for it's ingredient
27-
</p>
56+
<button
57+
onClick={() => {
58+
setLoadingTo(true);
59+
searchMealAPI();
60+
}}
61+
>
62+
Get Ingredients
63+
</button>
64+
<p className={styles.search_comment}>{statusText}</p>
2865
</span>
2966
</Fragment>
3067
);

RecipeFinder/src/store/searchSlice.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@ const initialState = { searchResult: null };
55
const searchSlice = createSlice({
66
name: "search",
77
initialState,
8-
reducers: { setSearchResult: async (prevState, action) => {} },
8+
reducers: {
9+
setSearchResult: (prevState, { payload }) => {
10+
return {
11+
...prevState,
12+
searchResult: payload,
13+
};
14+
},
15+
},
916
});
1017

1118
export const { setSearchResult } = searchSlice.actions;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Makes a GET request to https://www.themealdb.com/api/json/v1/1/search.php?s={searchQuery} to fetch the recipe.
3+
* @param {string} searchQuery
4+
*/
5+
6+
const fetchMealByName = async (searchQuery) => {
7+
const API_STRING = `${process.env.REACT_APP_MEALDB_API}api/json/v1/1/search.php?s=${searchQuery}`;
8+
const raw_request = await fetch(API_STRING, { method: "GET" });
9+
10+
const { meals } = await raw_request.json();
11+
return meals;
12+
};
13+
14+
export { fetchMealByName };

0 commit comments

Comments
 (0)