Skip to content

Commit d9c6697

Browse files
Merge pull request #188 from rolling-scopes-school/cart-api
Add cart api integration
2 parents 1f16f11 + f739cc8 commit d9c6697

File tree

3 files changed

+64
-9
lines changed

3 files changed

+64
-9
lines changed

src/components/MainLayout/components/Cart.tsx

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,35 @@
11
import Badge from "@material-ui/core/Badge";
22
import CartIcon from "@material-ui/icons/ShoppingCart";
33
import IconButton from "@material-ui/core/IconButton";
4-
import React from "react";
5-
import { useSelector } from "react-redux";
6-
import { selectCartItems } from "store/cartSlice";
4+
import React, { useEffect } from "react";
5+
import axios from 'axios';
6+
import { useDispatch, useSelector } from "react-redux";
7+
import { selectCartItems, updateFromApi } from "store/cartSlice";
78
import { Link } from 'react-router-dom';
9+
import API_PATHS from "../../../constants/apiPaths";
810

911
export default function Cart() {
12+
const dispatch = useDispatch();
13+
useEffect(() => {
14+
axios.get(
15+
`${API_PATHS.cart}/profile/cart`,
16+
{
17+
headers: {
18+
Authorization: `Basic ${localStorage.getItem('authorization_token')}`
19+
}
20+
}
21+
).then(({ data: { data: { cart } } }) => {
22+
dispatch(updateFromApi(cart))
23+
});
24+
}, []);
1025
const cartItems = useSelector(selectCartItems);
1126
const badgeContent = cartItems.length || undefined;
1227

1328
return (
14-
<IconButton
15-
aria-label="show 4 new mails"
16-
color="inherit"
17-
component={Link}
29+
<IconButton
30+
aria-label="show 4 new mails"
31+
color="inherit"
32+
component={Link}
1833
to="/cart"
1934
>
2035
<Badge badgeContent={badgeContent} color="secondary">

src/constants/apiPaths.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ const API_PATHS = {
33
product: 'https://.execute-api.eu-west-1.amazonaws.com/dev',
44
order: 'https://.execute-api.eu-west-1.amazonaws.com/dev',
55
import: 'https://.execute-api.eu-west-1.amazonaws.com/dev',
6-
bff: 'https://.execute-api.eu-west-1.amazonaws.com/dev'
6+
bff: 'https://.execute-api.eu-west-1.amazonaws.com/dev',
7+
cart: 'https://.execute-api.eu-west-1.amazonaws.com/dev',
78
};
89

910
export default API_PATHS;

src/store/cartSlice.ts

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import {createSlice, PayloadAction} from '@reduxjs/toolkit';
22
import {RootState} from 'store/store';
33
import {Product} from "models/Product";
44
import {CartItem} from "models/CartItem";
5+
import API_PATHS from "../constants/apiPaths";
6+
import axios from 'axios';
57

68
interface CartState {
79
items: CartItem[]
@@ -15,6 +17,13 @@ export const cartSlice = createSlice({
1517
name: 'cart',
1618
initialState,
1719
reducers: {
20+
updateFromApi: (state, { payload: { items } }: PayloadAction<CartState>) => {
21+
return {
22+
items: [
23+
...items,
24+
],
25+
}
26+
},
1827
// Use the PayloadAction type to declare the contents of `action.payload`
1928
addToCart: (state, action: PayloadAction<Product>) => {
2029
const {items} = state;
@@ -44,7 +53,37 @@ export const cartSlice = createSlice({
4453
},
4554
});
4655

47-
export const {addToCart, removeFromCart, clearCart} = cartSlice.actions;
56+
export const addToCart = (product: Product) => async (dispatch: any, getState: any) => {
57+
dispatch(cartSlice.actions.addToCart(product));
58+
const { cart: { items } } = getState();
59+
await axios.put(`${API_PATHS.cart}/profile/cart`, { items }, {
60+
headers: {
61+
Authorization: `Basic ${localStorage.getItem('authorization_token')}`,
62+
},
63+
})
64+
};
65+
66+
export const removeFromCart = (product: Product) => async (dispatch: any, getState: any) => {
67+
dispatch(cartSlice.actions.removeFromCart(product));
68+
const { cart: { items } } = getState();
69+
await axios.put(`${API_PATHS.cart}/profile/cart`, { items }, {
70+
headers: {
71+
Authorization: `Basic ${localStorage.getItem('authorization_token')}`,
72+
},
73+
})
74+
};
75+
76+
export const clearCart = () => async (dispatch: any, getState: any) => {
77+
dispatch(cartSlice.actions.clearCart());
78+
const { cart: { items } } = getState();
79+
await axios.put(`${API_PATHS.cart}/profile/cart`, { items }, {
80+
headers: {
81+
Authorization: `Basic ${localStorage.getItem('authorization_token')}`,
82+
},
83+
})
84+
};
85+
86+
export const {updateFromApi} = cartSlice.actions;
4887

4988

5089
// The function below is called a selector and allows us to select a value from

0 commit comments

Comments
 (0)