Skip to content

Commit

Permalink
updated checkout flow
Browse files Browse the repository at this point in the history
  • Loading branch information
mishaetaya committed Jan 24, 2017
1 parent e534156 commit 394d074
Show file tree
Hide file tree
Showing 14 changed files with 360 additions and 64 deletions.
13 changes: 11 additions & 2 deletions app/components/NavBar.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import React from 'react';
import Login from './Login';
import { Link } from 'react-router';

import axios from 'axios'

const NavBar = (props) =>{

const loginWithGmail = function(){

axios.post('/api/auth/google/login')

}

return(
<div>
<nav className="navbar navbar-default navbar-fixed-top">
Expand All @@ -25,7 +32,7 @@ const NavBar = (props) =>{

{ props.user && props.user.email ?
<div>
<li><a href="#">{props.user.name}'s Account</a></li>
<li><a href="#">{props.user.name}s Account</a></li>
<Link to="/shoppingcart">
View Cart
</Link>
Expand All @@ -37,8 +44,10 @@ const NavBar = (props) =>{
<Link to="/shoppingcart">
View Cart
</Link>
<div> <a href='#' onClick={()=>loginWithGmail()}> Log in with Gmail </a> </div>
</div>
}
<div className="g-signin2" data-onsuccess="onSignIn"></div>
</ul>
</div>
</div>
Expand Down
27 changes: 11 additions & 16 deletions app/components/ShoppingCart.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { connect } from 'react-redux';
import store from '../store.jsx'
import Promise from 'bluebird';
import axios from 'axios';
import { Link } from 'react-router';
import Checkout from './proceedToCheckout.jsx'

// this is the component
// const ShoppingCart = (props) => {
Expand All @@ -13,6 +15,7 @@ class ShoppingCart extends Component {
super();
this.state = {
shoppingCart: [],
// currentUser: {},
productToCheckForQuanity: {productId: 0, newQuantity: 0},
quantityAvailable: true,
unitsRemaining: 0
Expand All @@ -21,12 +24,13 @@ class ShoppingCart extends Component {
this.deleteFromCart = this.deleteFromCart.bind(this);
this.handleQuantity = this.handleQuantity.bind(this);
this.handleQuantityUpdate = this.handleQuantityUpdate.bind(this);
this.proceedToOrder = this.proceedToOrder.bind(this);

}

componentDidMount(){
this.setState({shoppingCart: this.props.shoppingCart.shoppingCart})
// axios.get('/api/auth/whoami')
// .then(res => this.setState({currentUser: res.auth}))
}

componentWillReceiveProps(nextProps) {
Expand All @@ -37,7 +41,6 @@ class ShoppingCart extends Component {
}

deleteFromCart(idToRemove){
console.log(idToRemove)
if(this.state.shoppingCart.length){
const currentCart = this.state.shoppingCart;
const newCart = currentCart.filter(elem => {return elem.id != idToRemove})
Expand Down Expand Up @@ -68,17 +71,11 @@ class ShoppingCart extends Component {

}

proceedToOrder(){
//create and submit to orders table
//update quantity on products
//move to the checkout page (maybe just create a div below?)
}

render(){
console.log(this.props)
return(

<div className="ShoppingCart_div">
<div className="container-fluid">
<div className="ShoppingCart_div">
<div className="panel panel-default">
<div className="panel-heading">Shopping Cart</div>

Expand Down Expand Up @@ -122,12 +119,10 @@ class ShoppingCart extends Component {
</div>
}

<div className="panel panel-default">
<div className="panel-heading">Address</div>
</div>

<button type="button" className="btn btn-primary">Proceed to Checkout</button>

<Link to="/checkout" >
<button type="button" className="btn btn-primary">Proceed to Checkout</button>
</Link>
</div>
</div>

)
Expand Down
52 changes: 52 additions & 0 deletions app/components/confirmation.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import React, { Component } from 'react';
import { connect } from 'react-redux';
import store from '../store.jsx'
import Promise from 'bluebird';
import axios from 'axios';
// this is the component

class Confirmation extends Component {
// const Confirmation = (props) => {
constructor(){
super();
}


render(){
const order = this.props.orderFromPreviousPage.shoppingCart.order;
const user = this.props.orderFromPreviousPage.auth;
console.log(this.props.orderFromPreviousPage)

return(

<div className="ShoppingCart_div" >
<h1> Dear {user ? user.name : 'Anonymous'}, </h1>
<p> Thank you for your purchase. Your confirmation number is: </p>
<p> {order.orderNumber} </p>
<p> A copy of your order has been sent to {order.email} </p>

</div>

)
}

}

export default Confirmation;


// this is the container
// const mapStateToProps = (state) => {
// return {
// shoppingCart: state.shoppingCart,
// auth: state.auth,
// order: state.order
// };
// };



// export default connect(
// mapStateToProps,
// )(Confirmation);

173 changes: 173 additions & 0 deletions app/components/proceedToCheckout.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
import React, { Component } from 'react';
import { connect } from 'react-redux';
import store from '../store.jsx'
import Promise from 'bluebird';
import axios from 'axios';
import { Link } from 'react-router';
import { purchase } from '../reducers/ShoppingCart_reducer';
import crypto from 'crypto';
import Confirmation from './confirmation'

// this is the component

class Checkout extends Component {
constructor(){
super();
this.state = {
currentuser:{},
order:{},
emailValid: false,
goToConfirmation: false,
error: false
}


this.updateAddress = this.updateAddress.bind(this);
this.sendToPurchase = this.sendToPurchase.bind(this);
this.emailValidation = this.emailValidation.bind(this);

}

componentDidMount(){
const id = this.props.auth ? this.props.auth.id : 'anonymous'
const order = {user_id: id, quantity: [], products: this.props.shoppingCart.shoppingCart}
this.setState({currentuser: this.props.auth, order: order})
}


updateAddress(event){
const order = this.state.order;
order.deliveryAddress = event.target.value;
this.setState({order: order})
}

sendToPurchase(event){
event.preventDefault();
const emailToPass = event.target.email.value;
const order = this.state.order;
order.email = emailToPass;

if(!this.state.currentuser){
order.orderNumber = crypto.randomBytes(5).toString('hex').toUpperCase();
this.setState({order: order})
axios.post('/api/sendmail', this.state.order)

} else{
// console.log(this.state.order)
axios.post('/api/orders', this.state.order)
.then((res) => {
if(res === 'error'){this.setState({error: true})}
else{
order.orderNumber = res.data.orderNumber;
this.setState({order:order})
this.setState({goToConfirmation: true})
this.props.purchase_func(this.state.order)

}
})

}

}

emailValidation(event){
event.preventDefault();
var validEmail = false;
const email = event.target.value;
const atpos = email.indexOf("@");
const dotpos = email.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=email.length) {
this.setState({emailValid: false})
} else this.setState({emailValid: true})

}

render(){
// console.log(this.state)
return(
<div className="container-fluid">
{!this.state.goToConfirmation
?
<div className="ShoppingCart_div" >
Welcome {this.props.auth ? this.props.auth.name : 'Unknown user'}
<form className="checkout" onSubmit={event => this.sendToPurchase(event)}>
<div className="form-group">
<label >Address</label>
<input onChange={event => this.updateAddress(event)}type="text" className="form-control" id="formGroupExampleInput" placeholder="Example input"/>
</div>
{this.props.auth
?
<div className="form-group">
<label>please enter your email</label>
<input onChange={event=>this.emailValidation(event)}name="email" type="text" className="form-control" id="formGroupExampleInput2" value={this.props.auth.email}/>


{
!this.state.emailValid
? <div className="alert alert-danger" style={{marginTop:'5px'}}>
<strong>Please enter a valid email!</strong>
</div>
: <button type="Submit" className='btn btn-primary'> Purchase </button>
}
</div>


: <div className="form-group">
<label >please enter your email</label>
<input onChange={event=>this.emailValidation(event)}name="email" type="text" className="form-control" id="formGroupExampleInput2" />
{
!this.state.emailValid
? <div className="alert alert-danger" style={{marginTop:'5px'}}>
<strong>Please enter a valid email!</strong>
</div>
: <button type="Submit" className='btn btn-primary'> Purchase </button>
}
</div>
}
</form>

</div>
: <div>
{!this.state.error
? <Confirmation orderFromPreviousPage={this.props}/>
: <div className="alert alert-danger" style={{marginTop:'5px'}}>
<strong>Sorry, one of your items is no longer available</strong>
</div>
}

</div>

}
</div>

)

}
}


// this is the container
const mapStateToProps = (state) => {
// console.log(state)
return {
shoppingCart: state.shoppingCart,
auth: state.auth,
order: state.order

};
};

const mapDispatchToProps = (dispatch) => {
return ({
purchase_func: function(ord) {
dispatch(purchase(ord));
}

});
};

export default connect(
mapStateToProps,
mapDispatchToProps
)(Checkout);

2 changes: 1 addition & 1 deletion app/constants.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export const PURCHASE = 'PROCEED_TO_CHECKOUT';
export const PURCHASE = 'PURCHASE';
export const UPDATE_CART = 'UPDATE_CART';
export const ADD_TO_CART = 'ADD_TO_CART';
7 changes: 6 additions & 1 deletion app/main.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import ShoppingCart from './components/ShoppingCart'

import ProductDetailContainer from './containers/ProductDetailContainer.jsx';
import ProductsHomeContainer from './containers/ProductsHomeContainer.jsx';

import Checkout from './components/proceedToCheckout'
import Confirmation from './components/confirmation'

render (
<Provider store={store}>
Expand All @@ -23,6 +24,10 @@ render (
<Route path="/products/:pId" component={ProductDetailContainer} />
<IndexRedirect to='/products' />
<Route path='/ShoppingCart' component={ShoppingCart} />
<Route path='/checkout' component={Checkout} />
<Route path='/confirmation' component={Confirmation} />


</Route>
</Router>
</Provider>,
Expand Down
Loading

0 comments on commit 394d074

Please sign in to comment.