From 477d19750c011e46f9e28061ab6604544ae39bad Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Chif=20Gerg=C5=91?= <chifgeri97@gmail.com>
Date: Sun, 13 Jan 2019 12:51:39 +0100
Subject: [PATCH] Modify visitorChange action and handling it in the reducer

---
 src/actions/statistics.js    |  9 +++++----
 src/reducers/EventReducer.js | 20 +++++++++++++++++++-
 2 files changed, 24 insertions(+), 5 deletions(-)

diff --git a/src/actions/statistics.js b/src/actions/statistics.js
index 2d8023b..f52e6ff 100644
--- a/src/actions/statistics.js
+++ b/src/actions/statistics.js
@@ -2,7 +2,8 @@ import axios from './session';
 import {
   GET_EVENTS,
   GET_EVENT_BY_ID,
-  GET_TRAINEES, VISITOR_CHANGE,
+  GET_TRAINEES,
+  VISITOR_CHANGE,
   GET_NOTES_BY_EVENT,
   WRITE_EVENT,
   ADD_EVENT,
@@ -51,9 +52,9 @@ export const getTrainees = () => (
   }
 );
 
-export const visitorChange = visitors => (
-  dispatch => (dispatch({ type: VISITOR_CHANGE, payload: visitors }))
-);
+export const visitorChange = ({ id }) => {
+  return (dispatch => (dispatch({ type: VISITOR_CHANGE, payload: id })));
+};
 
 export const getNotesByEvent = id => (
   async (dispatch) => {
diff --git a/src/reducers/EventReducer.js b/src/reducers/EventReducer.js
index b082c05..0766ddd 100644
--- a/src/reducers/EventReducer.js
+++ b/src/reducers/EventReducer.js
@@ -16,7 +16,25 @@ export default (state = INITIAL_STATE, action) => {
     case GET_EVENT_BY_ID:
       return { ...state, selectedEvent: action.payload };
     case VISITOR_CHANGE:
-      return { ...state, selectedEvent: { ...state.selectedEvent, visitors: action.payload } };
+      const index = state.selectedEvent.visitors.indexOf(action.payload);
+      if (index !== -1) {
+        state.selectedEvent.visitors.splice(index, 1);
+        return {
+          ...state,
+          selectedEvent: {
+            ...state.selectedEvent,
+            visitors: state.selectedEvent.visitors,
+          },
+        };
+      }
+      state.selectedEvent.visitors.push(action.payload)
+      return {
+        ...state,
+        selectedEvent: {
+          ...state.selectedEvent,
+          visitors: state.selectedEvent.visitors,
+        },
+      };
     case WRITE_EVENT:
       return { ...state, newEvent: { ...state.newEvent, [action.target]: action.payload } };
     case ADD_EVENT:
-- 
GitLab