Skip to content

Commit 6fcef41

Browse files
committed
feat: allow load data in classroom without user if load only space
1 parent 2c9cdcd commit 6fcef41

File tree

2 files changed

+59
-29
lines changed

2 files changed

+59
-29
lines changed

public/app/listeners/loadSpaceInClassroom.js

+31-9
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,35 @@ const loadSpaceInClassroom = (mainWindow, db) => async (
3838
try {
3939
const classroom = db.get(CLASSROOMS_COLLECTION).find({ id: classroomId });
4040

41-
// add user if doesn't exist
42-
let user = classroom
43-
.get(USERS_COLLECTION)
44-
.find({ username })
45-
.value();
46-
if (!user) {
47-
user = addUserInClassroomDatabase(db, { username, id: classroomId });
41+
// username should be defined if add resources or actions
42+
if (isResourcesSelected || isActionsSelected) {
43+
if (!username) {
44+
logger.debug('username not specified');
45+
return mainWindow.webContents.send(
46+
LOAD_SPACE_IN_CLASSROOM_CHANNEL,
47+
ERROR_GENERAL
48+
);
49+
}
50+
}
51+
52+
// add user
53+
let user = null;
54+
if (username) {
55+
user = classroom
56+
.get(USERS_COLLECTION)
57+
.find({ username })
58+
.value();
59+
if (!user) {
60+
try {
61+
user = addUserInClassroomDatabase(db, { username, id: classroomId });
62+
} catch (err) {
63+
logger.debug(err);
64+
return mainWindow.webContents.send(
65+
LOAD_SPACE_IN_CLASSROOM_CHANNEL,
66+
err
67+
);
68+
}
69+
}
4870
}
4971

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

104-
const { id: userId } = user;
105-
106126
// write resources to database if selected
107127
if (isResourcesSelected) {
108128
if (_.isEmpty(appInstanceResources)) {
@@ -113,6 +133,7 @@ const loadSpaceInClassroom = (mainWindow, db) => async (
113133
);
114134
}
115135

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

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

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

143165
// remove previous corresponding actions

src/components/classrooms/ImportDataAssignUserForm.js

+28-20
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React, { Component } from 'react';
2+
import { toastr } from 'react-redux-toastr';
23
import { connect } from 'react-redux';
34
import { withStyles } from '@material-ui/core';
45
import { withTranslation } from 'react-i18next';
@@ -13,6 +14,7 @@ import StudentForm from './StudentForm';
1314
import LoadSpaceSelectors from '../space/load/LoadSpaceSelectors';
1415
import { isSpaceDifferent as isSpaceDifferentFunc } from '../../utils/syncSpace';
1516
import { IMPORT_DATA_CLASSROOM_VALIDATE_BUTTON_ID } from '../../config/selectors';
17+
import { ERROR_MESSAGE_HEADER } from '../../config/messages';
1618

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

112-
// disable submit if no username is specified and if nothing is checked
114+
// disable submit if (no username is specified and there are resources/actions) and if nothing is checked
115+
const importResourcesOrActions = actions || appInstanceResources;
113116
disabled =
114117
!selection.includes(true) ||
115-
!usernameOption ||
116-
!usernameOption.value.length;
118+
(importResourcesOrActions &&
119+
(!usernameOption || !usernameOption.value.length));
117120
}
118121

119122
return (
@@ -137,24 +140,29 @@ class ImportDataAssignUserForm extends Component {
137140
classroom,
138141
dispatchLoadSpaceInClassroom,
139142
} = this.props;
140-
const {
141-
space,
142-
appInstanceResources,
143-
actions,
144-
usernameOption: { value: username },
145-
} = this.state;
143+
const { space, appInstanceResources, actions, usernameOption } = this.state;
146144

147-
dispatchLoadSpaceInClassroom({
148-
extractPath,
149-
classroomId: classroom.get('id'),
150-
elements: elements.toJS(),
151-
username,
152-
selection: {
153-
space,
154-
appInstanceResources,
155-
actions,
156-
},
157-
});
145+
let username = null;
146+
if (usernameOption) {
147+
({ value: username } = usernameOption);
148+
}
149+
150+
// username cannot be null if actions or resources are imported
151+
if (!username && (appInstanceResources || actions)) {
152+
toastr.error(ERROR_MESSAGE_HEADER, 'A user need to be specified');
153+
} else {
154+
dispatchLoadSpaceInClassroom({
155+
extractPath,
156+
classroomId: classroom.get('id'),
157+
elements: elements.toJS(),
158+
username,
159+
selection: {
160+
space,
161+
appInstanceResources,
162+
actions,
163+
},
164+
});
165+
}
158166
};
159167

160168
renderBackButton = () => {

0 commit comments

Comments
 (0)