Commit 198d7348 authored by Shaphen Pangburn's avatar Shaphen Pangburn

[AFP-48 Shaphen Pangburn]: Clear global state cart when logging out

parent 39ca21a0
import * as ApiUtil from '../util/cart_api_util'; 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";
const receiveUserCart = cart => ({ const receiveUserCart = cart => ({
type: RECEIVE_USER_CART, type: RECEIVE_USER_CART,
cart cart
}) })
export const clearUserCart = () => ({
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)));
......
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, createUserCart } from '../../actions/cart_actions'; import { fetchUserCart, createUserCart, clearUserCart } from '../../actions/cart_actions';
const mSTP = state => ({ const mSTP = state => ({
currentUser: state.user currentUser: state.user
...@@ -11,7 +11,8 @@ const mDTP = dispatch => ({ ...@@ -11,7 +11,8 @@ 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)) createCart: newCart => dispatch(createUserCart(newCart)),
clearCart: () => dispatch(clearUserCart())
}); });
export default connect(mSTP, mDTP)(Session); export default connect(mSTP, mDTP)(Session);
\ No newline at end of file
...@@ -42,6 +42,7 @@ export default class Session extends Component { ...@@ -42,6 +42,7 @@ export default class Session extends Component {
logOutSuccess = (response) => { logOutSuccess = (response) => {
this.props.logOut() this.props.logOut()
this.props.clearCart()
this.setState({logIn: false}) this.setState({logIn: false})
} }
......
import { RECEIVE_USER_CART } from '../actions/cart_actions'; import { RECEIVE_USER_CART, CLEAR_USER_CART } from '../actions/cart_actions';
const initialState = [] const initialState = []
...@@ -8,6 +8,8 @@ const cartReducer = (state = initialState, action) => { ...@@ -8,6 +8,8 @@ const cartReducer = (state = initialState, action) => {
switch (action.type) { switch (action.type) {
case RECEIVE_USER_CART: case RECEIVE_USER_CART:
return newState.cart = action.cart.data.cartItems return newState.cart = action.cart.data.cartItems
case CLEAR_USER_CART:
return []
default: default:
return state; return state;
} }
......
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