Skip to content

Commit da5d828

Browse files
committed
cambios guardados en local antes de sincronizar
1 parent 073e6e4 commit da5d828

4 files changed

Lines changed: 36 additions & 9 deletions

File tree

src/components/Card.jsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1+
import useGlobalReducer from "../hooks/useGlobalReducer.jsx";
2+
13
export const Card = ({ imgURL, title, children }) => {
4+
const { store, dispatch } = useGlobalReducer();
5+
6+
const addFavorite = () => {
7+
dispatch({ type: "add_favorite", payload: { name: title } });
8+
};
9+
210
return (
311
<>
412
<div className="card">
@@ -10,9 +18,13 @@ export const Card = ({ imgURL, title, children }) => {
1018
<a href="#" className="btn btn-primary me-5">
1119
Learn more
1220
</a>
13-
<a href="like" className="btn btn-primary">
21+
<button
22+
onClick={addFavorite}
23+
className="btn btn-primary"
24+
type="button"
25+
>
1426
<i className="fa-regular fa-bookmark"></i>
15-
</a>
27+
</button>
1628
</div>
1729
</div>
1830
</div>

src/components/Favorites.jsx

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ export const Favorites = () => {
44

55
const favorites = store.favoriteList || [];
66

7+
const removeFavorite = (name) => {
8+
dispatch({
9+
type: "remove_favorite",
10+
payload: { name: item },
11+
});
12+
};
13+
714
return (
815
<>
916
<div className="dropdown">
@@ -18,10 +25,17 @@ export const Favorites = () => {
1825

1926
<ul className="dropdown-menu">
2027
{favorites.map((item, index) => (
21-
<li key={item.name || index}>
22-
<a className="dropdown-item" href="#">
23-
{item.name || item}
24-
</a>
28+
<li
29+
key={item || index}
30+
className="d-flex justify-content-between align-item-center p-2"
31+
>
32+
<span className="dropdown-item-text">{item}</span>
33+
<button
34+
onClick={() => removeFavorite(item)}
35+
className="btn btn-second btn-sm p-0 m-0"
36+
>
37+
<i className="fa fa-trash"></i>
38+
</button>
2539
</li>
2640
))}
2741
</ul>

src/store.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ export default function storeReducer(store, action = {}) {
1919
todo.id === id ? { ...todo, background: color } : todo
2020
),
2121
};
22-
case "add_Favorite":
22+
case "add_favorite":
2323
const { name } = action.payload;
2424
return { ...store, favoriteList: [...store.favoriteList, name] };
2525
case "remove_favorite":
26-
const favoriteList = store.favoriteList.filter(
26+
const favoriteListFiltered = store.favoriteList.filter(
2727
(item) => item !== action.payload.name
2828
);
29-
return { ...store, favoriteList: favoriteList };
29+
return { ...store, favoriteList: favoriteListFiltered };
3030
case "update_characterList":
3131
const characterList = action.payload;
3232
return { ...store, characterList };

src/swapiFetch.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export const getCharacterList = async () => {
1515
return characterListWithDetails;
1616
};
1717

18+
1819
export const getPlanetList = async () => {
1920
const httpResponse = await fetch(`${SWAPI_URL}planets`);
2021
const data = await httpResponse.json();

0 commit comments

Comments
 (0)