Allow to delete session.
authorAlexandre Segura <mex.zktk@gmail.com>
Mon, 26 Jun 2017 17:40:28 +0200
changeset 93 469da13402e2
parent 92 5a73b44925f2
child 94 2c2a9c8dc216
Allow to delete session.
client/src/actions/sessionsActions.js
client/src/components/SessionList.js
client/src/constants/actionTypes.js
client/src/reducers/sessionsReducer.js
--- a/client/src/actions/sessionsActions.js	Mon Jun 26 16:50:16 2017 +0200
+++ b/client/src/actions/sessionsActions.js	Mon Jun 26 17:40:28 2017 +0200
@@ -47,6 +47,23 @@
   };
 }
 
+export const deleteSession = (session) => {
+  return {
+    type: types.DELETE_SESSION,
+    session: session,
+    meta: {
+      offline: {
+        effect: {
+          url: `/api/notes/sessions/${session.get('_id')}/`,
+          method: 'DELETE',
+        },
+        commit: { type: types.NOOP },
+        rollback: { type: types.NOOP }
+      }
+    }
+  };
+}
+
 export const loadSessions = () => {
   return {
     type: types.LOAD_SESSIONS
--- a/client/src/components/SessionList.js	Mon Jun 26 16:50:16 2017 +0200
+++ b/client/src/components/SessionList.js	Mon Jun 26 17:40:28 2017 +0200
@@ -16,6 +16,13 @@
     this.props.history.push('/sessions/' + sessionId);
   }
 
+  onClickDelete(session, e) {
+    e.preventDefault();
+    e.stopPropagation();
+
+    this.props.sessionsActions.deleteSession(session);
+  }
+
   render() {
     return (
       <div>
@@ -29,6 +36,9 @@
                     key={session.get('_id')}
                     onClick={() => this.props.history.push('/sessions/' + session.get('_id'))}>
                     {session.title || 'No title'} {session.get('_id')} {moment(session.get('date')).format('DD/MM/YYYY')}
+                    <a className="pull-right" onClick={ this.onClickDelete.bind(this, session) }>
+                      <span className="material-icons">delete</span>
+                    </a>
                   </ListGroupItem>
                 )}
               </ListGroup>
@@ -43,8 +53,6 @@
 
 function mapStateToProps(state, props) {
   return {
-    // currentSession: state.get('currentSession'),
-    // sessions: state.get('sessions')
     sessions: state['sessions']
   };
 }
--- a/client/src/constants/actionTypes.js	Mon Jun 26 16:50:16 2017 +0200
+++ b/client/src/constants/actionTypes.js	Mon Jun 26 17:40:28 2017 +0200
@@ -6,6 +6,7 @@
 
 export const CREATE_SESSION = 'CREATE_SESSION';
 export const UPDATE_SESSION = 'UPDATE_SESSION';
+export const DELETE_SESSION = 'DELETE_SESSION';
 export const LOAD_SESSIONS = 'LOAD_SESSIONS';
 
 export const AUTH_LOGIN_SUBMIT = 'AUTH_LOGIN_SUBMIT';
--- a/client/src/reducers/sessionsReducer.js	Mon Jun 26 16:50:16 2017 +0200
+++ b/client/src/reducers/sessionsReducer.js	Mon Jun 26 17:40:28 2017 +0200
@@ -2,27 +2,26 @@
 import * as types from '../constants/actionTypes';
 import SessionRecord from '../store/sessionRecord';
 
-// export const currentSession = (state = null, action) => {
-//   switch (action.type) {
-//     case types.CREATE_SESSION:
-//       return action.session;
-//     default:
-//       return state;
-//   }
-// };
+export const sessions = (state = Immutable.List([]), action) => {
+  let sessionIndex;
 
-export const sessions = (state = Immutable.List([]), action) => {
   switch (action.type) {
     case types.CREATE_SESSION:
       return state.push(new SessionRecord(action.session));
     case types.UPDATE_SESSION:
       const sessionToUpdate = state.find(session => session === action.session);
-      const sessionIndex = state.indexOf(action.session);
+      sessionIndex = state.indexOf(action.session);
       if (sessionIndex === -1) {
         return state;
       }
       const updatedSession = sessionToUpdate.merge(action.values);
       return state.set(sessionIndex, updatedSession);
+    case types.DELETE_SESSION:
+      sessionIndex = state.indexOf(action.session);
+      if (sessionIndex === -1) {
+        return state;
+      }
+      return state.delete(sessionIndex);
     case types.LOAD_SESSIONS:
       return action.sessions;
     default: