import {
  FETCH_STUDENT_SUCCESS
} from '../actions/studentSearchActions';

import {
  SUCCESS_STATS_FETCHED,
} from '../actions/crudActions';

const initialState = {
  currentStudent: null,
  stats: [],
  updates: []
};

function updateCurrentStudent(currentStudent, action) {
  return Object.assign({}, currentStudent, {
    stats: action.payload.stats
  });
}

function updateStats(stateStats, action) {
  const stats = stateStats.filter(studentStats =>
    Object.keys(studentStats)[0] !== action.payload.studentId);

  const newItem = {};
  newItem[action.payload.studentId] = action.payload.stats;
  return stats.concat(newItem);
}

export default function studentReducer(state = initialState, action) {
  switch (action.type) {
    case FETCH_STUDENT_SUCCESS:
      return Object.assign({}, state, {
        currentStudent: action.payload.user
      });

    case SUCCESS_STATS_FETCHED:
      return Object.assign({}, state, {
        currentStudent: updateCurrentStudent(state.currentStudent, action),
        stats: updateStats(state.stats, action)
      });

    default:
      return state;
  }
}

@@ -103,16 +103,42 @@
 ETCHED,%0A
+  SUCCESS_UPDATES_FETCHED%0A
 %7D from '
@@ -383,16 +383,459 @@
  %7D);%0A%7D%0A%0A
+function updateCurrentStudentUpdates(currentStudent, action) %7B%0A  return Object.assign(%7B%7D, currentStudent, %7B%0A    updates: action.payload.updates%0A  %7D);%0A%7D%0A%0Afunction updateStudentUpdates(stateUpdates, action) %7B%0A  const stats = stateUpdates.filter(studentStats =%3E%0A    Object.keys(studentStats)%5B0%5D !== action.payload.studentId);%0A%0A  const newItem = %7B%7D;%0A  newItem%5Baction.payload.studentId%5D = action.payload.updates;%0A  return stats.concat(newItem);%0A%7D%0A%0A
 function
@@ -1472,16 +1472,16 @@
 ction),%0A
-
         
@@ -1527,24 +1527,251 @@
 %0A      %7D);%0A%0A
+    case SUCCESS_UPDATES_FETCHED:%0A      return Object.assign(%7B%7D, state, %7B%0A        currentStudent: updateCurrentStudentUpdates(state.currentStudent, action),%0A        stats: updateStudentUpdates(state.updates, action)%0A      %7D);%0A%0A
     default:

handle success updates fetched