Skip to content

Feature/add to cart #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/components/cart/add-to-cart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { isEmpty } from 'lodash';

import axios from 'axios';
import { ADD_TO_CART_ENDPOINT } from '../../utils/constants/endpoints';
import { getSession, storeSession } from '../../utils/cart/session';
import { getAddOrViewCartConfig, getAddToCartConfig } from '../../utils/cart/api';

const AddToCart = ( { product } ) => {

if ( isEmpty( product ) ) {
return null;
}

const addToCart = ( productId, qty = 1 ) => {
const storedSession = getSession();
const addOrViewCartConfig = getAddOrViewCartConfig();
axios.post( ADD_TO_CART_ENDPOINT, {
product_id: productId,
quantity: qty,
},
addOrViewCartConfig,
)
.then( ( res ) => {

if ( ! isEmpty( storedSession ) ) {
storeSession( res?.headers?.[ 'x-wc-session' ] );
}
viewCart();
} )
.catch( err => {
console.log( 'err', err );
} );
};

const viewCart = () => {
const addOrViewCartConfig = getAddOrViewCartConfig();
axios.get( ADD_TO_CART_ENDPOINT, addOrViewCartConfig )
.then( ( res ) => {
console.log( 'res', res );
} )
.catch( err => {
console.log( 'err', err );
} );
};


return (
<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>
);
};

export default AddToCart;
23 changes: 4 additions & 19 deletions src/components/products/index.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,20 @@
import { isArray, isEmpty } from 'lodash';
import Link from 'next/link';
import Image from '../image';
import { sanitize } from '../../utils/miscellaneous';
import Product from './product';

const Products = ({ products }) => {

if ( isEmpty( products ) || !isArray( products ) ) {
return null;
}

console.log( 'products', products );

return (
<div className="flex flex-wrap -mx-2 overflow-hidden">

{ products.length ? products.map( product => {
const img = product?.images?.[0] ?? {};
return (
<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">
<Link href={product?.permalink ?? '/'}>
<a>
<Image
sourceUrl={ img?.src ?? '' }
altText={ img?.alt ?? ''}
title={ product?.name ?? '' }
width="380"
height="380"
/>
<h3 className="font-bold uppercase">{ product?.name ?? '' }</h3>
<div dangerouslySetInnerHTML={{ __html: sanitize( product?.price_html ?? '' ) }}/>
</a>
</Link>
</div>
<Product key={ product?.id } product={product} />
)
} ) : null }

Expand Down
37 changes: 37 additions & 0 deletions src/components/products/product.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import Link from 'next/link';
import Image from '../image';
import { sanitize } from '../../utils/miscellaneous';
import AddToCart from '../cart/add-to-cart';
import { isEmpty } from 'lodash';

const Product = ( { product } ) => {

if ( isEmpty( product ) ) {
return null;
}

const img = product?.images?.[0] ?? {};
const productType = product?.type ?? '';

return (
<div className="my-2 px-2 w-full overflow-hidden sm:w-1/2 md:w-1/3 xl:w-1/4">
<Link href={product?.permalink ?? '/'}>
<a>
<Image
sourceUrl={ img?.src ?? '' }
altText={ img?.alt ?? ''}
title={ product?.name ?? '' }
width="380"
height="380"
/>
<h3 className="font-bold uppercase my-2">{ product?.name ?? '' }</h3>
<div className="mb-4" dangerouslySetInnerHTML={{ __html: sanitize( product?.price_html ?? '' ) }}/>
</a>
</Link>

{ 'simple' === productType ? <AddToCart product={product}/> : null }
</div>
)
}

export default Product;
Empty file added src/utils/cart/api.js
Empty file.
Empty file added src/utils/cart/index.js
Empty file.
Empty file added src/utils/cart/session.js
Empty file.
6 changes: 6 additions & 0 deletions src/utils/constants/endpoints.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
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`;
export const GET_PRODUCTS_ENDPOINT = `${process.env.NEXT_PUBLIC_SITE_URL}/api/get-products`;

/**
* Cart
* @type {string}
*/
export const ADD_TO_CART_ENDPOINT = `${process.env.NEXT_PUBLIC_WORDPRESS_SITE_URL}/wp-json/rae/v1/cart/items/`;