Skip to content

Commit 318c992

Browse files
authored
Merge pull request #17 from imranhsayed/feaure/add-to-cart-update
Add To Cart And View Cart API
2 parents 33ca10f + dd8bfcf commit 318c992

File tree

8 files changed

+101
-44
lines changed

8 files changed

+101
-44
lines changed

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

Lines changed: 4 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,18 @@
11
import { isEmpty } from 'lodash';
2-
3-
import axios from 'axios';
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';
2+
import {addToCart} from '../../utils/cart';
73

84
const AddToCart = ( { product } ) => {
95

106
if ( isEmpty( product ) ) {
117
return null;
128
}
139

14-
const addToCart = ( productId, qty = 1 ) => {
15-
const storedSession = getSession();
16-
const addOrViewCartConfig = getAddOrViewCartConfig();
17-
axios.post( ADD_TO_CART_ENDPOINT, {
18-
product_id: productId,
19-
quantity: qty,
20-
},
21-
addOrViewCartConfig,
22-
)
23-
.then( ( res ) => {
24-
25-
if ( ! isEmpty( storedSession ) ) {
26-
storeSession( res?.headers?.[ 'x-wc-session' ] );
27-
}
28-
viewCart();
29-
} )
30-
.catch( err => {
31-
console.log( 'err', err );
32-
} );
33-
};
34-
35-
const viewCart = () => {
36-
const addOrViewCartConfig = getAddOrViewCartConfig();
37-
axios.get( ADD_TO_CART_ENDPOINT, addOrViewCartConfig )
38-
.then( ( res ) => {
39-
console.log( 'res', res );
40-
} )
41-
.catch( err => {
42-
console.log( 'err', err );
43-
} );
44-
};
45-
46-
4710
return (
4811
<button
4912
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>
13+
onClick={ () => addToCart( product?.id ?? 0 ) }>
14+
Add to cart
15+
</button>
5116
);
5217
};
5318

src/components/layouts/header/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const Header = ( { header } ) => {
1818
<link rel="icon" href={ favicon || '/favicon.ico' }/>
1919
</Head>
2020
<div className="header">
21-
<nav className="bg-white p-4">
21+
<nav className="bg-white py-5">
2222
<div className="flex items-center justify-between flex-wrap container mx-auto">
2323
<div className="flex items-center flex-shrink-0 text-black mr-20">
2424
<Link href="/">

src/components/products/index.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ const Products = ({ products }) => {
77
return null;
88
}
99

10-
console.log( 'products', products );
11-
1210
return (
1311
<div className="flex flex-wrap -mx-2 overflow-hidden">
1412

src/utils/cart/api.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
}
20+

src/utils/cart/index.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { getSession, storeSession } from './session';
2+
import { getAddOrViewCartConfig } from './api';
3+
import axios from 'axios';
4+
import { CART_ENDPOINT } from '../constants/endpoints';
5+
import { isEmpty } from 'lodash';
6+
7+
/**
8+
* Add To Cart Request Handler.
9+
*
10+
* @param {int} productId Product Id.
11+
* @param {int} qty Product Quantity.
12+
*/
13+
export const addToCart = ( productId, qty = 1 ) => {
14+
15+
const storedSession = getSession();
16+
const addOrViewCartConfig = getAddOrViewCartConfig();
17+
18+
axios.post( CART_ENDPOINT, {
19+
product_id: productId,
20+
quantity: qty,
21+
},
22+
addOrViewCartConfig,
23+
)
24+
.then( ( res ) => {
25+
26+
if ( isEmpty( storedSession ) ) {
27+
storeSession( res?.headers?.[ 'x-wc-session' ] );
28+
}
29+
viewCart();
30+
} )
31+
.catch( err => {
32+
console.log( 'err', err );
33+
} );
34+
};
35+
36+
/**
37+
* View Cart Request Handler
38+
*/
39+
export const viewCart = () => {
40+
41+
const addOrViewCartConfig = getAddOrViewCartConfig();
42+
43+
axios.get( CART_ENDPOINT, addOrViewCartConfig )
44+
.then( ( res ) => {
45+
console.log( 'res', res );
46+
} )
47+
.catch( err => {
48+
console.log( 'err', err );
49+
} );
50+
};
51+

src/utils/cart/session.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
}
15+

src/utils/constants/endpoints.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ export const GET_PRODUCTS_ENDPOINT = `${process.env.NEXT_PUBLIC_SITE_URL}/api/ge
55
* Cart
66
* @type {string}
77
*/
8-
export const ADD_TO_CART_ENDPOINT = `${process.env.NEXT_PUBLIC_WORDPRESS_SITE_URL}/wp-json/rae/v1/cart/items/`;
8+
export const CART_ENDPOINT = `${process.env.NEXT_PUBLIC_WORDPRESS_SITE_URL}/wp-json/rae/v1/cart/items/`;

tailwind.config.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,15 @@ module.exports = {
33
'./src/components/**/*.js',
44
'./pages/**/*.js'],
55
theme: {
6-
extend: {},
6+
container: {
7+
padding: {
8+
DEFAULT: '1rem',
9+
md: '2rem',
10+
lg: '4rem',
11+
xl: '5rem',
12+
'2xl': '6rem',
13+
},
14+
},
715
},
816
variants: {},
917
plugins: [

0 commit comments

Comments
 (0)