-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIngredients.js
More file actions
130 lines (116 loc) · 3.75 KB
/
Ingredients.js
File metadata and controls
130 lines (116 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import React, { useReducer, useEffect, useCallback, useMemo } from "react";
import IngredientForm from "./IngredientForm";
import IngredientList from "./IngredientList";
import ErrorModal from "../UI/ErrorModal";
import Search from "./Search";
const ingredientReducer = (currentIngredients, action) => {
switch (action.type) {
case "SET":
return action.ingredients;
case "ADD":
return [...currentIngredients, action.ingredient];
case "DELETE":
return currentIngredients.filter((ing) => ing.id !== action.id);
default:
throw new Error("Should not get there!");
}
};
const httpReducer = (curHttpState, action) => {
switch (action.type) {
case "SEND":
return { loading: true, error: null };
case "RESPONSE":
return { ...curHttpState, loading: false };
case "ERROR":
return { loading: false, error: action.errorMessage };
case "CLEAR":
return { ...curHttpState, error: null };
default:
throw new Error("Should not be reached!");
}
};
const Ingredients = () => {
const [userIngredients, dispatch] = useReducer(ingredientReducer, []);
const [httpState, dispatchHttp] = useReducer(httpReducer, {
loading: false,
error: null,
});
// const [userIngredients, setUserIngredients] = useState([]);
// const [isLoading, setIsLoading] = useState(false);
// const [error, setError] = useState();
useEffect(() => {
console.log("RENDERING INGREDIENTS", userIngredients);
}, [userIngredients]);
const filteredIngredientsHandler = useCallback((filteredIngredients) => {
// setUserIngredients(filteredIngredients);
dispatch({ type: "SET", ingredients: filteredIngredients });
}, []);
const addIngredientHandler = useCallback((ingredient) => {
dispatchHttp({ type: "SEND" });
fetch("https://react-hooks-update.firebaseio.com/ingredients.json", {
method: "POST",
body: JSON.stringify(ingredient),
headers: { "Content-Type": "application/json" },
})
.then((response) => {
dispatchHttp({ type: "RESPONSE" });
return response.json();
})
.then((responseData) => {
// setUserIngredients(prevIngredients => [
// ...prevIngredients,
// { id: responseData.name, ...ingredient }
// ]);
dispatch({
type: "ADD",
ingredient: { id: responseData.name, ...ingredient },
});
});
}, []);
const removeIngredientHandler = useCallback((ingredientId) => {
dispatchHttp({ type: "SEND" });
fetch(
`https://react-hooks-update.firebaseio.com/ingredients/${ingredientId}.json`,
{
method: "DELETE",
}
)
.then((response) => {
dispatchHttp({ type: "RESPONSE" });
// setUserIngredients(prevIngredients =>
// prevIngredients.filter(ingredient => ingredient.id !== ingredientId)
// );
dispatch({ type: "DELETE", id: ingredientId });
})
.catch((error) => {
dispatchHttp({ type: "ERROR", errorMessage: "Something went wrong!" });
});
}, []);
const clearError = useCallback(() => {
dispatchHttp({ type: "CLEAR" });
}, []);
const ingredientList = useMemo(() => {
return (
<IngredientList
ingredients={userIngredients}
onRemoveItem={removeIngredientHandler}
/>
);
}, [userIngredients, removeIngredientHandler]);
return (
<div className="App">
{httpState.error && (
<ErrorModal onClose={clearError}>{httpState.error}</ErrorModal>
)}
<IngredientForm
onAddIngredient={addIngredientHandler}
loading={httpState.loading}
/>
<section>
<Search onLoadIngredients={filteredIngredientsHandler} />
{ingredientList}
</section>
</div>
);
};
export default Ingredients;