Skip to content

Commit

Permalink
feat: retrieve post actions and save it in appInstance
Browse files Browse the repository at this point in the history
  • Loading branch information
pyphilia committed Mar 2, 2020
1 parent e98573f commit 47b4fc0
Show file tree
Hide file tree
Showing 10 changed files with 128 additions and 1 deletion.
1 change: 1 addition & 0 deletions public/app/config/channels.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@ module.exports = {
CLEARED_USER_INPUT_CHANNEL: 'space:cleared',
SHOW_CLEAR_USER_INPUT_PROMPT_CHANNEL: 'prompt:space:clear:show',
RESPOND_CLEAR_USER_INPUT_PROMPT_CHANNEL: 'prompt:space:clear:respond',
POST_ACTION_CHANNEL: 'action:post',
};
2 changes: 2 additions & 0 deletions public/app/listeners/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const getDeveloperMode = require('./getDeveloperMode');
const setDeveloperMode = require('./setDeveloperMode');
const clearUserInput = require('./clearUserInput');
const showClearUserInputPrompt = require('./showClearUserInputPrompt');
const postAction = require('./postAction');

module.exports = {
loadSpace,
Expand All @@ -40,4 +41,5 @@ module.exports = {
setDeveloperMode,
clearUserInput,
showClearUserInputPrompt,
postAction,
};
63 changes: 63 additions & 0 deletions public/app/listeners/postAction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
const ObjectId = require('bson-objectid');
const { POST_ACTION_CHANNEL } = require('../config/channels');

const postAction = (mainWindow, db) => (event, payload = {}) => {
try {
const {
userId,
appInstanceId,
spaceId,
subSpaceId,
format,
verb,
data,
visibility = 'private',
} = payload;

// prepare the timestamp
const now = new Date();

// prepare the resource that we will create
const actionToWrite = {
appInstance: appInstanceId,
createdAt: now,
updatedAt: now,
data,
format,
verb,
visibility,
user: userId,
id: ObjectId().str,
};

// write the resource to the database
const appInstanceElement = db
.get('spaces')
.find({ id: spaceId })
.get('phases')
.find({ id: subSpaceId })
.get('items')
.filter(item => item.appInstance)
.map(item => item.appInstance)
.find({ id: appInstanceId });

// add the actions key if it does not exist
const hasActions = appInstanceElement.has('actions').value();
if (!hasActions) {
appInstanceElement.set('actions', [actionToWrite]).write();
} else {
appInstanceElement
.get('actions')
.push(actionToWrite)
.write();
}

// send back the resource
mainWindow.webContents.send(POST_ACTION_CHANNEL, actionToWrite);
} catch (e) {
console.error(e);
mainWindow.webContents.send(POST_ACTION_CHANNEL, null);
}
};

module.exports = postAction;
5 changes: 5 additions & 0 deletions public/electron.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const {
SYNC_SPACE_CHANNEL,
CLEAR_USER_INPUT_CHANNEL,
SHOW_CLEAR_USER_INPUT_PROMPT_CHANNEL,
POST_ACTION_CHANNEL,
} = require('./app/config/channels');
const env = require('./env.json');
const {
Expand All @@ -72,6 +73,7 @@ const {
setDeveloperMode,
clearUserInput,
showClearUserInputPrompt,
postAction,
} = require('./app/listeners');
const isMac = require('./app/utils/isMac');

Expand Down Expand Up @@ -368,6 +370,9 @@ app.on('ready', async () => {
setGeolocationEnabled(mainWindow, db)
);

// called when creating an action
ipcMain.on(POST_ACTION_CHANNEL, postAction(mainWindow, db));

// called when getting AppInstanceResources
ipcMain.on(GET_APP_INSTANCE_RESOURCES_CHANNEL, (event, data = {}) => {
const defaultResponse = [];
Expand Down
41 changes: 41 additions & 0 deletions src/actions/action.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { POST_ACTION_SUCCEEDED } from '../types';
import { POST_ACTION_CHANNEL } from '../config/channels';

const postAction = async (ddd = {}, callback) => () => {
const {
userId,
id: appInstanceId,
spaceId,
subSpaceId,
format,
data,
verb,
visibility,
} = ddd;
try {
window.ipcRenderer.send(POST_ACTION_CHANNEL, {
userId,
appInstanceId,
spaceId,
subSpaceId,
format,
data,
verb,
visibility,
});

window.ipcRenderer.once(POST_ACTION_CHANNEL, async (event, response) => {
callback({
// have to include the appInstanceId to avoid broadcasting
appInstanceId,
type: POST_ACTION_SUCCEEDED,
payload: response,
});
});
} catch (err) {
console.error(err);
}
};

// eslint-disable-next-line import/prefer-default-export
export { postAction };
1 change: 1 addition & 0 deletions src/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from './appInstanceResource';
export * from './appInstance';
export * from './developer';
export * from './layout';
export * from './action';
12 changes: 11 additions & 1 deletion src/components/phase/PhaseApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ import {
GET_APP_INSTANCE,
POST_APP_INSTANCE_RESOURCE,
APP_INSTANCE_RESOURCE_TYPES,
POST_ACTION,
} from '../../types';
import {
getAppInstanceResources,
patchAppInstanceResource,
postAppInstanceResource,
getAppInstance,
postAction,
} from '../../actions';
import {
DEFAULT_LANGUAGE,
Expand Down Expand Up @@ -45,6 +47,7 @@ class PhaseApp extends Component {
name: PropTypes.string,
folder: PropTypes.string.isRequired,
dispatchGetAppInstance: PropTypes.func.isRequired,
dispatchPostAction: PropTypes.func.isRequired,
id: PropTypes.string.isRequired,
phaseId: PropTypes.string.isRequired,
spaceId: PropTypes.string.isRequired,
Expand Down Expand Up @@ -101,7 +104,11 @@ class PhaseApp extends Component {

handleReceiveMessage = event => {
try {
const { dispatchGetAppInstance, appInstance } = this.props;
const {
dispatchGetAppInstance,
appInstance,
dispatchPostAction,
} = this.props;

// get app instance id in message
const { id: componentAppInstanceId } = appInstance || {};
Expand All @@ -122,6 +129,8 @@ class PhaseApp extends Component {
return patchAppInstanceResource(payload, this.postMessage);
case GET_APP_INSTANCE:
return dispatchGetAppInstance(payload, this.postMessage);
case POST_ACTION:
return dispatchPostAction(payload, this.postMessage);
default:
return false;
}
Expand Down Expand Up @@ -280,6 +289,7 @@ const mapStateToProps = ({ User, Space }) => ({

const mapDispatchToProps = {
dispatchGetAppInstance: getAppInstance,
dispatchPostAction: postAction,
};

const ConnectedComponent = connect(
Expand Down
1 change: 1 addition & 0 deletions src/config/channels.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@ module.exports = {
CLEARED_USER_INPUT_CHANNEL: 'space:cleared',
SHOW_CLEAR_USER_INPUT_PROMPT_CHANNEL: 'prompt:space:clear:show',
RESPOND_CLEAR_USER_INPUT_PROMPT_CHANNEL: 'prompt:space:clear:respond',
POST_ACTION_CHANNEL: 'action:post',
};
2 changes: 2 additions & 0 deletions src/types/action.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const POST_ACTION = 'POST_ACTION';
export const POST_ACTION_SUCCEEDED = 'POST_ACTION_SUCCEEDED';
1 change: 1 addition & 0 deletions src/types/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from './appInstance';
export * from './appInstanceResource';
export * from './developer';
export * from './layout';
export * from './action';

0 comments on commit 47b4fc0

Please sign in to comment.