Skip to content

Commit 47b4fc0

Browse files
committed
feat: retrieve post actions and save it in appInstance
1 parent e98573f commit 47b4fc0

File tree

10 files changed

+128
-1
lines changed

10 files changed

+128
-1
lines changed

public/app/config/channels.js

+1
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,5 @@ module.exports = {
4141
CLEARED_USER_INPUT_CHANNEL: 'space:cleared',
4242
SHOW_CLEAR_USER_INPUT_PROMPT_CHANNEL: 'prompt:space:clear:show',
4343
RESPOND_CLEAR_USER_INPUT_PROMPT_CHANNEL: 'prompt:space:clear:respond',
44+
POST_ACTION_CHANNEL: 'action:post',
4445
};

public/app/listeners/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const getDeveloperMode = require('./getDeveloperMode');
1818
const setDeveloperMode = require('./setDeveloperMode');
1919
const clearUserInput = require('./clearUserInput');
2020
const showClearUserInputPrompt = require('./showClearUserInputPrompt');
21+
const postAction = require('./postAction');
2122

2223
module.exports = {
2324
loadSpace,
@@ -40,4 +41,5 @@ module.exports = {
4041
setDeveloperMode,
4142
clearUserInput,
4243
showClearUserInputPrompt,
44+
postAction,
4345
};

public/app/listeners/postAction.js

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
const ObjectId = require('bson-objectid');
2+
const { POST_ACTION_CHANNEL } = require('../config/channels');
3+
4+
const postAction = (mainWindow, db) => (event, payload = {}) => {
5+
try {
6+
const {
7+
userId,
8+
appInstanceId,
9+
spaceId,
10+
subSpaceId,
11+
format,
12+
verb,
13+
data,
14+
visibility = 'private',
15+
} = payload;
16+
17+
// prepare the timestamp
18+
const now = new Date();
19+
20+
// prepare the resource that we will create
21+
const actionToWrite = {
22+
appInstance: appInstanceId,
23+
createdAt: now,
24+
updatedAt: now,
25+
data,
26+
format,
27+
verb,
28+
visibility,
29+
user: userId,
30+
id: ObjectId().str,
31+
};
32+
33+
// write the resource to the database
34+
const appInstanceElement = db
35+
.get('spaces')
36+
.find({ id: spaceId })
37+
.get('phases')
38+
.find({ id: subSpaceId })
39+
.get('items')
40+
.filter(item => item.appInstance)
41+
.map(item => item.appInstance)
42+
.find({ id: appInstanceId });
43+
44+
// add the actions key if it does not exist
45+
const hasActions = appInstanceElement.has('actions').value();
46+
if (!hasActions) {
47+
appInstanceElement.set('actions', [actionToWrite]).write();
48+
} else {
49+
appInstanceElement
50+
.get('actions')
51+
.push(actionToWrite)
52+
.write();
53+
}
54+
55+
// send back the resource
56+
mainWindow.webContents.send(POST_ACTION_CHANNEL, actionToWrite);
57+
} catch (e) {
58+
console.error(e);
59+
mainWindow.webContents.send(POST_ACTION_CHANNEL, null);
60+
}
61+
};
62+
63+
module.exports = postAction;

public/electron.js

+5
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ const {
4949
SYNC_SPACE_CHANNEL,
5050
CLEAR_USER_INPUT_CHANNEL,
5151
SHOW_CLEAR_USER_INPUT_PROMPT_CHANNEL,
52+
POST_ACTION_CHANNEL,
5253
} = require('./app/config/channels');
5354
const env = require('./env.json');
5455
const {
@@ -72,6 +73,7 @@ const {
7273
setDeveloperMode,
7374
clearUserInput,
7475
showClearUserInputPrompt,
76+
postAction,
7577
} = require('./app/listeners');
7678
const isMac = require('./app/utils/isMac');
7779

@@ -368,6 +370,9 @@ app.on('ready', async () => {
368370
setGeolocationEnabled(mainWindow, db)
369371
);
370372

373+
// called when creating an action
374+
ipcMain.on(POST_ACTION_CHANNEL, postAction(mainWindow, db));
375+
371376
// called when getting AppInstanceResources
372377
ipcMain.on(GET_APP_INSTANCE_RESOURCES_CHANNEL, (event, data = {}) => {
373378
const defaultResponse = [];

src/actions/action.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { POST_ACTION_SUCCEEDED } from '../types';
2+
import { POST_ACTION_CHANNEL } from '../config/channels';
3+
4+
const postAction = async (ddd = {}, callback) => () => {
5+
const {
6+
userId,
7+
id: appInstanceId,
8+
spaceId,
9+
subSpaceId,
10+
format,
11+
data,
12+
verb,
13+
visibility,
14+
} = ddd;
15+
try {
16+
window.ipcRenderer.send(POST_ACTION_CHANNEL, {
17+
userId,
18+
appInstanceId,
19+
spaceId,
20+
subSpaceId,
21+
format,
22+
data,
23+
verb,
24+
visibility,
25+
});
26+
27+
window.ipcRenderer.once(POST_ACTION_CHANNEL, async (event, response) => {
28+
callback({
29+
// have to include the appInstanceId to avoid broadcasting
30+
appInstanceId,
31+
type: POST_ACTION_SUCCEEDED,
32+
payload: response,
33+
});
34+
});
35+
} catch (err) {
36+
console.error(err);
37+
}
38+
};
39+
40+
// eslint-disable-next-line import/prefer-default-export
41+
export { postAction };

src/actions/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ export * from './appInstanceResource';
55
export * from './appInstance';
66
export * from './developer';
77
export * from './layout';
8+
export * from './action';

src/components/phase/PhaseApp.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ import {
1212
GET_APP_INSTANCE,
1313
POST_APP_INSTANCE_RESOURCE,
1414
APP_INSTANCE_RESOURCE_TYPES,
15+
POST_ACTION,
1516
} from '../../types';
1617
import {
1718
getAppInstanceResources,
1819
patchAppInstanceResource,
1920
postAppInstanceResource,
2021
getAppInstance,
22+
postAction,
2123
} from '../../actions';
2224
import {
2325
DEFAULT_LANGUAGE,
@@ -45,6 +47,7 @@ class PhaseApp extends Component {
4547
name: PropTypes.string,
4648
folder: PropTypes.string.isRequired,
4749
dispatchGetAppInstance: PropTypes.func.isRequired,
50+
dispatchPostAction: PropTypes.func.isRequired,
4851
id: PropTypes.string.isRequired,
4952
phaseId: PropTypes.string.isRequired,
5053
spaceId: PropTypes.string.isRequired,
@@ -101,7 +104,11 @@ class PhaseApp extends Component {
101104

102105
handleReceiveMessage = event => {
103106
try {
104-
const { dispatchGetAppInstance, appInstance } = this.props;
107+
const {
108+
dispatchGetAppInstance,
109+
appInstance,
110+
dispatchPostAction,
111+
} = this.props;
105112

106113
// get app instance id in message
107114
const { id: componentAppInstanceId } = appInstance || {};
@@ -122,6 +129,8 @@ class PhaseApp extends Component {
122129
return patchAppInstanceResource(payload, this.postMessage);
123130
case GET_APP_INSTANCE:
124131
return dispatchGetAppInstance(payload, this.postMessage);
132+
case POST_ACTION:
133+
return dispatchPostAction(payload, this.postMessage);
125134
default:
126135
return false;
127136
}
@@ -280,6 +289,7 @@ const mapStateToProps = ({ User, Space }) => ({
280289

281290
const mapDispatchToProps = {
282291
dispatchGetAppInstance: getAppInstance,
292+
dispatchPostAction: postAction,
283293
};
284294

285295
const ConnectedComponent = connect(

src/config/channels.js

+1
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,5 @@ module.exports = {
4141
CLEARED_USER_INPUT_CHANNEL: 'space:cleared',
4242
SHOW_CLEAR_USER_INPUT_PROMPT_CHANNEL: 'prompt:space:clear:show',
4343
RESPOND_CLEAR_USER_INPUT_PROMPT_CHANNEL: 'prompt:space:clear:respond',
44+
POST_ACTION_CHANNEL: 'action:post',
4445
};

src/types/action.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const POST_ACTION = 'POST_ACTION';
2+
export const POST_ACTION_SUCCEEDED = 'POST_ACTION_SUCCEEDED';

src/types/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ export * from './appInstance';
55
export * from './appInstanceResource';
66
export * from './developer';
77
export * from './layout';
8+
export * from './action';

0 commit comments

Comments
 (0)