Skip to content

Commit

Permalink
fix: allow online use to access app instance
Browse files Browse the repository at this point in the history
Try and get the app instance from the Redux store before
fetching it from the local database.

closes #94
  • Loading branch information
juancarlosfarah committed Jun 14, 2019
1 parent a844542 commit 31d4e59
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 17 deletions.
45 changes: 30 additions & 15 deletions src/actions/appInstance.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import _ from 'lodash';
import {
GET_APP_INSTANCE_CHANNEL,
PATCH_APP_INSTANCE_CHANNEL,
Expand All @@ -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);
}
Expand Down
13 changes: 11 additions & 2 deletions src/components/phase/PhaseApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -60,6 +61,7 @@ class PhaseApp extends Component {

handleReceiveMessage = event => {
try {
const { dispatchGetAppInstance } = this.props;
const { type, payload } = JSON.parse(event.data);

switch (type) {
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;

0 comments on commit 31d4e59

Please sign in to comment.