Skip to content

Commit 2d5b4e4

Browse files
committed
feat: delete appInstanceResource and related file
1 parent c1b2d7e commit 2d5b4e4

File tree

12 files changed

+163
-5
lines changed

12 files changed

+163
-5
lines changed

public/app/config/channels.js

+1
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,5 @@ module.exports = {
7272
LOAD_SPACE_IN_CLASSROOM_CHANNEL: 'classroom:space:load',
7373
GET_SPACE_TO_LOAD_IN_CLASSROOM_CHANNEL: 'classroom:space:load:get-space',
7474
POST_FILE_CHANNEL: 'file:post',
75+
DELETE_FILE_CHANNEL: 'file:delete',
7576
};

public/app/config/messages.js

+2
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ const ERROR_SETTING_ACTION_ACCESSIBILITY =
103103
'There was an error setting the action accessibility';
104104
const ERROR_SETTING_ACTIONS_AS_ENABLED = 'There was an error enabling actions';
105105
const ERROR_POSTING_FILE_MESSAGE = 'There was an error uploading the file';
106+
const ERROR_DELETING_FILE_MESSAGE = 'There was an error deleting the file';
106107

107108
module.exports = {
108109
ERROR_GETTING_DEVELOPER_MODE,
@@ -174,4 +175,5 @@ module.exports = {
174175
ERROR_SETTING_ACTION_ACCESSIBILITY,
175176
ERROR_SETTING_ACTIONS_AS_ENABLED,
176177
ERROR_POSTING_FILE_MESSAGE,
178+
ERROR_DELETING_FILE_MESSAGE,
177179
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const { DELETE_APP_INSTANCE_RESOURCE_CHANNEL } = require('../config/channels');
2+
const { APP_INSTANCE_RESOURCES_COLLECTION } = require('../db');
3+
4+
const deleteAppInstanceResource = (mainWindow, db) => (event, payload = {}) => {
5+
try {
6+
const { appInstanceId: appInstance, id } = payload;
7+
8+
db.get(APP_INSTANCE_RESOURCES_COLLECTION)
9+
.remove({ appInstance, id })
10+
.write();
11+
12+
mainWindow.webContents.send(DELETE_APP_INSTANCE_RESOURCE_CHANNEL, payload);
13+
} catch (e) {
14+
console.error(e);
15+
mainWindow.webContents.send(DELETE_APP_INSTANCE_RESOURCE_CHANNEL, null);
16+
}
17+
};
18+
19+
module.exports = deleteAppInstanceResource;

public/app/listeners/deleteFile.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const fs = require('fs');
2+
const logger = require('../logger');
3+
const { DELETE_FILE_CHANNEL } = require('../config/channels');
4+
const { ERROR_GENERAL } = require('../config/errors');
5+
6+
const deleteFile = mainWindow => (event, payload = {}) => {
7+
try {
8+
const {
9+
data: { uri },
10+
} = payload;
11+
12+
if (uri) {
13+
const filepath = uri.substr('file://'.length);
14+
fs.unlinkSync(filepath);
15+
logger.debug(`${uri} was deleted`);
16+
} else {
17+
logger.error('no uri specified');
18+
}
19+
20+
mainWindow.webContents.send(DELETE_FILE_CHANNEL, payload);
21+
} catch (e) {
22+
console.error(e);
23+
mainWindow.webContents.send(DELETE_FILE_CHANNEL, ERROR_GENERAL);
24+
}
25+
};
26+
27+
module.exports = deleteFile;

public/app/listeners/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const signOut = require('./signOut');
3131
const isAuthenticated = require('./isAuthenticated');
3232
const getAppInstanceResources = require('./getAppInstanceResources');
3333
const postAppInstanceResource = require('./postAppInstanceResource');
34+
const deleteAppInstanceResource = require('./deleteAppInstanceResource');
3435
const patchAppInstanceResource = require('./patchAppInstanceResource');
3536
const getAppInstance = require('./getAppInstance');
3637
const setSpaceAsFavorite = require('./setSpaceAsFavorite');
@@ -51,6 +52,7 @@ const setActionAccessibility = require('./setActionAccessibility');
5152
const setActionsAsEnabled = require('./setActionsAsEnabled');
5253
const windowAllClosed = require('./windowAllClosed');
5354
const postFile = require('./postFile');
55+
const deleteFile = require('./deleteFile');
5456

5557
module.exports = {
5658
loadSpace,
@@ -83,6 +85,7 @@ module.exports = {
8385
isAuthenticated,
8486
getAppInstanceResources,
8587
postAppInstanceResource,
88+
deleteAppInstanceResource,
8689
patchAppInstanceResource,
8790
getAppInstance,
8891
getUserMode,
@@ -105,4 +108,5 @@ module.exports = {
105108
setActionsAsEnabled,
106109
windowAllClosed,
107110
postFile,
111+
deleteFile,
108112
};

public/electron.js

+13
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const {
3737
SET_LANGUAGE_CHANNEL,
3838
GET_APP_INSTANCE_RESOURCES_CHANNEL,
3939
POST_APP_INSTANCE_RESOURCE_CHANNEL,
40+
DELETE_APP_INSTANCE_RESOURCE_CHANNEL,
4041
PATCH_APP_INSTANCE_RESOURCE_CHANNEL,
4142
GET_APP_INSTANCE_CHANNEL,
4243
GET_DEVELOPER_MODE_CHANNEL,
@@ -75,6 +76,7 @@ const {
7576
SET_ACTION_ACCESSIBILITY_CHANNEL,
7677
SET_ACTIONS_AS_ENABLED_CHANNEL,
7778
POST_FILE_CHANNEL,
79+
DELETE_FILE_CHANNEL,
7880
} = require('./app/config/channels');
7981
const env = require('./env.json');
8082
const {
@@ -104,6 +106,7 @@ const {
104106
getAppInstanceResources,
105107
postAppInstanceResource,
106108
patchAppInstanceResource,
109+
deleteAppInstanceResource,
107110
getAppInstance,
108111
setSyncMode,
109112
getSyncMode,
@@ -129,6 +132,7 @@ const {
129132
setActionsAsEnabled,
130133
windowAllClosed,
131134
postFile,
135+
deleteFile,
132136
} = require('./app/listeners');
133137
const isMac = require('./app/utils/isMac');
134138

@@ -494,6 +498,9 @@ app.on('ready', async () => {
494498
// called when creating a file
495499
ipcMain.on(POST_FILE_CHANNEL, postFile(mainWindow, db));
496500

501+
// called when creating a file
502+
ipcMain.on(DELETE_FILE_CHANNEL, deleteFile(mainWindow, db));
503+
497504
// called when logging in a user
498505
ipcMain.on(SIGN_IN_CHANNEL, signIn(mainWindow, db));
499506

@@ -521,6 +528,12 @@ app.on('ready', async () => {
521528
patchAppInstanceResource(mainWindow, db)
522529
);
523530

531+
// called when deleting an AppInstanceResource
532+
ipcMain.on(
533+
DELETE_APP_INSTANCE_RESOURCE_CHANNEL,
534+
deleteAppInstanceResource(mainWindow, db)
535+
);
536+
524537
// called when getting an AppInstance
525538
ipcMain.on(GET_APP_INSTANCE_CHANNEL, getAppInstance(mainWindow, db));
526539

src/actions/appInstanceResource.js

+32
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ import {
22
GET_APP_INSTANCE_RESOURCES_CHANNEL,
33
PATCH_APP_INSTANCE_RESOURCE_CHANNEL,
44
POST_APP_INSTANCE_RESOURCE_CHANNEL,
5+
DELETE_APP_INSTANCE_RESOURCE_CHANNEL,
56
} from '../config/channels';
67
import {
78
GET_APP_INSTANCE_RESOURCES_SUCCEEDED,
89
PATCH_APP_INSTANCE_RESOURCE_SUCCEEDED,
910
POST_APP_INSTANCE_RESOURCE_SUCCEEDED,
11+
DELETE_APP_INSTANCE_RESOURCE_SUCCEEDED,
1012
} from '../types';
1113

1214
const getAppInstanceResources = async (
@@ -100,8 +102,38 @@ const patchAppInstanceResource = async (
100102
}
101103
};
102104

105+
const deleteAppInstanceResource = async (
106+
{ id, data, appInstanceId, spaceId, subSpaceId, type } = {},
107+
callback
108+
) => {
109+
try {
110+
window.ipcRenderer.send(DELETE_APP_INSTANCE_RESOURCE_CHANNEL, {
111+
id,
112+
data,
113+
appInstanceId,
114+
spaceId,
115+
subSpaceId,
116+
type,
117+
});
118+
119+
window.ipcRenderer.once(
120+
DELETE_APP_INSTANCE_RESOURCE_CHANNEL,
121+
async (event, response) => {
122+
callback({
123+
appInstanceId,
124+
type: DELETE_APP_INSTANCE_RESOURCE_SUCCEEDED,
125+
payload: response,
126+
});
127+
}
128+
);
129+
} catch (err) {
130+
console.error(err);
131+
}
132+
};
133+
103134
export {
104135
getAppInstanceResources,
105136
postAppInstanceResource,
106137
patchAppInstanceResource,
138+
deleteAppInstanceResource,
107139
};

src/actions/file.js

+36-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1-
import { POST_FILE_CHANNEL } from '../config/channels';
2-
import { POST_FILE_SUCCEEDED, POST_FILE_FAILED } from '../types';
1+
import { POST_FILE_CHANNEL, DELETE_FILE_CHANNEL } from '../config/channels';
2+
import {
3+
POST_FILE_SUCCEEDED,
4+
POST_FILE_FAILED,
5+
DELETE_FILE_FAILED,
6+
DELETE_FILE_SUCCEEDED,
7+
} from '../types';
38
import { ERROR_GENERAL } from '../config/errors';
4-
import { ERROR_POSTING_FILE_MESSAGE } from '../config/messages';
9+
import {
10+
ERROR_POSTING_FILE_MESSAGE,
11+
ERROR_DELETING_FILE_MESSAGE,
12+
} from '../config/messages';
513

6-
// eslint-disable-next-line import/prefer-default-export
714
export const postFile = async (
815
{ userId, appInstanceId, spaceId, subSpaceId, format, data, type } = {},
916
callback
@@ -43,3 +50,28 @@ export const postFile = async (
4350
});
4451
}
4552
};
53+
54+
export const deleteFile = async (payload = {}, callback) => {
55+
try {
56+
window.ipcRenderer.send(DELETE_FILE_CHANNEL, payload);
57+
const { appInstanceId } = payload;
58+
window.ipcRenderer.once(DELETE_FILE_CHANNEL, async (event, response) => {
59+
if (response === ERROR_GENERAL) {
60+
callback({
61+
appInstanceId,
62+
type: DELETE_FILE_FAILED,
63+
payload: ERROR_DELETING_FILE_MESSAGE,
64+
});
65+
} else {
66+
callback({
67+
// have to include the appInstanceId to avoid broadcasting
68+
appInstanceId,
69+
type: DELETE_FILE_SUCCEEDED,
70+
payload: response,
71+
});
72+
}
73+
});
74+
} catch (err) {
75+
console.error(err);
76+
}
77+
};

src/components/phase/PhaseApp.js

+15
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,23 @@ import {
1212
PATCH_APP_INSTANCE_RESOURCE,
1313
GET_APP_INSTANCE,
1414
POST_APP_INSTANCE_RESOURCE,
15+
DELETE_APP_INSTANCE_RESOURCE,
1516
APP_INSTANCE_RESOURCE_TYPES,
1617
POST_ACTION,
1718
ACTION_TYPES,
1819
POST_FILE,
1920
FILE_TYPES,
21+
DELETE_FILE,
2022
} from '../../types';
2123
import {
2224
getAppInstanceResources,
2325
patchAppInstanceResource,
2426
postAppInstanceResource,
27+
deleteAppInstanceResource,
2528
getAppInstance,
2629
postAction,
2730
postFile,
31+
deleteFile,
2832
} from '../../actions';
2933
import {
3034
DEFAULT_LANGUAGE,
@@ -156,6 +160,11 @@ class PhaseApp extends Component {
156160
return patchAppInstanceResource(payload, this.postMessage);
157161
}
158162
break;
163+
case DELETE_APP_INSTANCE_RESOURCE:
164+
if (isSpaceSaved) {
165+
return deleteAppInstanceResource(payload, this.postMessage);
166+
}
167+
break;
159168
case POST_ACTION: {
160169
if (isSpaceSaved) {
161170
return dispatchPostAction(payload, user, this.postMessage);
@@ -168,6 +177,12 @@ class PhaseApp extends Component {
168177
}
169178
break;
170179
}
180+
case DELETE_FILE: {
181+
if (isSpaceSaved) {
182+
return deleteFile(payload, this.postMessage);
183+
}
184+
break;
185+
}
171186
default:
172187
return false;
173188
}

src/config/channels.js

+1
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,5 @@ module.exports = {
7272
LOAD_SPACE_IN_CLASSROOM_CHANNEL: 'classroom:space:load',
7373
GET_SPACE_TO_LOAD_IN_CLASSROOM_CHANNEL: 'classroom:space:load:get-space',
7474
POST_FILE_CHANNEL: 'file:post',
75+
DELETE_FILE_CHANNEL: 'file:delete',
7576
};

src/config/messages.js

+2
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ const ERROR_SETTING_ACTION_ACCESSIBILITY =
103103
'There was an error setting the action accessibility';
104104
const ERROR_SETTING_ACTIONS_AS_ENABLED = 'There was an error enabling actions';
105105
const ERROR_POSTING_FILE_MESSAGE = 'There was an error uploading the file';
106+
const ERROR_DELETING_FILE_MESSAGE = 'There was an error deleting the file';
106107

107108
module.exports = {
108109
ERROR_GETTING_DEVELOPER_MODE,
@@ -174,4 +175,5 @@ module.exports = {
174175
ERROR_SETTING_ACTION_ACCESSIBILITY,
175176
ERROR_SETTING_ACTIONS_AS_ENABLED,
176177
ERROR_POSTING_FILE_MESSAGE,
178+
ERROR_DELETING_FILE_MESSAGE,
177179
};

src/types/file.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
export const POST_FILE = 'POST_FILE';
22
export const POST_FILE_SUCCEEDED = 'POST_FILE_SUCCEEDED';
33
export const POST_FILE_FAILED = 'POST_FILE_FAILED';
4+
export const DELETE_FILE_SUCCEEDED = 'DELETE_FILE_SUCCEEDED';
5+
export const DELETE_FILE_FAILED = 'DELETE_FILE_FAILED';
6+
export const DELETE_FILE = 'DELETE_FILE';
47

5-
export const FILE_TYPES = [POST_FILE, POST_FILE_SUCCEEDED, POST_FILE_FAILED];
8+
export const FILE_TYPES = [
9+
POST_FILE,
10+
POST_FILE_SUCCEEDED,
11+
POST_FILE_FAILED,
12+
DELETE_FILE_SUCCEEDED,
13+
DELETE_FILE_FAILED,
14+
DELETE_FILE,
15+
];

0 commit comments

Comments
 (0)