Skip to content

Commit

Permalink
fix(nav): Check user role before dispatching cart
Browse files Browse the repository at this point in the history
- Dispatch the cart only if the user role is "buyer."
- Do not dispatch the cart for users with "seller" or "admin" roles.
  • Loading branch information
Hakizimana-Clement committed Aug 1, 2024
1 parent e1f1a9f commit b3a4efe
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
4 changes: 3 additions & 1 deletion src/components/Nav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ const Nav = () => {
if (!data) {
dispatch(fetchUserProfile()).unwrap();
}
dispatch(getCarts()).unwrap();
if (userData?.role === 'BUYER') {
dispatch(getCarts()).unwrap();
}
}
}, [accessToken, data, dispatch]);

Expand Down
12 changes: 7 additions & 5 deletions src/pages/ProductsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,14 @@ const ProductsPage = () => {

useEffect(() => {
const fetchCarts = async () => {
try {
if (carts && !carts.length) {
await dispatch(getCarts()).unwrap();
if (tokenInfo?.role === 'BUYER') {
try {
if (carts && !carts.length) {
await dispatch(getCarts()).unwrap();
}
} catch (error) {
showErrorMessage('Error fetching carts!');
}
} catch (error) {
showErrorMessage('Error fetching carts!');
}
};
fetchCarts();
Expand Down

0 comments on commit b3a4efe

Please sign in to comment.