Commit 4415c132 authored by Shaphen Pangburn's avatar Shaphen Pangburn

[AFP-48 Shaphen Pangburn, Joe Lu]: Disable Add to Cart button if not logged in...

[AFP-48 Shaphen Pangburn, Joe Lu]: Disable Add to Cart button if not logged in and create user cart in backend if it does not already exist
parent b84c07d1
...@@ -7,5 +7,8 @@ const receiveUserCart = cart => ({ ...@@ -7,5 +7,8 @@ const receiveUserCart = cart => ({
cart cart
}) })
export const fetchUserCart = userId => dispatch => ApiUtil.fetchUserCart(userId) export const fetchUserCart = userEmail => dispatch => ApiUtil.fetchUserCart(userEmail)
.then(cart => dispatch(receiveUserCart(cart))); .then(cart => dispatch(receiveUserCart(cart)));
\ No newline at end of file
export const createUserCart = newCart => dispatch => ApiUtil.createCart(newCart)
.then(res => console.log('yay'))
\ No newline at end of file
...@@ -94,7 +94,7 @@ export default class ProductItem extends Component { ...@@ -94,7 +94,7 @@ export default class ProductItem extends Component {
placeholder="Qty" placeholder="Qty"
onChange={ this.handleChange("orderItemQuantity") } onChange={ this.handleChange("orderItemQuantity") }
/> />
<button className="add-to-cart-button">Add to Cart</button> <button disabled={ !this.props.user?.currentUser } className="add-to-cart-button">Add to Cart</button>
</form> </form>
<Modal <Modal
......
...@@ -23,7 +23,7 @@ export default class ProductMarket extends Component { ...@@ -23,7 +23,7 @@ export default class ProductMarket extends Component {
products.forEach(prod => { products.forEach(prod => {
promotions.forEach(promo => { promotions.forEach(promo => {
if (prod.sku === promo.productSku) { if (prod.sku === promo.productSku) {
prod.promo = (promo.discountPercentage * 100); prod.promo = (promo.discountPercentage);
} }
}); });
}); });
...@@ -40,7 +40,7 @@ export default class ProductMarket extends Component { ...@@ -40,7 +40,7 @@ export default class ProductMarket extends Component {
return ( return (
<div className={prod.productName ? "product-item-container" : "no-item"} key={ prod.sku }> <div className={prod.productName ? "product-item-container" : "no-item"} key={ prod.sku }>
{ prod.productName ? { prod.productName ?
<ProductItem item={ prod } cart={ this.props.cart }/> <ProductItem item={ prod } cart={ this.props.cart } user={ this.props.user } />
: :
<div className="product-item"> <div className="product-item">
<p id="prod-not-available">This Product is No Longer Available</p> <p id="prod-not-available">This Product is No Longer Available</p>
......
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import Session from './session'; import Session from './session';
import {login, logOut} from '../../actions/session_actions'; import {login, logOut} from '../../actions/session_actions';
import { fetchUserCart } from '../../actions/cart_actions'; import { fetchUserCart, createUserCart } from '../../actions/cart_actions';
const mSTP = state => ({ const mSTP = state => ({
currentUser: state.user currentUser: state.user
...@@ -10,7 +10,8 @@ const mSTP = state => ({ ...@@ -10,7 +10,8 @@ const mSTP = state => ({
const mDTP = dispatch => ({ const mDTP = dispatch => ({
login: (userResponse) => dispatch(login(userResponse)), login: (userResponse) => dispatch(login(userResponse)),
logOut: () => dispatch(logOut()), logOut: () => dispatch(logOut()),
getUserCart: userId => dispatch(fetchUserCart(userId)) getUserCart: userId => dispatch(fetchUserCart(userId)),
createCart: newCart => dispatch(createUserCart(newCart))
}); });
export default connect(mSTP, mDTP)(Session); export default connect(mSTP, mDTP)(Session);
\ No newline at end of file
...@@ -23,9 +23,17 @@ export default class Session extends Component { ...@@ -23,9 +23,17 @@ export default class Session extends Component {
const userResponse = {idToken: tokenId, userId, email, firstName, lastName, accessToken} const userResponse = {idToken: tokenId, userId, email, firstName, lastName, accessToken}
this.props.login(userResponse) this.props.login(userResponse)
this.setState({logIn: true}) this.setState({logIn: true})
// try grabbing user cart
this.props.getUserCart(email) this.props.getUserCart(email)
.then(res => res.json) .then(res => res.json)
.catch(err => err.json) .catch(err => {
const newCart = {
userId: email,
cartItems: []
}
this.props.createCart(newCart)
})
} }
loginFailed = (response) => { loginFailed = (response) => {
......
...@@ -9,7 +9,8 @@ const userReducer = (state = initialState, action) => { ...@@ -9,7 +9,8 @@ const userReducer = (state = initialState, action) => {
let newState = Object.assign({}, state); let newState = Object.assign({}, state);
switch (action.type) { switch (action.type) {
case SET_CURRENT_USER: case SET_CURRENT_USER:
return newState.currentUser = action.user.data; newState.currentUser = action.user.data;
return newState;
case LOGOUT_USER: case LOGOUT_USER:
return newState.currentUser = null; return newState.currentUser = null;
default: default:
......
import axios from 'axios'; import axios from 'axios';
export const fetchUserCart = userId => { export const fetchUserCart = userEmail => {
return axios.get(`http://localhost:8080/api/carts/${userId}`) return axios.get(`http://localhost:8080/api/carts/${userEmail}`)
}
export const createCart = newCart => {
debugger
return axios.post(`http://localhost:8080/api/carts`, newCart)
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment