Skip to content

Commit b8aa162

Browse files
committed
Remove redux in favor of react-query
1 parent 5ab872e commit b8aa162

File tree

24 files changed

+495
-641
lines changed

24 files changed

+495
-641
lines changed

package-lock.json

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

package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,12 @@
3030
"@mui/icons-material": "^5.8.4",
3131
"@mui/material": "^5.8.7",
3232
"@mui/styles": "^5.8.7",
33-
"@reduxjs/toolkit": "^1.2.5",
34-
"@types/react-redux": "^7.1.7",
3533
"@types/yup": "^0.29.14",
3634
"axios": "^0.27.2",
3735
"formik": "^2.2.9",
3836
"react": "^17.0.0",
3937
"react-dom": "^17.0.0",
4038
"react-query": "^3.39.1",
41-
"react-redux": "^7.2.0",
4239
"react-router-dom": "^6.3.0",
4340
"serverless": "^2.29.0",
4441
"serverless-finch": "^2.6.0",

src/components/AddProductToCart/AddProductToCart.tsx

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,47 @@ import CartIcon from "@mui/icons-material/ShoppingCart";
44
import Add from "@mui/icons-material/Add";
55
import Remove from "@mui/icons-material/Remove";
66
import IconButton from "@mui/material/IconButton";
7-
import { useDispatch, useSelector } from "react-redux";
8-
import { addToCart, selectCartItems, removeFromCart } from "~/store/cartSlice";
7+
import { useCart, useInvalidateCart, useUpsertCart } from "~/queries/cart";
98

109
type AddProductToCartProps = {
1110
product: Product;
1211
};
1312

1413
export default function AddProductToCart({ product }: AddProductToCartProps) {
15-
const dispatch = useDispatch();
16-
const cartItems = useSelector(selectCartItems);
17-
const cartItem = cartItems.find((i) => i.product.id === product.id);
14+
const { data = [], isFetching } = useCart();
15+
const { mutate: upsertCart } = useUpsertCart();
16+
const invalidateCart = useInvalidateCart();
17+
const cartItem = data.find((i) => i.product.id === product.id);
1818

19-
return (
19+
const addProduct = () => {
20+
upsertCart(
21+
{ product, count: cartItem ? cartItem.count + 1 : 1 },
22+
{ onSuccess: invalidateCart }
23+
);
24+
};
25+
26+
const removeProduct = () => {
27+
if (cartItem) {
28+
upsertCart(
29+
{ ...cartItem, count: cartItem.count - 1 },
30+
{ onSuccess: invalidateCart }
31+
);
32+
}
33+
};
34+
35+
return cartItem ? (
2036
<>
21-
{cartItem ? (
22-
<>
23-
<IconButton
24-
onClick={() => dispatch(removeFromCart(product))}
25-
size="large"
26-
>
27-
<Remove color={"secondary"} />
28-
</IconButton>
29-
<Typography align="center">{cartItem.count}</Typography>
30-
<IconButton onClick={() => dispatch(addToCart(product))} size="large">
31-
<Add color={"secondary"} />
32-
</IconButton>
33-
</>
34-
) : (
35-
<IconButton onClick={() => dispatch(addToCart(product))} size="large">
36-
<CartIcon color={"secondary"} />
37-
</IconButton>
38-
)}
37+
<IconButton disabled={isFetching} onClick={removeProduct} size="large">
38+
<Remove color={"secondary"} />
39+
</IconButton>
40+
<Typography align="center">{cartItem.count}</Typography>
41+
<IconButton disabled={isFetching} onClick={addProduct} size="large">
42+
<Add color={"secondary"} />
43+
</IconButton>
3944
</>
45+
) : (
46+
<IconButton disabled={isFetching} onClick={addProduct} size="large">
47+
<CartIcon color={"secondary"} />
48+
</IconButton>
4049
);
4150
}

src/components/Form/TextField.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export function fieldToTextField({
2828
fieldOnBlur(e ?? field.name);
2929
},
3030
...field,
31+
value: field.value ?? "",
3132
...props,
3233
};
3334
}

src/components/MainLayout/components/Cart.tsx

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,15 @@
11
import Badge from "@mui/material/Badge";
22
import CartIcon from "@mui/icons-material/ShoppingCart";
33
import IconButton from "@mui/material/IconButton";
4-
import axios, { AxiosError } from "axios";
5-
import { useDispatch, useSelector } from "react-redux";
6-
import { selectCartItems, updateFromApi } from "~/store/cartSlice";
74
import { Link } from "react-router-dom";
8-
import API_PATHS from "~/constants/apiPaths";
9-
import { useQuery } from "react-query";
10-
import { CartItem } from "~/models/CartItem";
5+
import { useCart } from "~/queries/cart";
116

127
export default function Cart() {
13-
const dispatch = useDispatch();
14-
useQuery<CartItem[], AxiosError>(
15-
"cart",
16-
async () => {
17-
const res = await axios.get<CartItem[]>(
18-
`${API_PATHS.cart}/profile/cart`,
19-
{
20-
headers: {
21-
Authorization: `Basic ${localStorage.getItem(
22-
"authorization_token"
23-
)}`,
24-
},
25-
}
26-
);
27-
return res.data;
28-
},
29-
{
30-
onSuccess: (data) => {
31-
dispatch(updateFromApi({ items: data }));
32-
},
33-
}
34-
);
35-
36-
const cartItems = useSelector(selectCartItems);
37-
const badgeContent = cartItems.length || undefined;
8+
const { data = [] } = useCart();
9+
const badgeContent = data.length || undefined;
3810

3911
return (
40-
<IconButton
41-
aria-label="show 4 new mails"
42-
color="inherit"
43-
component={Link}
44-
to="/cart"
45-
size="large"
46-
>
12+
<IconButton color="inherit" component={Link} to="/cart" size="large">
4713
<Badge badgeContent={badgeContent} color="secondary">
4814
<CartIcon />
4915
</Badge>

0 commit comments

Comments
 (0)