Skip to content

Commit

Permalink
feat: allow load data in classroom without user if load only space
Browse files Browse the repository at this point in the history
  • Loading branch information
pyphilia committed Jun 3, 2020
1 parent 2c9cdcd commit 6fcef41
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 29 deletions.
40 changes: 31 additions & 9 deletions public/app/listeners/loadSpaceInClassroom.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,35 @@ const loadSpaceInClassroom = (mainWindow, db) => async (
try {
const classroom = db.get(CLASSROOMS_COLLECTION).find({ id: classroomId });

// add user if doesn't exist
let user = classroom
.get(USERS_COLLECTION)
.find({ username })
.value();
if (!user) {
user = addUserInClassroomDatabase(db, { username, id: classroomId });
// username should be defined if add resources or actions
if (isResourcesSelected || isActionsSelected) {
if (!username) {
logger.debug('username not specified');
return mainWindow.webContents.send(
LOAD_SPACE_IN_CLASSROOM_CHANNEL,
ERROR_GENERAL
);
}
}

// add user
let user = null;
if (username) {
user = classroom
.get(USERS_COLLECTION)
.find({ username })
.value();
if (!user) {
try {
user = addUserInClassroomDatabase(db, { username, id: classroomId });
} catch (err) {
logger.debug(err);
return mainWindow.webContents.send(
LOAD_SPACE_IN_CLASSROOM_CHANNEL,
err
);
}
}
}

// todo: check teacher can write in classroom
Expand Down Expand Up @@ -101,8 +123,6 @@ const loadSpaceInClassroom = (mainWindow, db) => async (
clean(extractPath);
}

const { id: userId } = user;

// write resources to database if selected
if (isResourcesSelected) {
if (_.isEmpty(appInstanceResources)) {
Expand All @@ -113,6 +133,7 @@ const loadSpaceInClassroom = (mainWindow, db) => async (
);
}

const { id: userId } = user;
const savedResources = classroom.get(APP_INSTANCE_RESOURCES_COLLECTION);

// remove previous corresponding resources
Expand All @@ -138,6 +159,7 @@ const loadSpaceInClassroom = (mainWindow, db) => async (
);
}

const { id: userId } = user;
const savedActions = classroom.get(ACTIONS_COLLECTION);

// remove previous corresponding actions
Expand Down
48 changes: 28 additions & 20 deletions src/components/classrooms/ImportDataAssignUserForm.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { Component } from 'react';
import { toastr } from 'react-redux-toastr';
import { connect } from 'react-redux';
import { withStyles } from '@material-ui/core';
import { withTranslation } from 'react-i18next';
Expand All @@ -13,6 +14,7 @@ import StudentForm from './StudentForm';
import LoadSpaceSelectors from '../space/load/LoadSpaceSelectors';
import { isSpaceDifferent as isSpaceDifferentFunc } from '../../utils/syncSpace';
import { IMPORT_DATA_CLASSROOM_VALIDATE_BUTTON_ID } from '../../config/selectors';
import { ERROR_MESSAGE_HEADER } from '../../config/messages';

const styles = theme => ({
...Styles(theme),
Expand Down Expand Up @@ -109,11 +111,12 @@ class ImportDataAssignUserForm extends Component {
const { space, actions, appInstanceResources } = this.state;
const selection = [space, actions, appInstanceResources];

// disable submit if no username is specified and if nothing is checked
// disable submit if (no username is specified and there are resources/actions) and if nothing is checked
const importResourcesOrActions = actions || appInstanceResources;
disabled =
!selection.includes(true) ||
!usernameOption ||
!usernameOption.value.length;
(importResourcesOrActions &&
(!usernameOption || !usernameOption.value.length));
}

return (
Expand All @@ -137,24 +140,29 @@ class ImportDataAssignUserForm extends Component {
classroom,
dispatchLoadSpaceInClassroom,
} = this.props;
const {
space,
appInstanceResources,
actions,
usernameOption: { value: username },
} = this.state;
const { space, appInstanceResources, actions, usernameOption } = this.state;

dispatchLoadSpaceInClassroom({
extractPath,
classroomId: classroom.get('id'),
elements: elements.toJS(),
username,
selection: {
space,
appInstanceResources,
actions,
},
});
let username = null;
if (usernameOption) {
({ value: username } = usernameOption);
}

// username cannot be null if actions or resources are imported
if (!username && (appInstanceResources || actions)) {
toastr.error(ERROR_MESSAGE_HEADER, 'A user need to be specified');
} else {
dispatchLoadSpaceInClassroom({
extractPath,
classroomId: classroom.get('id'),
elements: elements.toJS(),
username,
selection: {
space,
appInstanceResources,
actions,
},
});
}
};

renderBackButton = () => {
Expand Down

0 comments on commit 6fcef41

Please sign in to comment.