@@ -2,6 +2,8 @@ import { isEmpty } from 'lodash';
2
2
3
3
import axios from 'axios' ;
4
4
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' ;
5
7
6
8
const AddToCart = ( { product } ) => {
7
9
@@ -10,18 +12,19 @@ const AddToCart = ( { product } ) => {
10
12
}
11
13
12
14
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 , {
14
18
product_id : productId ,
15
19
quantity : qty ,
16
20
} ,
17
- {
18
- withCredentials : true ,
19
- headers : {
20
- 'X-Headless-CMS' : true ,
21
- } ,
22
- } )
21
+ addOrViewCartConfig ,
22
+ )
23
23
. then ( ( res ) => {
24
- console . log ( 'card added' , res ) ;
24
+
25
+ if ( ! isEmpty ( storedSession ) ) {
26
+ storeSession ( res ?. headers ?. [ 'x-wc-session' ] ) ;
27
+ }
25
28
viewCart ( ) ;
26
29
} )
27
30
. catch ( err => {
@@ -30,12 +33,8 @@ const AddToCart = ( { product } ) => {
30
33
} ;
31
34
32
35
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 )
39
38
. then ( ( res ) => {
40
39
console . log ( 'res' , res ) ;
41
40
} )
@@ -46,8 +45,10 @@ const AddToCart = ( { product } ) => {
46
45
47
46
48
47
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 >
50
51
) ;
51
- }
52
+ } ;
52
53
53
- export default AddToCart
54
+ export default AddToCart ;
0 commit comments