Commit f799f576 authored by Kyle Muldoon's avatar Kyle Muldoon

Like half way through moving logic to utils / reducers. Order placement so...

Like half way through moving logic to utils / reducers. Order placement so close to working. Backend issue is ocurring
parent 258280dc
...@@ -3,6 +3,7 @@ import * as ApiUtil from '../util/cart_api_util'; ...@@ -3,6 +3,7 @@ import * as ApiUtil from '../util/cart_api_util';
export const RECEIVE_USER_CART = "RECEIVE_USER_CART"; export const RECEIVE_USER_CART = "RECEIVE_USER_CART";
export const CLEAR_USER_CART = "CLEAR_USER_CART"; export const CLEAR_USER_CART = "CLEAR_USER_CART";
const receiveUserCart = cart => ({ const receiveUserCart = cart => ({
type: RECEIVE_USER_CART, type: RECEIVE_USER_CART,
cart cart
...@@ -12,6 +13,7 @@ export const clearUserCart = () => ({ ...@@ -12,6 +13,7 @@ export const clearUserCart = () => ({
type: CLEAR_USER_CART type: CLEAR_USER_CART
}) })
export const fetchUserCart = userEmail => dispatch => ApiUtil.fetchUserCart(userEmail) export const fetchUserCart = userEmail => dispatch => ApiUtil.fetchUserCart(userEmail)
.then(cart => dispatch(receiveUserCart(cart))); .then(cart => dispatch(receiveUserCart(cart)));
...@@ -20,3 +22,4 @@ export const updateUserCart = (updatedCart, userEmail) => dispatch => ApiUtil.up ...@@ -20,3 +22,4 @@ export const updateUserCart = (updatedCart, userEmail) => dispatch => ApiUtil.up
export const createUserCart = newCart => dispatch => ApiUtil.createCart(newCart) export const createUserCart = newCart => dispatch => ApiUtil.createCart(newCart)
.then(res => res.json) .then(res => res.json)
import {priceCalcUtil, numItemsCalcUtil} from '../util/cart_processing_util'
export const GET_TOTAL_PRICE = "GET_TOTAL_PRICE"
export const GET_NUM_ITEMS = "GET_NUM_ITEMS"
export const GET_CART_PRODUCTS = "GET_CART_PRODUCTS"
export const getTotalPrice = (totalPrice) => ({
type: GET_TOTAL_PRICE,
payload: totalPrice
})
export const getNumItems = (numItems) => ({
type: GET_NUM_ITEMS,
payload: numItems
})
export const getCartProducts = (cartProducts) => ({
type: GET_CART_PRODUCTS,
payload: cartProducts
})
export const calcTotalPrice = (productList) => {
return (dispatch) => {
return dispatch(getTotalPrice(priceCalcUtil(productList)))
}
}
export const calcNumItems = (productList) => {
return (dispatch) => {
return dispatch(getNumItems(numItemsCalcUtil(productList)))
}
}
export const storeCartItemsInProcessing = (productList) => {
return (dispatch) => {
return dispatch(getCartProducts(productList))
}
}
\ No newline at end of file
...@@ -12,36 +12,26 @@ import {dispatchOrderInfo} from './../../actions/checkout_actions' ...@@ -12,36 +12,26 @@ import {dispatchOrderInfo} from './../../actions/checkout_actions'
export default function Checkout() { export default function Checkout() {
///////////////////////
// REDUX Global State
///////////////////////
// const {currentUser} = useSelector(state => state)
// const {cart} = useSelector(state => state)
///////////////////////
// Submit Action State
///////////////////////
const dispatch = useDispatch() const dispatch = useDispatch()
/////////////////////// ///////////////////////
// Shipping Info State // Shipping Info State
/////////////////////// ///////////////////////
const [firstName, setFirstName] = useState([]) const [firstName, setFirstName] = useState(["Guy"])
const [lastName, setLastName] = useState([]) const [lastName, setLastName] = useState(["Fieri"])
const [shippingAddress, setShippingAddress] = useState([]) const [shippingAddress, setShippingAddress] = useState(["1234 Flavortown Dr"])
const [aptSuiteNo, setAptSuiteNo] = useState([]) const [aptSuiteNo, setAptSuiteNo] = useState(["Apt 317"])
const [city, setCity] = useState([]) const [city, setCity] = useState(["Santa Rosa"])
const [state, setState] = useState([]) const [state, setState] = useState(["California"])
const [zipCode, setZipCode] = useState([]) const [zipCode, setZipCode] = useState(["90210"])
/////////////////////// ///////////////////////
// Billing Info State // Billing Info State
/////////////////////// ///////////////////////
const [cardNumber, setCardNumber] = useState(["123456789"]) const [cardNumber, setCardNumber] = useState(["123456789"])
const [cardHolderName, setCardHolderName] = useState(["Jane Doe"]) const [cardHolderName, setCardHolderName] = useState(["Guy Fieri"])
const [expirationDate, setExpirationDate] = useState(["05/20206"]) const [expirationDate, setExpirationDate] = useState(["05/20206"])
const [cvv, setCVV] = useState(["123"]) const [cvv, setCVV] = useState(["123"])
...@@ -49,10 +39,11 @@ export default function Checkout() { ...@@ -49,10 +39,11 @@ export default function Checkout() {
/////////////////////// ///////////////////////
// Order Review / Summary State // Order Review / Summary State
/////////////////////// ///////////////////////
const [numCartItems, setNumCartItems] = useState([3])
const [itemsTotal, setItemsTotal] = useState([87.65]) // arbitrary cart total const numCartItems = useSelector(state => state.cartProcessing.numItems)
const [shippingHandling, setShippingHandling] = useState([8.00]) // arbitraty shipping and handling amount const itemsTotal = useSelector(state => state.cartProcessing.totalPrice)
const [taxRate, setTaxRate] = useState([0.0725]) // california tax rate const taxRate = 0.0725
const shippingHandling = 9.00
/////////////////////// ///////////////////////
...@@ -63,6 +54,15 @@ export default function Checkout() { ...@@ -63,6 +54,15 @@ export default function Checkout() {
const [errorWhileValidating, setErrorWhileValidating] = useState([0]) const [errorWhileValidating, setErrorWhileValidating] = useState([0])
///////////////////////
// Form Submission State
///////////////////////
const user = useSelector(state => state.user.currentUser)
const cart = useSelector(state => state.cart)
// This effect listens for the submit button to be clicked // This effect listens for the submit button to be clicked
// It then checks each of the required fields to make sure they are non null // It then checks each of the required fields to make sure they are non null
useEffect( () => { useEffect( () => {
...@@ -125,30 +125,21 @@ export default function Checkout() { ...@@ -125,30 +125,21 @@ export default function Checkout() {
let chrisSpec = { let chrisSpec = {
"user": { "user": {
"userId": "e-com-test-id", "userId": user.email,
"email": "test@test.test", "email": user.email,
"firstName":"ecom", "firstName": user.firstName,
"lastName":"test" "lastName": user.lastName
}, },
"address": { "address": {
"state": "IL", "state": state,
"city": "chicago", "city": city,
"zip": "90210", "zip": zipCode,
"street": "Grand" "street": shippingAddress
}, },
"cart": { "cart": {
"id": "something", "id": "something",
"userId": "e-com-test-id", "userId": user.email,
"cartItems":[ "cartItems": cart
{
"quantity": 2,
"productRef": {
"id": "something",
"sku": "AFP-1",
"upc": "00002"
}
}
]
} }
} }
......
...@@ -3,7 +3,8 @@ import CartItem from './CartItem.js' ...@@ -3,7 +3,8 @@ import CartItem from './CartItem.js'
import './shopping-cart.css' import './shopping-cart.css'
import { useSelector, useDispatch } from 'react-redux' import { useSelector, useDispatch } from 'react-redux'
import { Link, Redirect } from 'react-router-dom' import { Link, Redirect } from 'react-router-dom'
import { updateUserCart } from './../../actions/cart_actions' import { updateUserCart} from './../../actions/cart_actions'
import { calcTotalPrice, calcNumItems, storeCartItemsInProcessing } from './../../actions/cart_processing_actions'
export default function ShoppingCart() { export default function ShoppingCart() {
...@@ -75,12 +76,16 @@ export default function ShoppingCart() { ...@@ -75,12 +76,16 @@ export default function ShoppingCart() {
//////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////
useEffect(() => { useEffect(() => {
if (!userSession) return; if (!userSession) return;
const cartUpdateObj = { const cartUpdateObj = {
userId: userSession.email, userId: userSession.email,
cartItems: cartRefs cartItems: cartRefs
} }
dispatch(updateUserCart(cartUpdateObj, cartUpdateObj.userId)) dispatch(updateUserCart(cartUpdateObj, cartUpdateObj.userId))
dispatch(storeCartItemsInProcessing(cartItems))
dispatch(calcTotalPrice(cartItems))
dispatch(calcNumItems(cartItems))
}, [cartItems]) }, [cartItems])
......
import {GET_TOTAL_PRICE, GET_NUM_ITEMS, GET_CART_PRODUCTS} from './../actions/cart_processing_actions'
const initialState = {
cartProducts: [],
numItems: 0,
totalPrice: 0
}
const cartProcessingReducer = (prevState = initialState, action) => {
Object.freeze(prevState)
const nextState = {...prevState}
switch (action.type) {
case GET_TOTAL_PRICE:
nextState.totalPrice = action.payload
return nextState
case GET_NUM_ITEMS:
nextState.numItems = action.payload
return nextState
case GET_CART_PRODUCTS:
nextState.cartProducts = action.payload
return nextState
default:
return nextState
}
}
export default cartProcessingReducer
\ No newline at end of file
...@@ -3,14 +3,15 @@ import productsReducer from './products_reducer'; ...@@ -3,14 +3,15 @@ import productsReducer from './products_reducer';
import orderReducer from './order_reducer' import orderReducer from './order_reducer'
import userReducer from './user_reducer' import userReducer from './user_reducer'
import cartReducer from './cart_reducer'; import cartReducer from './cart_reducer';
import cartProcessingReducer from './cart_processing_reducer'
const rootReducer = combineReducers({ const rootReducer = combineReducers({
market: productsReducer, market: productsReducer,
user: userReducer, user: userReducer,
cart: cartReducer, cart: cartReducer,
orderStatus: orderReducer orderStatus: orderReducer,
cartProcessing: cartProcessingReducer
}); });
export default rootReducer; export default rootReducer;
\ No newline at end of file
...@@ -9,6 +9,5 @@ export const createCart = newCart => { ...@@ -9,6 +9,5 @@ export const createCart = newCart => {
} }
export const updateCart = (updatedCart, userEmail) => { export const updateCart = (updatedCart, userEmail) => {
// debugger
return axios.put(`http://localhost:8080/api/carts/${userEmail}`, updatedCart) return axios.put(`http://localhost:8080/api/carts/${userEmail}`, updatedCart)
} }
\ No newline at end of file
export const priceCalcUtil = (productList) => {
const totalPrice = productList.reduce((acc, curr) => {return parseFloat(acc) + (parseFloat(curr.product.price) * parseFloat(curr.quantity))}, 0)
console.log("PRICE IN CART: " + totalPrice)
return totalPrice
}
export const numItemsCalcUtil = (productList) => {
const totalItems = productList.reduce((acc, curr) => {return parseInt(acc) + parseInt(curr.quantity)}, 0)
console.log("ITEMS IN CART: " + totalItems)
return totalItems
}
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