Skip to content

Commit

Permalink
Updated auth library and added cart
Browse files Browse the repository at this point in the history
  • Loading branch information
zanselm5 committed May 11, 2017
1 parent 0be1e8f commit bd808bc
Show file tree
Hide file tree
Showing 10 changed files with 29,498 additions and 7 deletions.
29,308 changes: 29,307 additions & 1 deletion dist/js/app.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/js/app.map

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/components/pages/Contacts.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import React from 'react';
import {Link, withRouter} from 'react-router-dom';
import ContactService from '../../services/ContactService';

class Contacts extends React.Component {
constructor() {
Expand All @@ -16,6 +17,7 @@ class Contacts extends React.Component {

componentDidMount() {
document.title = "Sandbox | Contacts";
ContactService.getAll();
}

render() {
Expand Down
3 changes: 2 additions & 1 deletion src/interceptors.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ const initInterceptors = (history, baseUrl = 'http://localhost:8000/api/', timeo
return response;
}, (error) => {
if (error.response) {
if (error.response.status == 401 || error.response.data.statusCode == 401) {
if (error.response.status == 401 || error.response.data.statusCode === 401) {
store.dispatch(UserActions.setRedirect(window.location.pathname));
store.dispatch(UserActions.logout());
store.dispatch(AlertActions.addAlert({
title: 'Not Authorized',
Expand Down
8 changes: 6 additions & 2 deletions src/library/authentication/components/configureAuthRoute.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ const mapStateToProps = (state) => {

const mapDispatchToProps = (dispatch) => {
return bindActionCreators({
'addAlert': AlertActions.addAlert
'addAlert': AlertActions.addAlert,
'setRedirect': UserActions.setRedirect
}, dispatch);
};

Expand All @@ -42,14 +43,16 @@ function configureAuthRoute(roleConfig) {
setTimeout(() => {
this.showAlert('notAuthenticated');
});
this.props.setRedirect(this.props.path);
_redirectPath = '/login';
return false;
} else {
let accessGranted = checkAuthorization(accessLevels, this.props.user, roleConfig);
if (accessGranted) {
return true;
} else {
_redirectPath = '/login';
this.props.setRedirect(this.props.path);
_redirectPath = this.props.customRedirect || '/login';
setTimeout(() => {
this.showAlert('notAuthorized');
});
Expand Down Expand Up @@ -87,6 +90,7 @@ function configureAuthRoute(roleConfig) {
const routeProps = Object.assign({}, this.props);
delete routeProps.access;
delete routeProps.component;
delete routeProps.customRedirect;

let isAuthorized = this.checkAccess(accessLevels);

Expand Down
54 changes: 54 additions & 0 deletions src/library/cart/actions/CartActions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
'use strict';

import CartItemConstants from '../constants/CartItemConstants';

export default {
add: (product, qty) => {
let data = {
'product': product,
'cartQty': parseInt(qty, 10)
};
return (dispatch) => {
dispatch({
'type': CartItemConstants.ADD_CART_ITEM,
'data': data
});
};
},
clearCart: () => {
return (dispatch) => {
dispatch({
'type': CartItemConstants.CLEAR_CART,
'data': true
});
};
},
toggle: (showHide) => {
return (dispatch) => {
dispatch({
'type': CartItemConstants.TOGGLE,
'data': showHide
});
};
},
update: (product, newCartQty) => {
let data = {
'product': product,
'cartQty': parseInt(newCartQty, 10)
};
return (dispatch) => {
dispatch({
'type': CartItemConstants.UPDATE_CART_ITEM,
'data': data
});
};
},
remove: (itemId) => {
return (dispatch) => {
dispatch({
'type': CartItemConstants.REMOVE_CART_ITEM,
'id': itemId
});
};
}
};
12 changes: 12 additions & 0 deletions src/library/cart/constants/CartItemConstants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

import keyMirror from 'keymirror';

export default keyMirror({
ADD_CART_ITEM: null,
CLEAR_CART: null,
TOGGLE: null,
UPDATE_CART_ITEM: null,
REMOVE_CART_ITEM: null,
UPDATE_CART_TOTAL: null
});
11 changes: 11 additions & 0 deletions src/library/cart/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

import CartActions from './actions/CartActions';
import {cartIsActive, cartItems, cartQtyPlaceholders} from './reducers/cartItems';

export {
CartActions,
cartIsActive,
cartItems,
cartQtyPlaceholders
};
99 changes: 99 additions & 0 deletions src/library/cart/reducers/cartItems.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
'use strict';

import CartItemConstants from '../constants/CartItemConstants';

const updateSessionCart = (newCartItems) => {
sessionStorage.setItem('cartItems', JSON.stringify(newCartItems));
};

const updateSessionPlaceholders = (placeholders) => {
sessionStorage.setItem('cartQuantities', JSON.stringify(placeholders));
};

const cartIsActive = (state = 'hide', action) => {
switch (action.type) {
case CartItemConstants.TOGGLE:
return action.data;
default:
return state;
}
};

const cartItems = (state = [], action) => {
let cartItems, newItem, index;
switch (action.type) {
case CartItemConstants.ADD_CART_ITEM:
cartItems = [...state];
newItem = action.data;
index = state.findIndex((item) => item.product.id === action.data.product.id);
if (index < 0) {
cartItems.push(newItem);
} else {
cartItems[index].product = action.data.product;
cartItems[index].cartQty += action.data.cartQty;
}
updateSessionCart(cartItems);
return cartItems;
case CartItemConstants.CLEAR_CART:
return [];
case CartItemConstants.UPDATE_CART_ITEM:
cartItems = [...state];
newItem = action.data;
index = cartItems.findIndex((item) => item.product.id === action.data.product.id);
if (index < 0) {
cartItems.push(newItem);
} else {
cartItems[index].product = action.data.product;
cartItems[index].cartQty = action.data.cartQty;
}
if (cartItems[index].cartQty < 1) {
cartItems.splice(index, 1);
}
updateSessionCart(cartItems);
return cartItems;
case CartItemConstants.REMOVE_CART_ITEM:
cartItems = [...state];
index = state.findIndex((cartItem) => cartItem.product.id === action.id);
if (index !== -1) {
cartItems.splice(index, 1);
}
updateSessionCart(cartItems);
return cartItems;
default:
return state;
}
};

const cartQtyPlaceholders = (state = {}, action) => {
let placeholders, productId;
switch (action.type) {
case CartItemConstants.ADD_CART_ITEM:
placeholders = Object.assign({}, state);
productId = action.data.product.id;
placeholders[productId] = placeholders[productId] ? placeholders[productId] + action.data.cartQty: action.data.cartQty;
updateSessionPlaceholders(placeholders);
return placeholders;
case CartItemConstants.CLEAR_CART:
return {};
case CartItemConstants.UPDATE_CART_ITEM:
placeholders = Object.assign({}, state);
productId = action.data.product.id;
placeholders[productId] = action.data.cartQty;
updateSessionPlaceholders(placeholders);
return placeholders;
case CartItemConstants.REMOVE_CART_ITEM:
placeholders = Object.assign({}, state);
productId = action.id;
placeholders[productId] = 0;
updateSessionPlaceholders(placeholders);
return placeholders;
default:
return state;
}
};

export {
cartItems,
cartQtyPlaceholders,
cartIsActive
};
6 changes: 4 additions & 2 deletions src/library/validations/components/Input.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ class Input extends React.Component {

return (
<div className="validate-error-element">
<input className={validationClasses} type={this.props.type} name={this.props.name} value={this.props.value} placeholder={this.props.placeholder} min={this.props.min} max={this.props.max} minLength={this.props.minlength} maxLength={this.props.maxlength} onChange={this.validateInput} onMouseDown={this.handleMouseDown} onFocus={this.handleFocus} onBlur={this.handleBlur} autoComplete={this.props.autoComplete} disabled={this.props.disabled}/>
<input className={validationClasses} type={this.props.type} name={this.props.name} value={this.props.value} placeholder={this.props.placeholder} step={this.props.step} min={this.props.min} max={this.props.max} minLength={this.props.minlength} maxLength={this.props.maxlength} onChange={this.validateInput} onMouseDown={this.handleMouseDown} onFocus={this.handleFocus} onBlur={this.handleBlur} autoComplete={this.props.autoComplete} disabled={this.props.disabled}/>
<div className="validate-errors">
{
this.state.errors.map((error, i) =>
Expand All @@ -232,6 +232,7 @@ Input.propTypes = {
'minlength': PropTypes.number,
'maxlength': PropTypes.number,
'max': PropTypes.number,
'step': PropTypes.string,
'validate': PropTypes.string,
'handleInputChange': PropTypes.func.isRequired,
'preserveState': PropTypes.bool,
Expand All @@ -242,7 +243,8 @@ Input.propTypes = {

Input.defaultProps = {
'autoComplete': 'on',
'preserveState': false
'preserveState': false,
'step': '1'
};

export default withRouter(connect(mapStateToProps, mapDispatchToProps)(Input));

0 comments on commit bd808bc

Please sign in to comment.