From 46e8173f5a8afb89899388dbb84da2c9c111d130 Mon Sep 17 00:00:00 2001
From: Chris Beer <cabeer@stanford.edu>
Date: Mon, 11 Mar 2019 18:17:14 -0700
Subject: [PATCH] Use removeCompanionWindow instead of closeCompanionWindow

---
 __tests__/src/actions/companionWindow.test.js |  3 ++-
 .../src/components/CompanionWindow.test.js    |  1 -
 __tests__/src/reducers/windows.test.js        | 19 +++++++++++++++++++
 src/components/CompanionWindow.js             |  2 +-
 src/containers/CompanionWindow.js             | 10 ++++++----
 src/state/actions/companionWindow.js          | 17 ++---------------
 src/state/actions/window.js                   |  1 -
 src/state/reducers/windows.js                 |  9 +++++++++
 8 files changed, 39 insertions(+), 23 deletions(-)

diff --git a/__tests__/src/actions/companionWindow.test.js b/__tests__/src/actions/companionWindow.test.js
index 183949b73..0e7a1df61 100644
--- a/__tests__/src/actions/companionWindow.test.js
+++ b/__tests__/src/actions/companionWindow.test.js
@@ -90,9 +90,10 @@ describe('companionWindow actions', () => {
 
   describe('removeCompanionWindow', () => {
     it('should return correct action object', () => {
-      const action = actions.removeCompanionWindow('cw-123');
+      const action = actions.removeCompanionWindow('window', 'cw-123');
       expect(action.type).toBe(ActionTypes.REMOVE_COMPANION_WINDOW);
       expect(action.id).toBe('cw-123');
+      expect(action.windowId).toBe('window');
     });
   });
 });
diff --git a/__tests__/src/components/CompanionWindow.test.js b/__tests__/src/components/CompanionWindow.test.js
index 3666ce275..a88faa330 100644
--- a/__tests__/src/components/CompanionWindow.test.js
+++ b/__tests__/src/components/CompanionWindow.test.js
@@ -45,7 +45,6 @@ describe('CompanionWindow', () => {
       const closeButton = companionWindow.find('WithStyles(IconButton)[aria-label="closeCompanionWindow"]');
       closeButton.simulate('click');
       expect(removeCompanionWindowEvent).toHaveBeenCalledTimes(1);
-      expect(removeCompanionWindowEvent).toHaveBeenCalledWith('x', 'abc123');
     });
   });
 
diff --git a/__tests__/src/reducers/windows.test.js b/__tests__/src/reducers/windows.test.js
index d39aad6e4..a7ee9845d 100644
--- a/__tests__/src/reducers/windows.test.js
+++ b/__tests__/src/reducers/windows.test.js
@@ -293,4 +293,23 @@ describe('windows reducer', () => {
       },
     });
   });
+
+  it('should handle REMOVE_COMPANION_WINDOW', () => {
+    // on the right, just tacks the new id on
+    expect(windowsReducer({
+      abc123: {
+        id: 'abc123',
+        companionWindowIds: ['123', 'xyz'],
+      },
+    }, {
+      type: ActionTypes.REMOVE_COMPANION_WINDOW,
+      id: 'xyz',
+      windowId: 'abc123',
+    })).toEqual({
+      abc123: {
+        id: 'abc123',
+        companionWindowIds: ['123'],
+      },
+    });
+  });
 });
diff --git a/src/components/CompanionWindow.js b/src/components/CompanionWindow.js
index 86eb87682..b474e90fa 100644
--- a/src/components/CompanionWindow.js
+++ b/src/components/CompanionWindow.js
@@ -71,7 +71,7 @@ export class CompanionWindow extends Component {
                   <IconButton
                     aria-label={t('closeCompanionWindow')}
                     className={classes.closeButton}
-                    onClick={() => { onCloseClick(windowId, id); }}
+                    onClick={onCloseClick}
                   >
                     <CloseIcon />
                   </IconButton>
diff --git a/src/containers/CompanionWindow.js b/src/containers/CompanionWindow.js
index b49ca85c4..ce99afa5d 100644
--- a/src/containers/CompanionWindow.js
+++ b/src/containers/CompanionWindow.js
@@ -27,10 +27,12 @@ const mapStateToProps = (state, { id, windowId }) => {
  * @memberof CompanionWindow
  * @private
  */
-const mapDispatchToProps = {
-  onCloseClick: actions.closeCompanionWindow,
-  updateCompanionWindow: actions.updateCompanionWindow,
-};
+const mapDispatchToProps = (dispatch, { windowId, id }) => ({
+  onCloseClick: () => dispatch(
+    actions.removeCompanionWindow(windowId, id),
+  ),
+  updateCompanionWindow: (...args) => dispatch(actions.updateCompanionWindow(...args)),
+});
 
 /**
  *
diff --git a/src/state/actions/companionWindow.js b/src/state/actions/companionWindow.js
index 044bbc648..02ef3a273 100644
--- a/src/state/actions/companionWindow.js
+++ b/src/state/actions/companionWindow.js
@@ -1,6 +1,5 @@
 import uuid from 'uuid/v4';
 import ActionTypes from './action-types';
-import { updateWindow } from './window';
 
 const defaultProps = {
   content: null,
@@ -34,18 +33,6 @@ export function updateCompanionWindow(windowId, id, payload) {
 }
 
 /** */
-export function removeCompanionWindow(id) {
-  return { type: ActionTypes.REMOVE_COMPANION_WINDOW, id };
-}
-
-/**
-* Close companion window and remove reference from window
-*/
-export function closeCompanionWindow(windowId, companionWindowId) {
-  return (dispatch, getState) => {
-    dispatch(removeCompanionWindow(companionWindowId));
-    const companionWindowIds = getState().windows[windowId].companionWindowIds
-      .filter(id => id !== companionWindowId);
-    dispatch(updateWindow(windowId, { companionWindowIds }));
-  };
+export function removeCompanionWindow(windowId, id) {
+  return { type: ActionTypes.REMOVE_COMPANION_WINDOW, id, windowId };
 }
diff --git a/src/state/actions/window.js b/src/state/actions/window.js
index ff246162c..cc195dfb2 100644
--- a/src/state/actions/window.js
+++ b/src/state/actions/window.js
@@ -1,6 +1,5 @@
 import uuid from 'uuid/v4';
 import ActionTypes from './action-types';
-import { removeCompanionWindow } from './companionWindow';
 
 /**
  * focusWindow - action creator
diff --git a/src/state/reducers/windows.js b/src/state/reducers/windows.js
index 8a0818767..9ac1ce28a 100644
--- a/src/state/reducers/windows.js
+++ b/src/state/reducers/windows.js
@@ -117,6 +117,15 @@ export const windowsReducer = (state = {}, action) => {
           companionWindowIds: state[action.windowId].companionWindowIds.concat([action.id]),
         },
       };
+    case ActionTypes.REMOVE_COMPANION_WINDOW:
+      return {
+        ...state,
+        [action.windowId]: {
+          ...state[action.windowId],
+          companionWindowIds: state[action.windowId]
+            .companionWindowIds.filter(id => id !== action.id),
+        },
+      };
     default:
       return state;
   }
-- 
GitLab