Skip to content

Commit 5ab872e

Browse files
committed
Add react-query
1 parent 3ee1d97 commit 5ab872e

File tree

17 files changed

+454
-251
lines changed

17 files changed

+454
-251
lines changed

package-lock.json

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

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,11 @@
3333
"@reduxjs/toolkit": "^1.2.5",
3434
"@types/react-redux": "^7.1.7",
3535
"@types/yup": "^0.29.14",
36-
"axios": "^0.19.2",
36+
"axios": "^0.27.2",
3737
"formik": "^2.2.9",
3838
"react": "^17.0.0",
3939
"react-dom": "^17.0.0",
40+
"react-query": "^3.39.1",
4041
"react-redux": "^7.2.0",
4142
"react-router-dom": "^6.3.0",
4243
"serverless": "^2.29.0",
@@ -59,7 +60,7 @@
5960
"jsdom": "^20.0.0",
6061
"msw": "^0.43.0",
6162
"prettier": "2.7.1",
62-
"typescript": "^4.6.3",
63+
"typescript": "^4.7.4",
6364
"vite": "^2.9.9",
6465
"vitest": "^0.17.0"
6566
},

src/components/App/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function App() {
1515
<Route path="cart" element={<PageCart />} />
1616
<Route path="admin">
1717
<Route path="orders" element={<PageOrders />} />
18-
<Route path="order/:id" element={<PageOrder />} />
18+
<Route path="orders/:id" element={<PageOrder />} />
1919
<Route path="products" element={<PageProductImport />} />
2020
<Route path="product-form" element={<PageProductForm />}>
2121
<Route path=":id" element={<PageProductForm />} />

src/components/MainLayout/components/Cart.tsx

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,38 @@
11
import Badge from "@mui/material/Badge";
22
import CartIcon from "@mui/icons-material/ShoppingCart";
33
import IconButton from "@mui/material/IconButton";
4-
import React from "react";
5-
import axios from "axios";
4+
import axios, { AxiosError } from "axios";
65
import { useDispatch, useSelector } from "react-redux";
76
import { selectCartItems, updateFromApi } from "~/store/cartSlice";
87
import { Link } from "react-router-dom";
98
import API_PATHS from "~/constants/apiPaths";
9+
import { useQuery } from "react-query";
10+
import { CartItem } from "~/models/CartItem";
1011

1112
export default function Cart() {
1213
const dispatch = useDispatch();
13-
React.useEffect(() => {
14-
axios
15-
.get(`${API_PATHS.cart}/profile/cart`, {
16-
headers: {
17-
Authorization: `Basic ${localStorage.getItem("authorization_token")}`,
18-
},
19-
})
20-
.then((response) => {
21-
dispatch(updateFromApi({ items: response.data }));
22-
});
23-
}, [dispatch]);
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+
2436
const cartItems = useSelector(selectCartItems);
2537
const badgeContent = cartItems.length || undefined;
2638

src/components/pages/PageCart/PageCart.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import TextField from "~/components/Form/TextField";
1919

2020
const steps = ["Review your cart", "Shipping address", "Review your order"];
2121

22-
const initialAddressValues: any = AddressSchema.cast({});
22+
const initialAddressValues = AddressSchema.cast({});
2323

2424
const CartIsEmpty = () => (
2525
<Typography variant="h6" gutterBottom>

0 commit comments

Comments
 (0)