You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
1.2 KiB
46 lines
1.2 KiB
import { createContext, useReducer, useEffect } from "react";
|
|
|
|
const BookmarkContext = createContext();
|
|
|
|
const recipeIsSaved = (key, recipes) => {
|
|
return recipes.some((recipe) => recipe.key === key);
|
|
};
|
|
|
|
const reducer = (recipes, action) => {
|
|
switch (action.type) {
|
|
case "TOGGLE_SAVE":
|
|
let newRecipes = [...recipes];
|
|
if (!recipeIsSaved(action.key, recipes)) {
|
|
newRecipes.push(action.payload);
|
|
} else {
|
|
newRecipes = recipes.filter((recipe) => recipe.key !== action.key);
|
|
}
|
|
return newRecipes;
|
|
default:
|
|
return recipes;
|
|
}
|
|
};
|
|
|
|
const BookmarkContextProvider = (props) => {
|
|
const [recipes, dispatch] = useReducer(reducer, [], () => {
|
|
const localData = localStorage.getItem("recipes");
|
|
|
|
return localData ? JSON.parse(localData) : [];
|
|
});
|
|
const isSaved = (key) => {
|
|
return recipeIsSaved(key, recipes);
|
|
};
|
|
|
|
// simpan data recipe ke localStorage
|
|
useEffect(() => {
|
|
localStorage.setItem("recipes", JSON.stringify(recipes));
|
|
}, [recipes]);
|
|
|
|
return (
|
|
<BookmarkContext.Provider value={{ recipes, dispatch, isSaved }}>
|
|
{props.children}
|
|
</BookmarkContext.Provider>
|
|
);
|
|
};
|
|
|
|
export { BookmarkContext, BookmarkContextProvider };
|