Commit 8a4d479a authored by Christopher Cottier's avatar Christopher Cottier

proper state management of order history

parent 0988926b
import OrderHistory from "../components/order-history/order-history";
import { getOrderHistory } from "../util/order_api_util";
export const SET_USER_HISTORY = "SET_USER_HISTORY";
export const dispatchOrderHistory = (userId) => async dispatch =>{
const orderHistory = await getOrderHistory(userId);
return dispatch(orderHistoryAction(orderHistory));
}
const orderHistoryAction = (orderHistory) => (
{
type: SET_USER_HISTORY,
orderHistory
}
)
\ No newline at end of file
import React, { useEffect, useState } from 'react'
import { useSelector } from 'react-redux';
import { useDispatch, useSelector } from 'react-redux';
import { dispatchOrderHistory } from '../../actions/order_history_actions';
import { getOrderHistory } from '../../util/order_api_util';
import Order from './Order';
......@@ -7,22 +8,24 @@ import './order-history.css'
const OrderHistory = () => {
const [orderHistory, setOrderHistory] = useState([]);
const [callMade, setCallMade] = useState(false);
const dispatch = useDispatch();
const {currentUser} = useSelector(state => state.user);
const user = useSelector(state => state.user);
const {orderHistory} = useSelector(state => state.orderStatus)
//const [callMade, setCallMade] = useState(false);
useEffect( async () => {
if (callMade || currentUser === null) return;
const userHistory = await getOrderHistory(currentUser.userId); //insert user id from user details stored in redux
setOrderHistory(userHistory);
setCallMade(true);
});
if (!user) return;
if (!user.currentUser) return;
dispatch(dispatchOrderHistory(user.currentUser.userId));
//setCallMade(true);
}, [user]);
return (
<main>
<h1 id="order-history-header">Order History</h1>
{currentUser === null ? (
{user === null ? (
<div>Please login to place orders!</div>
) : null}
<div id="orders">
......
import { SET_USER_HISTORY } from '../actions/order_history_actions';
import { LOGOUT_USER } from '../actions/session_actions';
import {SEND_USER_ORDER} from './../actions/checkout_actions'
const initialState = {
orderResponse: {},
orderHistory: []
}
const orderReducer = (prevState = initialState, action) => {
......@@ -13,6 +16,12 @@ const orderReducer = (prevState = initialState, action) => {
nextState.orderResponse = action.payload
return nextState
case SET_USER_HISTORY:
nextState.orderHistory = action.orderHistory;
return nextState;
case LOGOUT_USER:
nextState.orderHistory = [];
return nextState;
default:
return nextState
}
......
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