Skip to content

Commit a1be813

Browse files
authored
Merge pull request #79 from leonardoOluz/fetchData
feat: ✅ Tratando dados de api mock de pedidos e favo…
2 parents 80fad2a + 722a249 commit a1be813

17 files changed

Lines changed: 541 additions & 312 deletions

File tree

package-lock.json

Lines changed: 90 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"react-redux": "^9.2.0",
2525
"react-router-dom": "^7.5.1",
2626
"react-toastify": "^11.0.5",
27+
"redux-saga": "^1.3.0",
2728
"styled-components": "^6.1.16",
2829
"swiper": "^11.2.6",
2930
"uuid": "^11.1.0",

src/components/BtnLogar/index.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import { thema } from "@/styles/thema";
88
import { useNavigate } from "react-router-dom";
99
import { RootState } from "@/types/store";
1010
import { useSelector, useDispatch } from "react-redux";
11-
import { logout } from "@/store/reducers/usuario";
11+
import { logoutUser } from "@/store/reducers/usuario";
12+
import { AppDispatch } from "@/store";
1213

1314
const iconsStyles = {
1415
width: "6rem",
@@ -25,13 +26,13 @@ const SpanBtn = styled.span`
2526
`;
2627
const BtnLogar = () => {
2728
const user = useSelector((state: RootState) => state.usuario);
28-
const dispatch = useDispatch();
29+
const dispatch = useDispatch<AppDispatch>();
2930
const width = useResize();
3031
const navigate = useNavigate();
3132

3233
const handleLogout = () => {
3334
if (user.isLogado) {
34-
dispatch(logout());
35+
dispatch(logoutUser());
3536
navigate("/");
3637
return;
3738
}

src/components/CardProduto/index.tsx

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import useSelectCatPromocao from "@/hooks/useSelectCatPromocao";
1212
import { FaHeart } from "react-icons/fa";
1313
import { useDispatch } from "react-redux";
1414
import { AppDispatch } from "@/store";
15-
import { setFavorito } from "@/store/reducers/favorito";
15+
import { createFavoriteFetch, deleteFavoriteFetch } from "@/store/reducers/favorito";
1616
import { useSelector } from "react-redux";
1717
import { RootState } from "@/types/store";
1818
interface IProps {
@@ -24,14 +24,23 @@ const CardProduto = ({ card }: IProps) => {
2424
const checkPromocao = useSelectCatPromocao();
2525
const dispatch = useDispatch<AppDispatch>();
2626
const favorite = useSelector((state: RootState) => state.favorito);
27+
const { isLogado } = useSelector((state: RootState) => state.usuario);
2728

28-
const handleFavorite = (id: number): boolean => {
29+
const handleFavoriteColor = (id: number): boolean => {
2930
return favorite.some((item) => item.idProduct === id);
3031
};
3132
const openModalProduto = () => {
3233
setModalOpen(!modalOpen);
3334
};
3435

36+
const handleCheckFavoite = (idProduct: number) => {
37+
if (favorite.some((item) => item.idProduct === idProduct)) {
38+
dispatch(deleteFavoriteFetch(idProduct));
39+
return;
40+
}
41+
dispatch(createFavoriteFetch({ idProduct: card.id }))
42+
}
43+
3544
return (
3645
<ArticleStyle>
3746
<Container>
@@ -63,21 +72,23 @@ const CardProduto = ({ card }: IProps) => {
6372
>
6473
Ver Mais
6574
</Botao>
66-
<Botao
67-
classNameBtn="btnUnset"
68-
aria-label="Botão de favorito"
69-
onClick={() => dispatch(setFavorito({ idProduct: card.id }))}
70-
type="button"
71-
>
72-
<FaHeart
73-
size={25}
74-
color={
75-
handleFavorite(card.id)
76-
? thema.colorsPrimary.laranja
77-
: thema.colorsPrimary.cinza
78-
}
79-
/>
80-
</Botao>
75+
{isLogado && (
76+
<Botao
77+
classNameBtn="btnUnset"
78+
aria-label="Botão de favorito"
79+
onClick={() => handleCheckFavoite(card.id)} // adiciona o id do usuario e id do produto ao estado global
80+
type="button"
81+
>
82+
<FaHeart
83+
size={25}
84+
color={
85+
handleFavoriteColor(card.id)
86+
? thema.colorsPrimary.laranja
87+
: thema.colorsPrimary.cinza
88+
}
89+
/>
90+
</Botao>
91+
)}
8192
</div>
8293
</Container>
8394
<CheckModal

src/components/Navbar/components/LoginButton.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,20 @@ import { useSelector } from "react-redux";
33
import { ItemLink } from "../styles";
44
import { Link } from "react-router-dom";
55
import { RootState } from "@/types/store";
6-
import { logout } from "@/store/reducers/usuario";
6+
import { logoutUser } from "@/store/reducers/usuario";
7+
import { AppDispatch } from "@/store";
78

89
const LoginButton = () => {
910
const user = useSelector((state: RootState) => state.usuario);
10-
const dispatch = useDispatch();
11+
const dispatch = useDispatch<AppDispatch>();
1112

1213
return (
1314
<ItemLink>
1415
<Link
1516
to={user.isLogado ? "/" : "/login"}
1617
title={`${user.isLogado ? "Sair" : "Entrar"} na Meteora`}
1718
onClick={() => {
18-
if (user.isLogado) dispatch(logout());
19+
if (user.isLogado) dispatch(logoutUser());
1920
}}
2021
>
2122
{user.isLogado ? "Sair" : "Entrar"}

src/pages/favorite/index.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,23 @@ import ItemList from "@/components/List/ItemList";
66
import NewsLetter from "@/components/NewsLetter";
77
import Section from "@/components/Section";
88
import Typography from "@/components/Typography";
9+
import { AppDispatch } from "@/store";
10+
import { fetchGetFavoritosSaga } from "@/store/reducers/favorito";
911
import { selectProductForFavorite } from "@/store/selectors/itemSelectors";
1012
import { thema } from "@/styles/thema";
1113
import { RootState } from "@/types/store";
14+
import { useEffect } from "react";
15+
import { useDispatch } from "react-redux";
1216
import { useSelector } from "react-redux";
1317

1418
const Favorite = () => {
1519
const produtos = useSelector((state: RootState) => {
1620
return selectProductForFavorite(state);
1721
});
22+
const dispatch = useDispatch<AppDispatch>();
23+
useEffect(() => {
24+
dispatch(fetchGetFavoritosSaga());
25+
}, [dispatch]);
1826

1927
return (
2028
<>

src/pages/login/FormLogin/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import { MessageError } from "@/components/MessageError";
77
import { useEffect } from "react";
88
import { ILogin } from "@/types/usuarios";
99
import { useDispatch } from "react-redux";
10-
import { loginUser } from "@/store/reducers/usuario";
1110
import { useSelector } from "react-redux";
1211
import { RootState } from "@/types/store";
1312
import { useLocation, useNavigate } from "react-router-dom";
1413
import { AppDispatch } from "@/store";
14+
import { loginUser } from "@/store/reducers/usuario";
1515

1616
const FormLogin = () => {
1717
const dispatch = useDispatch<AppDispatch>();

src/pages/pedidos/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ import Facilidades from "@/components/Facilidades";
1010
import { useEffect } from "react";
1111
import { useDispatch } from "react-redux";
1212
import { AppDispatch } from "@/store";
13-
import { fetchGetPedidos } from "@/store/reducers/pedidos";
13+
import { fetchGetPedidosSaga } from "@/store/reducers/pedidos";
1414
const Pedidos = () => {
1515
const pedidos = useSelector((state: RootState) => state.pedidos);
1616
const dispatch = useDispatch<AppDispatch>();
1717

1818
useEffect(() => {
19-
dispatch(fetchGetPedidos());
19+
dispatch(fetchGetPedidosSaga());
2020
}, [dispatch]);
2121

2222
return (

0 commit comments

Comments
 (0)