diff --git a/src/actions/appInstance.js b/src/actions/appInstance.js index 03bd8a0d..d96341df 100644 --- a/src/actions/appInstance.js +++ b/src/actions/appInstance.js @@ -1,3 +1,4 @@ +import _ from 'lodash'; import { GET_APP_INSTANCE_CHANNEL, PATCH_APP_INSTANCE_CHANNEL, @@ -7,23 +8,37 @@ import { PATCH_APP_INSTANCE_SUCCEEDED, } from '../types'; -const getAppInstance = async ({ id, spaceId, subSpaceId } = {}, callback) => { +const getAppInstance = async ( + { id, spaceId, subSpaceId } = {}, + callback +) => async (dispatch, getState) => { try { - window.ipcRenderer.send(GET_APP_INSTANCE_CHANNEL, { - id, - spaceId, - subSpaceId, - }); + // first check to see if app instance is available in the redux store + const { Phase } = getState(); + const items = Phase.getIn(['current', 'content', 'items']); + const item = items && _.find(items, ['id', id]); + if (item && item.appInstance) { + callback({ + type: GET_APP_INSTANCE_SUCCEEDED, + payload: item.appInstance, + }); + } else { + window.ipcRenderer.send(GET_APP_INSTANCE_CHANNEL, { + id, + spaceId, + subSpaceId, + }); - window.ipcRenderer.once( - GET_APP_INSTANCE_CHANNEL, - async (event, response) => { - callback({ - type: GET_APP_INSTANCE_SUCCEEDED, - payload: response, - }); - } - ); + window.ipcRenderer.once( + GET_APP_INSTANCE_CHANNEL, + async (event, response) => { + callback({ + type: GET_APP_INSTANCE_SUCCEEDED, + payload: response, + }); + } + ); + } } catch (err) { console.error(err); } diff --git a/src/components/phase/PhaseApp.js b/src/components/phase/PhaseApp.js index 1a00f0d2..54662de7 100644 --- a/src/components/phase/PhaseApp.js +++ b/src/components/phase/PhaseApp.js @@ -27,6 +27,7 @@ class PhaseApp extends Component { asset: PropTypes.string, name: PropTypes.string, folder: PropTypes.string.isRequired, + dispatchGetAppInstance: PropTypes.func.isRequired, id: PropTypes.string.isRequired, phaseId: PropTypes.string.isRequired, spaceId: PropTypes.string.isRequired, @@ -60,6 +61,7 @@ class PhaseApp extends Component { handleReceiveMessage = event => { try { + const { dispatchGetAppInstance } = this.props; const { type, payload } = JSON.parse(event.data); switch (type) { @@ -70,7 +72,7 @@ class PhaseApp extends Component { case PATCH_APP_INSTANCE_RESOURCE: return patchAppInstanceResource(payload, this.postMessage); case GET_APP_INSTANCE: - return getAppInstance(payload, this.postMessage); + return dispatchGetAppInstance(payload, this.postMessage); default: return false; } @@ -142,6 +144,13 @@ const mapStateToProps = ({ User, Space }) => ({ User.getIn(['current', 'lang']), }); -const ConnectedComponent = connect(mapStateToProps)(PhaseApp); +const mapDispatchToProps = { + dispatchGetAppInstance: getAppInstance, +}; + +const ConnectedComponent = connect( + mapStateToProps, + mapDispatchToProps +)(PhaseApp); export default ConnectedComponent;