Skip to content

Commit 15f9615

Browse files
committed
Add to cart
1 parent 34a84b3 commit 15f9615

File tree

4 files changed

+84
-19
lines changed

4 files changed

+84
-19
lines changed

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { isEmpty } from 'lodash';
2+
3+
import axios from 'axios';
4+
import { ADD_TO_CART_ENDPOINT } from '../../utils/constants/endpoints';
5+
6+
const AddToCart = ( { product } ) => {
7+
8+
if ( isEmpty( product ) ) {
9+
return null;
10+
}
11+
12+
const addToCart = ( productId, qty = 1 ) => {
13+
axios.post( ADD_TO_CART_ENDPOINT, {
14+
product_id: productId,
15+
quantity: qty,
16+
},
17+
{
18+
withCredentials: true,
19+
headers: {
20+
'X-Headless-CMS': true,
21+
},
22+
} )
23+
.then( ( res ) => {
24+
console.log( 'card added', res );
25+
} )
26+
.catch( err => {
27+
console.log( 'err', err );
28+
} );
29+
};
30+
31+
32+
return (
33+
<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>
34+
);
35+
}
36+
37+
export default AddToCart

src/components/products/index.js

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,20 @@
11
import { isArray, isEmpty } from 'lodash';
2-
import Link from 'next/link';
3-
import Image from '../image';
4-
import { sanitize } from '../../utils/miscellaneous';
2+
import Product from './product';
53

64
const Products = ({ products }) => {
75

86
if ( isEmpty( products ) || !isArray( products ) ) {
97
return null;
108
}
119

10+
console.log( 'products', products );
11+
1212
return (
1313
<div className="flex flex-wrap -mx-2 overflow-hidden">
1414

1515
{ products.length ? products.map( product => {
16-
const img = product?.images?.[0] ?? {};
1716
return (
18-
<div key={ product?.id } className="my-2 px-2 w-full overflow-hidden sm:w-1/2 md:w-1/3 xl:w-1/4">
19-
<Link href={product?.permalink ?? '/'}>
20-
<a>
21-
<Image
22-
sourceUrl={ img?.src ?? '' }
23-
altText={ img?.alt ?? ''}
24-
title={ product?.name ?? '' }
25-
width="380"
26-
height="380"
27-
/>
28-
<h3 className="font-bold uppercase">{ product?.name ?? '' }</h3>
29-
<div dangerouslySetInnerHTML={{ __html: sanitize( product?.price_html ?? '' ) }}/>
30-
</a>
31-
</Link>
32-
</div>
17+
<Product key={ product?.id } product={product} />
3318
)
3419
} ) : null }
3520

src/components/products/product.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import Link from 'next/link';
2+
import Image from '../image';
3+
import { sanitize } from '../../utils/miscellaneous';
4+
import AddToCart from '../cart/add-to-cart';
5+
import { isEmpty } from 'lodash';
6+
7+
const Product = ( { product } ) => {
8+
9+
if ( isEmpty( product ) ) {
10+
return null;
11+
}
12+
13+
const img = product?.images?.[0] ?? {};
14+
const productType = product?.type ?? '';
15+
16+
return (
17+
<div className="my-2 px-2 w-full overflow-hidden sm:w-1/2 md:w-1/3 xl:w-1/4">
18+
<Link href={product?.permalink ?? '/'}>
19+
<a>
20+
<Image
21+
sourceUrl={ img?.src ?? '' }
22+
altText={ img?.alt ?? ''}
23+
title={ product?.name ?? '' }
24+
width="380"
25+
height="380"
26+
/>
27+
<h3 className="font-bold uppercase my-2">{ product?.name ?? '' }</h3>
28+
<div className="mb-4" dangerouslySetInnerHTML={{ __html: sanitize( product?.price_html ?? '' ) }}/>
29+
</a>
30+
</Link>
31+
32+
{ 'simple' === productType ? <AddToCart product={product}/> : null }
33+
</div>
34+
)
35+
}
36+
37+
export default Product;

src/utils/constants/endpoints.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
11
export const HEADER_FOOTER_ENDPOINT = `${ process.env.NEXT_PUBLIC_WORDPRESS_SITE_URL }/wp-json/rae/v1/header-footer?header_location_id=hcms-menu-header&footer_location_id=hcms-menu-footer`;
22
export const GET_PRODUCTS_ENDPOINT = `${process.env.NEXT_PUBLIC_SITE_URL}/api/get-products`;
3+
4+
/**
5+
* Cart
6+
* @type {string}
7+
*/
8+
export const ADD_TO_CART_ENDPOINT = `${process.env.NEXT_PUBLIC_WORDPRESS_SITE_URL}/wp-json/rae/v1/cart/items`;

0 commit comments

Comments
 (0)