Skip to content

Commit 97206b4

Browse files
committed
feat: add selection when loading a space, multiple refactor
1 parent 335a3a9 commit 97206b4

20 files changed

+691
-164
lines changed

public/app/config/channels.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,16 @@ module.exports = {
88
GET_SPACES_CHANNEL: 'spaces:get',
99
DELETE_SPACE_CHANNEL: 'space:delete',
1010
DELETED_SPACE_CHANNEL: 'space:deleted',
11+
CANCEL_LOAD_SPACE_CHANNEL: 'space:load:cancel',
12+
EXTRACT_FILE_TO_LOAD_SPACE_CHANNEL: 'space:load:extract-file',
1113
LOAD_SPACE_CHANNEL: 'space:load',
1214
LOADED_SPACE_CHANNEL: 'space:loaded',
1315
EXPORT_SPACE_CHANNEL: 'space:export',
1416
EXPORTED_SPACE_CHANNEL: 'space:exported',
1517
SHOW_LOAD_SPACE_PROMPT_CHANNEL: 'prompt:space:load:show',
1618
SHOW_EXPORT_SPACE_PROMPT_CHANNEL: 'prompt:space:export:show',
17-
RESPOND_LOAD_SPACE_PROMPT_CHANNEL: 'prompt:space:load:response',
1819
RESPOND_EXPORT_SPACE_PROMPT_CHANNEL: 'prompt:space:export:respond',
20+
RESPOND_LOAD_SPACE_PROMPT_CHANNEL: 'prompt:space:load:respond',
1921
SHOW_DELETE_SPACE_PROMPT_CHANNEL: 'prompt:space:delete:show',
2022
RESPOND_DELETE_SPACE_PROMPT_CHANNEL: 'prompt:space:delete:respond',
2123
GET_USER_FOLDER_CHANNEL: 'user:folder:get',

public/app/listeners/index.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
const loadSpace = require('./loadSpace');
1+
const {
2+
loadSpace,
3+
cancelLoadSpace,
4+
extractFileToLoadSpace,
5+
} = require('./loadSpace');
26
const saveSpace = require('./saveSpace');
37
const getSpace = require('./getSpace');
48
const getSpaces = require('./getSpaces');
@@ -34,6 +38,8 @@ const setSpaceAsRecent = require('./setSpaceAsRecent');
3438

3539
module.exports = {
3640
loadSpace,
41+
cancelLoadSpace,
42+
extractFileToLoadSpace,
3743
saveSpace,
3844
getSpace,
3945
getSpaces,

public/app/listeners/loadSpace.js

+99-45
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
const extract = require('extract-zip');
2+
const _ = require('lodash');
23
const { promisify } = require('util');
34
const fs = require('fs');
45
const ObjectId = require('bson-objectid');
56
const { VAR_FOLDER } = require('../config/config');
6-
const { LOADED_SPACE_CHANNEL } = require('../config/channels');
77
const {
8-
ERROR_SPACE_ALREADY_AVAILABLE,
9-
ERROR_GENERAL,
10-
ERROR_ZIP_CORRUPTED,
11-
} = require('../config/errors');
8+
LOADED_SPACE_CHANNEL,
9+
CANCEL_LOAD_SPACE_CHANNEL,
10+
EXTRACT_FILE_TO_LOAD_SPACE_CHANNEL,
11+
} = require('../config/channels');
12+
const { ERROR_GENERAL, ERROR_ZIP_CORRUPTED } = require('../config/errors');
1213
const logger = require('../logger');
1314
const {
1415
performFileSystemOperation,
@@ -24,7 +25,10 @@ const {
2425
// use promisified fs
2526
const fsPromises = fs.promises;
2627

27-
const loadSpace = (mainWindow, db) => async (event, { fileLocation }) => {
28+
const extractFileToLoadSpace = (mainWindow, db) => async (
29+
event,
30+
{ fileLocation }
31+
) => {
2832
const tmpId = ObjectId().str;
2933

3034
// make temporary folder hidden
@@ -37,7 +41,10 @@ const loadSpace = (mainWindow, db) => async (event, { fileLocation }) => {
3741
// abort if there is no manifest
3842
const hasManifest = await isFileAvailable(manifestPath);
3943
if (!hasManifest) {
40-
mainWindow.webContents.send(LOADED_SPACE_CHANNEL, ERROR_ZIP_CORRUPTED);
44+
mainWindow.webContents.send(
45+
EXTRACT_FILE_TO_LOAD_SPACE_CHANNEL,
46+
ERROR_ZIP_CORRUPTED
47+
);
4148
return clean(extractPath);
4249
}
4350
const manifestString = await fsPromises.readFile(manifestPath);
@@ -47,62 +54,109 @@ const loadSpace = (mainWindow, db) => async (event, { fileLocation }) => {
4754

4855
// get handle to spaces collection
4956
const spaces = db.get(SPACES_COLLECTION);
50-
const existingSpace = spaces.find({ id }).value();
51-
52-
// abort if there is already a space with that id
53-
if (existingSpace) {
54-
mainWindow.webContents.send(
55-
LOADED_SPACE_CHANNEL,
56-
ERROR_SPACE_ALREADY_AVAILABLE
57-
);
58-
return clean(extractPath);
59-
}
60-
61-
// abort if there is no space
62-
const hasSpace = await isFileAvailable(spacePath);
63-
if (!hasSpace) {
64-
mainWindow.webContents.send(LOADED_SPACE_CHANNEL, ERROR_ZIP_CORRUPTED);
65-
return clean(extractPath);
66-
}
57+
const savedSpace = spaces.find({ id }).value();
6758

6859
const spaceString = await fsPromises.readFile(spacePath);
6960
const {
70-
space,
61+
space = {},
7162
appInstanceResources: resources = [],
7263
actions = [],
7364
} = JSON.parse(spaceString);
74-
const finalPath = `${VAR_FOLDER}/${id}`;
65+
const elements = { space, resources, actions };
7566

76-
// we need to wrap this operation to avoid errors in windows
77-
performFileSystemOperation(fs.renameSync)(extractPath, finalPath);
67+
return mainWindow.webContents.send(EXTRACT_FILE_TO_LOAD_SPACE_CHANNEL, {
68+
extractPath,
69+
savedSpace,
70+
elements,
71+
});
72+
} catch (err) {
73+
logger.error(err);
74+
mainWindow.webContents.send(
75+
EXTRACT_FILE_TO_LOAD_SPACE_CHANNEL,
76+
ERROR_GENERAL
77+
);
78+
return clean(extractPath);
79+
}
80+
};
7881

79-
const wasRenamed = await isFileAvailable(finalPath);
82+
const cancelLoadSpace = mainWindow => async (event, { extractPath }) => {
83+
const isCleanSuccessful = clean(extractPath);
84+
mainWindow.webContents.send(CANCEL_LOAD_SPACE_CHANNEL, isCleanSuccessful);
85+
return isCleanSuccessful;
86+
};
8087

81-
if (!wasRenamed) {
82-
logger.error('unable to rename extract path');
83-
mainWindow.webContents.send(LOADED_SPACE_CHANNEL, ERROR_GENERAL);
84-
return clean(extractPath);
88+
const loadSpace = (mainWindow, db) => async (
89+
event,
90+
{
91+
extractPath,
92+
elements: { space, actions, resources },
93+
selection: {
94+
space: isSpaceSelected,
95+
resources: isResourcesSelected,
96+
actions: isActionsSelected,
97+
},
98+
}
99+
) => {
100+
try {
101+
// space must be always defined
102+
if (_.isEmpty(space)) {
103+
logger.debug('try to load undefined space');
104+
return mainWindow.webContents.send(LOADED_SPACE_CHANNEL, ERROR_GENERAL);
85105
}
86106

87-
// write space to database
88-
spaces.push(space).write();
107+
// write space to database if selected
108+
if (isSpaceSelected) {
109+
const { id } = space;
110+
const finalPath = `${VAR_FOLDER}/${id}`;
111+
112+
// we need to wrap this operation to avoid errors in windows
113+
performFileSystemOperation(fs.renameSync)(extractPath, finalPath);
89114

90-
// write resources to database
91-
db.get(APP_INSTANCE_RESOURCES_COLLECTION)
92-
.push(...resources)
93-
.write();
115+
const wasRenamed = await isFileAvailable(finalPath);
94116

95-
// write actions to database
96-
db.get(ACTIONS_COLLECTION)
97-
.push(...actions)
98-
.write();
117+
if (!wasRenamed) {
118+
logger.error('unable to rename extract path');
119+
mainWindow.webContents.send(LOADED_SPACE_CHANNEL, ERROR_GENERAL);
120+
return clean(extractPath);
121+
}
122+
123+
db.get(SPACES_COLLECTION)
124+
.push(space)
125+
.write();
126+
} else {
127+
clean(extractPath);
128+
}
129+
130+
// write resources to database if selected
131+
if (isResourcesSelected) {
132+
if (_.isEmpty(resources)) {
133+
logger.debug('try to load empty resources');
134+
return mainWindow.webContents.send(LOADED_SPACE_CHANNEL, ERROR_GENERAL);
135+
}
136+
db.get(APP_INSTANCE_RESOURCES_COLLECTION)
137+
.push(...resources)
138+
.write();
139+
}
140+
141+
// write actions to database if selected
142+
if (isActionsSelected) {
143+
if (_.isEmpty(actions)) {
144+
logger.debug('try to load empty actions');
145+
return mainWindow.webContents.send(LOADED_SPACE_CHANNEL, ERROR_GENERAL);
146+
}
147+
db.get(ACTIONS_COLLECTION)
148+
.push(...actions)
149+
.write();
150+
}
99151

100-
return mainWindow.webContents.send(LOADED_SPACE_CHANNEL, { spaceId: id });
152+
return mainWindow.webContents.send(LOADED_SPACE_CHANNEL, {
153+
spaceId: space.id,
154+
});
101155
} catch (err) {
102156
logger.error(err);
103157
mainWindow.webContents.send(LOADED_SPACE_CHANNEL, ERROR_GENERAL);
104158
return clean(extractPath);
105159
}
106160
};
107161

108-
module.exports = loadSpace;
162+
module.exports = { cancelLoadSpace, extractFileToLoadSpace, loadSpace };

public/electron.js

+16-3
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ const {
5858
SET_USER_MODE_CHANNEL,
5959
SET_SPACE_AS_FAVORITE_CHANNEL,
6060
SET_SPACE_AS_RECENT_CHANNEL,
61+
EXTRACT_FILE_TO_LOAD_SPACE_CHANNEL,
62+
CANCEL_LOAD_SPACE_CHANNEL,
6163
} = require('./app/config/channels');
6264
const env = require('./env.json');
6365
const {
@@ -94,6 +96,8 @@ const {
9496
getUserMode,
9597
setSpaceAsFavorite,
9698
setSpaceAsRecent,
99+
cancelLoadSpace,
100+
extractFileToLoadSpace,
97101
} = require('./app/listeners');
98102
const isMac = require('./app/utils/isMac');
99103

@@ -358,15 +362,24 @@ app.on('ready', async () => {
358362
// called when deleting a space
359363
ipcMain.on(DELETE_SPACE_CHANNEL, deleteSpace(mainWindow, db));
360364

365+
// prompt when loading a space
366+
ipcMain.on(SHOW_LOAD_SPACE_PROMPT_CHANNEL, showLoadSpacePrompt(mainWindow));
367+
361368
// called when loading a space
362369
ipcMain.on(LOAD_SPACE_CHANNEL, loadSpace(mainWindow, db));
363370

371+
// called when requesting to load a space
372+
ipcMain.on(
373+
EXTRACT_FILE_TO_LOAD_SPACE_CHANNEL,
374+
extractFileToLoadSpace(mainWindow, db)
375+
);
376+
377+
// called when canceling to load a space
378+
ipcMain.on(CANCEL_LOAD_SPACE_CHANNEL, cancelLoadSpace(mainWindow));
379+
364380
// called when exporting a space
365381
ipcMain.on(EXPORT_SPACE_CHANNEL, exportSpace(mainWindow, db));
366382

367-
// prompt when loading a space
368-
ipcMain.on(SHOW_LOAD_SPACE_PROMPT_CHANNEL, showLoadSpacePrompt(mainWindow));
369-
370383
// prompt when exporting a space
371384
ipcMain.on(
372385
SHOW_EXPORT_SPACE_PROMPT_CHANNEL,

src/App.js

+7
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import LoadSpace from './components/LoadSpace';
1717
import SpaceScreen from './components/space/SpaceScreen';
1818
import SyncScreen from './components/space/SyncScreen';
1919
import ExportSelectionScreen from './components/space/export/ExportSelectionScreen';
20+
import LoadSelectionScreen from './components/space/load/LoadSelectionScreen';
2021
import DeveloperScreen from './components/developer/DeveloperScreen';
2122
import { OnlineTheme, OfflineTheme } from './themes';
2223
import Dashboard from './components/dashboard/Dashboard';
@@ -35,6 +36,7 @@ import {
3536
SIGN_IN_PATH,
3637
SAVED_SPACES_PATH,
3738
buildExportSelectionPathForSpaceId,
39+
LOAD_SELECTION_SPACE_PATH,
3840
} from './config/paths';
3941
import {
4042
getGeolocation,
@@ -185,6 +187,11 @@ export class App extends Component {
185187
path={LOAD_SPACE_PATH}
186188
component={Authorization()(LoadSpace)}
187189
/>
190+
<Route
191+
exact
192+
path={LOAD_SELECTION_SPACE_PATH}
193+
component={Authorization()(LoadSelectionScreen)}
194+
/>
188195
<Route
189196
exact
190197
path={SETTINGS_PATH}

src/actions/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ export * from './layout';
88
export * from './action';
99
export * from './authentication';
1010
export * from './syncSpace';
11+
export * from './loadSpace';

0 commit comments

Comments
 (0)