diff --git a/client/modules/IDE/actions/preferences.js b/client/modules/IDE/actions/preferences.js index 5f2e0a7e..ab8d2c5c 100644 --- a/client/modules/IDE/actions/preferences.js +++ b/client/modules/IDE/actions/preferences.js @@ -23,7 +23,7 @@ export function setFontSize(value) { if (state.user.authenticated) { const formParams = { preferences: { - textSize: value + fontSize: value } }; updatePreferences(formParams, dispatch); diff --git a/client/modules/IDE/pages/IDEView.js b/client/modules/IDE/pages/IDEView.js index c6a5bc47..a975b02e 100644 --- a/client/modules/IDE/pages/IDEView.js +++ b/client/modules/IDE/pages/IDEView.js @@ -21,20 +21,29 @@ class IDEView extends React.Component { const id = this.props.params.project_id; this.props.getProject(id); - if (this.props.preferences.autosave) { + // if autosave is on and the user is the owner of the project + if (this.props.preferences.autosave + && this.props.project.owner + && this.props.project.owner.id === this.props.user.id) { this.autosaveInterval = setInterval(this.props.saveProject, 30000); } } } componentDidUpdate(prevProps) { - if (!this.autosaveInterval && - ((this.props.preferences.autosave && !prevProps.preferences.autosave) || - (this.props.project.id && !prevProps.project.id))) { - this.autosaveInterval = setInterval(this.props.saveProject, 30000); - } else if (this.autosaveInterval && !this.props.preferences.autosave && prevProps.preferences.autosave) { - clearInterval(this.autosaveInterval); - this.autosaveInterval = null; + // if user is the owner of the project + if (this.props.project.owner && this.props.project.owner.id === this.props.user.id) { + // if the user turns on autosave + // or the user saves the project for the first time + if (!this.autosaveInterval && + ((this.props.preferences.autosave && !prevProps.preferences.autosave) || + (this.props.project.id && !prevProps.project.id))) { + this.autosaveInterval = setInterval(this.props.saveProject, 30000); + // if user turns off autosave preference + } else if (this.autosaveInterval && !this.props.preferences.autosave && prevProps.preferences.autosave) { + clearInterval(this.autosaveInterval); + this.autosaveInterval = null; + } } } @@ -145,7 +154,8 @@ IDEView.propTypes = { }), getProject: PropTypes.func.isRequired, user: PropTypes.shape({ - authenticated: PropTypes.bool.isRequired + authenticated: PropTypes.bool.isRequired, + id: PropTypes.string }).isRequired, createProject: PropTypes.func.isRequired, saveProject: PropTypes.func.isRequired, @@ -163,7 +173,8 @@ IDEView.propTypes = { id: PropTypes.string, name: PropTypes.string.isRequired, owner: PropTypes.shape({ - username: PropTypes.string + username: PropTypes.string, + id: PropTypes.string }) }).isRequired, setProjectName: PropTypes.func.isRequired, diff --git a/client/modules/User/actions.js b/client/modules/User/actions.js index e46cb778..941624b9 100644 --- a/client/modules/User/actions.js +++ b/client/modules/User/actions.js @@ -51,6 +51,8 @@ export function getUser() { preferences: response.data.preferences }); }) - .catch(response => dispatch(authError(response.data.error))); + .catch(response => { + dispatch(authError(response.data.error)); + }); }; } diff --git a/server/controllers/project.controller.js b/server/controllers/project.controller.js index 955b9b1b..cdbfd650 100644 --- a/server/controllers/project.controller.js +++ b/server/controllers/project.controller.js @@ -46,7 +46,7 @@ export function getProjects(req, res) { if (req.user) { Project.find({user: req.user._id}) // eslint-disable-line no-underscore-dangle .sort('-createdAt') - .select('name files _id createdAt updatedAt') + .select('name files id createdAt updatedAt') .exec((err, projects) => { res.json(projects); }); diff --git a/server/controllers/session.controller.js b/server/controllers/session.controller.js index c36c9d51..cbee93f4 100644 --- a/server/controllers/session.controller.js +++ b/server/controllers/session.controller.js @@ -11,7 +11,9 @@ export function createSession(req, res, next) { if (innerErr) { return next(innerErr); } return res.json({ email: req.user.email, - username: req.user.username + username: req.user.username, + preferences: req.user.preferences, + id: req.user._id }); }); })(req, res, next); @@ -22,7 +24,8 @@ export function getSession(req, res) { return res.json({ email: req.user.email, username: req.user.username, - preferences: req.user.preferences + preferences: req.user.preferences, + id: req.user._id }); } return res.status(404).send({ message: 'Session does not exist' }); diff --git a/server/controllers/user.controller.js b/server/controllers/user.controller.js index 436c774c..488aca5f 100644 --- a/server/controllers/user.controller.js +++ b/server/controllers/user.controller.js @@ -22,7 +22,9 @@ export function createUser(req, res, next) { } res.json({ email: req.user.email, - username: req.user.username + username: req.user.username, + preferences: req.user.preferences, + id: req.user._id }); }); }); diff --git a/server/models/user.js b/server/models/user.js index 8f7aff93..c593159c 100644 --- a/server/models/user.js +++ b/server/models/user.js @@ -10,7 +10,7 @@ const userSchema = new Schema({ email: { type: String, unique: true }, tokens: Array, preferences: { - textSize: { type: Number, default: 18 }, + fontSize: { type: Number, default: 18 }, indentationAmount: { type: Number, default: 2 }, isTabIndent: { type: Boolean, default: false }, autosave: { type: Boolean, default: true } @@ -33,6 +33,15 @@ userSchema.pre('save', function checkPassword(next) { // eslint-disable-line con }); }); +userSchema.virtual('id').get(function(){ + return this._id.toHexString(); +}); + +userSchema.set('toJSON', { + virtuals: true +}); + + /** * Helper method for validating user's password. */