Skip to content

Commit

Permalink
feat: add selection when loading a space, multiple refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
pyphilia committed May 12, 2020
1 parent 335a3a9 commit 97206b4
Show file tree
Hide file tree
Showing 20 changed files with 691 additions and 164 deletions.
4 changes: 3 additions & 1 deletion public/app/config/channels.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ module.exports = {
GET_SPACES_CHANNEL: 'spaces:get',
DELETE_SPACE_CHANNEL: 'space:delete',
DELETED_SPACE_CHANNEL: 'space:deleted',
CANCEL_LOAD_SPACE_CHANNEL: 'space:load:cancel',
EXTRACT_FILE_TO_LOAD_SPACE_CHANNEL: 'space:load:extract-file',
LOAD_SPACE_CHANNEL: 'space:load',
LOADED_SPACE_CHANNEL: 'space:loaded',
EXPORT_SPACE_CHANNEL: 'space:export',
EXPORTED_SPACE_CHANNEL: 'space:exported',
SHOW_LOAD_SPACE_PROMPT_CHANNEL: 'prompt:space:load:show',
SHOW_EXPORT_SPACE_PROMPT_CHANNEL: 'prompt:space:export:show',
RESPOND_LOAD_SPACE_PROMPT_CHANNEL: 'prompt:space:load:response',
RESPOND_EXPORT_SPACE_PROMPT_CHANNEL: 'prompt:space:export:respond',
RESPOND_LOAD_SPACE_PROMPT_CHANNEL: 'prompt:space:load:respond',
SHOW_DELETE_SPACE_PROMPT_CHANNEL: 'prompt:space:delete:show',
RESPOND_DELETE_SPACE_PROMPT_CHANNEL: 'prompt:space:delete:respond',
GET_USER_FOLDER_CHANNEL: 'user:folder:get',
Expand Down
8 changes: 7 additions & 1 deletion public/app/listeners/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
const loadSpace = require('./loadSpace');
const {
loadSpace,
cancelLoadSpace,
extractFileToLoadSpace,
} = require('./loadSpace');
const saveSpace = require('./saveSpace');
const getSpace = require('./getSpace');
const getSpaces = require('./getSpaces');
Expand Down Expand Up @@ -34,6 +38,8 @@ const setSpaceAsRecent = require('./setSpaceAsRecent');

module.exports = {
loadSpace,
cancelLoadSpace,
extractFileToLoadSpace,
saveSpace,
getSpace,
getSpaces,
Expand Down
144 changes: 99 additions & 45 deletions public/app/listeners/loadSpace.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
const extract = require('extract-zip');
const _ = require('lodash');
const { promisify } = require('util');
const fs = require('fs');
const ObjectId = require('bson-objectid');
const { VAR_FOLDER } = require('../config/config');
const { LOADED_SPACE_CHANNEL } = require('../config/channels');
const {
ERROR_SPACE_ALREADY_AVAILABLE,
ERROR_GENERAL,
ERROR_ZIP_CORRUPTED,
} = require('../config/errors');
LOADED_SPACE_CHANNEL,
CANCEL_LOAD_SPACE_CHANNEL,
EXTRACT_FILE_TO_LOAD_SPACE_CHANNEL,
} = require('../config/channels');
const { ERROR_GENERAL, ERROR_ZIP_CORRUPTED } = require('../config/errors');
const logger = require('../logger');
const {
performFileSystemOperation,
Expand All @@ -24,7 +25,10 @@ const {
// use promisified fs
const fsPromises = fs.promises;

const loadSpace = (mainWindow, db) => async (event, { fileLocation }) => {
const extractFileToLoadSpace = (mainWindow, db) => async (
event,
{ fileLocation }
) => {
const tmpId = ObjectId().str;

// make temporary folder hidden
Expand All @@ -37,7 +41,10 @@ const loadSpace = (mainWindow, db) => async (event, { fileLocation }) => {
// abort if there is no manifest
const hasManifest = await isFileAvailable(manifestPath);
if (!hasManifest) {
mainWindow.webContents.send(LOADED_SPACE_CHANNEL, ERROR_ZIP_CORRUPTED);
mainWindow.webContents.send(
EXTRACT_FILE_TO_LOAD_SPACE_CHANNEL,
ERROR_ZIP_CORRUPTED
);
return clean(extractPath);
}
const manifestString = await fsPromises.readFile(manifestPath);
Expand All @@ -47,62 +54,109 @@ const loadSpace = (mainWindow, db) => async (event, { fileLocation }) => {

// get handle to spaces collection
const spaces = db.get(SPACES_COLLECTION);
const existingSpace = spaces.find({ id }).value();

// abort if there is already a space with that id
if (existingSpace) {
mainWindow.webContents.send(
LOADED_SPACE_CHANNEL,
ERROR_SPACE_ALREADY_AVAILABLE
);
return clean(extractPath);
}

// abort if there is no space
const hasSpace = await isFileAvailable(spacePath);
if (!hasSpace) {
mainWindow.webContents.send(LOADED_SPACE_CHANNEL, ERROR_ZIP_CORRUPTED);
return clean(extractPath);
}
const savedSpace = spaces.find({ id }).value();

const spaceString = await fsPromises.readFile(spacePath);
const {
space,
space = {},
appInstanceResources: resources = [],
actions = [],
} = JSON.parse(spaceString);
const finalPath = `${VAR_FOLDER}/${id}`;
const elements = { space, resources, actions };

// we need to wrap this operation to avoid errors in windows
performFileSystemOperation(fs.renameSync)(extractPath, finalPath);
return mainWindow.webContents.send(EXTRACT_FILE_TO_LOAD_SPACE_CHANNEL, {
extractPath,
savedSpace,
elements,
});
} catch (err) {
logger.error(err);
mainWindow.webContents.send(
EXTRACT_FILE_TO_LOAD_SPACE_CHANNEL,
ERROR_GENERAL
);
return clean(extractPath);
}
};

const wasRenamed = await isFileAvailable(finalPath);
const cancelLoadSpace = mainWindow => async (event, { extractPath }) => {
const isCleanSuccessful = clean(extractPath);
mainWindow.webContents.send(CANCEL_LOAD_SPACE_CHANNEL, isCleanSuccessful);
return isCleanSuccessful;
};

if (!wasRenamed) {
logger.error('unable to rename extract path');
mainWindow.webContents.send(LOADED_SPACE_CHANNEL, ERROR_GENERAL);
return clean(extractPath);
const loadSpace = (mainWindow, db) => async (
event,
{
extractPath,
elements: { space, actions, resources },
selection: {
space: isSpaceSelected,
resources: isResourcesSelected,
actions: isActionsSelected,
},
}
) => {
try {
// space must be always defined
if (_.isEmpty(space)) {
logger.debug('try to load undefined space');
return mainWindow.webContents.send(LOADED_SPACE_CHANNEL, ERROR_GENERAL);
}

// write space to database
spaces.push(space).write();
// write space to database if selected
if (isSpaceSelected) {
const { id } = space;
const finalPath = `${VAR_FOLDER}/${id}`;

// we need to wrap this operation to avoid errors in windows
performFileSystemOperation(fs.renameSync)(extractPath, finalPath);

// write resources to database
db.get(APP_INSTANCE_RESOURCES_COLLECTION)
.push(...resources)
.write();
const wasRenamed = await isFileAvailable(finalPath);

// write actions to database
db.get(ACTIONS_COLLECTION)
.push(...actions)
.write();
if (!wasRenamed) {
logger.error('unable to rename extract path');
mainWindow.webContents.send(LOADED_SPACE_CHANNEL, ERROR_GENERAL);
return clean(extractPath);
}

db.get(SPACES_COLLECTION)
.push(space)
.write();
} else {
clean(extractPath);
}

// write resources to database if selected
if (isResourcesSelected) {
if (_.isEmpty(resources)) {
logger.debug('try to load empty resources');
return mainWindow.webContents.send(LOADED_SPACE_CHANNEL, ERROR_GENERAL);
}
db.get(APP_INSTANCE_RESOURCES_COLLECTION)
.push(...resources)
.write();
}

// write actions to database if selected
if (isActionsSelected) {
if (_.isEmpty(actions)) {
logger.debug('try to load empty actions');
return mainWindow.webContents.send(LOADED_SPACE_CHANNEL, ERROR_GENERAL);
}
db.get(ACTIONS_COLLECTION)
.push(...actions)
.write();
}

return mainWindow.webContents.send(LOADED_SPACE_CHANNEL, { spaceId: id });
return mainWindow.webContents.send(LOADED_SPACE_CHANNEL, {
spaceId: space.id,
});
} catch (err) {
logger.error(err);
mainWindow.webContents.send(LOADED_SPACE_CHANNEL, ERROR_GENERAL);
return clean(extractPath);
}
};

module.exports = loadSpace;
module.exports = { cancelLoadSpace, extractFileToLoadSpace, loadSpace };
19 changes: 16 additions & 3 deletions public/electron.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ const {
SET_USER_MODE_CHANNEL,
SET_SPACE_AS_FAVORITE_CHANNEL,
SET_SPACE_AS_RECENT_CHANNEL,
EXTRACT_FILE_TO_LOAD_SPACE_CHANNEL,
CANCEL_LOAD_SPACE_CHANNEL,
} = require('./app/config/channels');
const env = require('./env.json');
const {
Expand Down Expand Up @@ -94,6 +96,8 @@ const {
getUserMode,
setSpaceAsFavorite,
setSpaceAsRecent,
cancelLoadSpace,
extractFileToLoadSpace,
} = require('./app/listeners');
const isMac = require('./app/utils/isMac');

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

// prompt when loading a space
ipcMain.on(SHOW_LOAD_SPACE_PROMPT_CHANNEL, showLoadSpacePrompt(mainWindow));

// called when loading a space
ipcMain.on(LOAD_SPACE_CHANNEL, loadSpace(mainWindow, db));

// called when requesting to load a space
ipcMain.on(
EXTRACT_FILE_TO_LOAD_SPACE_CHANNEL,
extractFileToLoadSpace(mainWindow, db)
);

// called when canceling to load a space
ipcMain.on(CANCEL_LOAD_SPACE_CHANNEL, cancelLoadSpace(mainWindow));

// called when exporting a space
ipcMain.on(EXPORT_SPACE_CHANNEL, exportSpace(mainWindow, db));

// prompt when loading a space
ipcMain.on(SHOW_LOAD_SPACE_PROMPT_CHANNEL, showLoadSpacePrompt(mainWindow));

// prompt when exporting a space
ipcMain.on(
SHOW_EXPORT_SPACE_PROMPT_CHANNEL,
Expand Down
7 changes: 7 additions & 0 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import LoadSpace from './components/LoadSpace';
import SpaceScreen from './components/space/SpaceScreen';
import SyncScreen from './components/space/SyncScreen';
import ExportSelectionScreen from './components/space/export/ExportSelectionScreen';
import LoadSelectionScreen from './components/space/load/LoadSelectionScreen';
import DeveloperScreen from './components/developer/DeveloperScreen';
import { OnlineTheme, OfflineTheme } from './themes';
import Dashboard from './components/dashboard/Dashboard';
Expand All @@ -35,6 +36,7 @@ import {
SIGN_IN_PATH,
SAVED_SPACES_PATH,
buildExportSelectionPathForSpaceId,
LOAD_SELECTION_SPACE_PATH,
} from './config/paths';
import {
getGeolocation,
Expand Down Expand Up @@ -185,6 +187,11 @@ export class App extends Component {
path={LOAD_SPACE_PATH}
component={Authorization()(LoadSpace)}
/>
<Route
exact
path={LOAD_SELECTION_SPACE_PATH}
component={Authorization()(LoadSelectionScreen)}
/>
<Route
exact
path={SETTINGS_PATH}
Expand Down
1 change: 1 addition & 0 deletions src/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export * from './layout';
export * from './action';
export * from './authentication';
export * from './syncSpace';
export * from './loadSpace';
Loading

0 comments on commit 97206b4

Please sign in to comment.