Skip to content

Commit f7202e2

Browse files
committed
Add to cart
1 parent 3514eec commit f7202e2

File tree

3 files changed

+51
-17
lines changed

3 files changed

+51
-17
lines changed

src/components/cart/add-to-cart.js

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { isEmpty } from 'lodash';
22

33
import axios from 'axios';
44
import { ADD_TO_CART_ENDPOINT } from '../../utils/constants/endpoints';
5+
import { getSession, storeSession } from '../../utils/cart/session';
6+
import { getAddOrViewCartConfig, getAddToCartConfig } from '../../utils/cart/api';
57

68
const AddToCart = ( { product } ) => {
79

@@ -10,18 +12,19 @@ const AddToCart = ( { product } ) => {
1012
}
1113

1214
const addToCart = ( productId, qty = 1 ) => {
13-
axios.post( 'http://localhost:8888/wp-json/rae/v1/cart/items/', {
15+
const storedSession = getSession();
16+
const addOrViewCartConfig = getAddOrViewCartConfig();
17+
axios.post( ADD_TO_CART_ENDPOINT, {
1418
product_id: productId,
1519
quantity: qty,
1620
},
17-
{
18-
withCredentials: true,
19-
headers: {
20-
'X-Headless-CMS': true,
21-
},
22-
} )
21+
addOrViewCartConfig,
22+
)
2323
.then( ( res ) => {
24-
console.log( 'card added', res );
24+
25+
if ( ! isEmpty( storedSession ) ) {
26+
storeSession( res?.headers?.[ 'x-wc-session' ] );
27+
}
2528
viewCart();
2629
} )
2730
.catch( err => {
@@ -30,12 +33,8 @@ const AddToCart = ( { product } ) => {
3033
};
3134

3235
const viewCart = () => {
33-
axios.get( 'http://localhost:8888/wp-json/rae/v1/cart/items/', {
34-
withCredentials: true,
35-
headers: {
36-
'X-Headless-CMS': true,
37-
},
38-
} )
36+
const addOrViewCartConfig = getAddOrViewCartConfig();
37+
axios.get( ADD_TO_CART_ENDPOINT, addOrViewCartConfig )
3938
.then( ( res ) => {
4039
console.log( 'res', res );
4140
} )
@@ -46,8 +45,10 @@ const AddToCart = ( { product } ) => {
4645

4746

4847
return (
49-
<button className="bg-white hover:bg-gray-100 text-gray-800 font-semibold py-2 px-4 border border-gray-400 rounded shadow" onClick={ () => addToCart( product?.id ) }>Add to cart</button>
48+
<button
49+
className="bg-white hover:bg-gray-100 text-gray-800 font-semibold py-2 px-4 border border-gray-400 rounded shadow"
50+
onClick={ () => addToCart( product?.id ) }>Add to cart</button>
5051
);
51-
}
52+
};
5253

53-
export default AddToCart
54+
export default AddToCart;

src/utils/cart/api.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { getSession } from './session';
2+
import { isEmpty } from 'lodash';
3+
4+
export const getAddOrViewCartConfig = () => {
5+
6+
const config = {
7+
headers: {
8+
'X-Headless-CMS': true,
9+
},
10+
}
11+
12+
const storedSession = getSession();
13+
14+
if ( !isEmpty( storedSession ) ) {
15+
config.headers['x-wc-session'] = storedSession;
16+
}
17+
18+
return config;
19+
}

src/utils/cart/session.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { isEmpty } from 'lodash';
2+
3+
export const storeSession = ( session ) => {
4+
5+
if ( isEmpty( session ) ) {
6+
return null;
7+
}
8+
9+
localStorage.setItem( 'x-wc-session', session );
10+
}
11+
12+
export const getSession = () => {
13+
return localStorage.getItem( 'x-wc-session' );
14+
}

0 commit comments

Comments
 (0)