diff --git a/src/components/cart/add-to-cart.js b/src/components/cart/add-to-cart.js new file mode 100644 index 0000000..6067096 --- /dev/null +++ b/src/components/cart/add-to-cart.js @@ -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 ( + + ); +}; + +export default AddToCart; diff --git a/src/components/products/index.js b/src/components/products/index.js index b0c4526..ca40149 100644 --- a/src/components/products/index.js +++ b/src/components/products/index.js @@ -1,7 +1,5 @@ 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 }) => { @@ -9,27 +7,14 @@ const Products = ({ products }) => { return null; } + console.log( 'products', products ); + return (