From 9f9425c5e962e277b57b08cdcb0ec1d512ae2b3a Mon Sep 17 00:00:00 2001 From: catarak Date: Tue, 9 Aug 2016 14:20:54 -0400 Subject: [PATCH] update preferences server side --- client/constants.js | 2 ++ client/modules/IDE/actions/preferences.js | 7 ++++++ client/modules/IDE/reducers/preferences.js | 3 ++- server/controllers/user.controller.js | 26 +++++++++++++++------- 4 files changed, 29 insertions(+), 9 deletions(-) diff --git a/client/constants.js b/client/constants.js index 9916478e..065b2efb 100644 --- a/client/constants.js +++ b/client/constants.js @@ -51,5 +51,7 @@ export const DELETE_FILE = 'DELETE_FILE'; export const SHOW_EDIT_FILE_NAME = 'SHOW_EDIT_FILE_NAME'; export const HIDE_EDIT_FILE_NAME = 'HIDE_EDIT_FILE_NAME'; +export const TOGGLE_AUTOSAVE = 'SET_AUTOSAVE'; + // eventually, handle errors more specifically and better export const ERROR = 'ERROR'; diff --git a/client/modules/IDE/actions/preferences.js b/client/modules/IDE/actions/preferences.js index 2fb93ee2..5b5ccc66 100644 --- a/client/modules/IDE/actions/preferences.js +++ b/client/modules/IDE/actions/preferences.js @@ -82,3 +82,10 @@ export function indentWithSpace() { } }; } + +export function setAutosave(value) { + return { + type: ActionTypes.SET_AUTOSAVE, + value + }; +} diff --git a/client/modules/IDE/reducers/preferences.js b/client/modules/IDE/reducers/preferences.js index 93da7855..4290e0e9 100644 --- a/client/modules/IDE/reducers/preferences.js +++ b/client/modules/IDE/reducers/preferences.js @@ -3,7 +3,8 @@ import * as ActionTypes from '../../../constants'; const initialState = { fontSize: 18, indentationAmount: 2, - isTabIndent: true + isTabIndent: true, + autosave: true }; const preferences = (state = initialState, action) => { diff --git a/server/controllers/user.controller.js b/server/controllers/user.controller.js index 113bd0f5..436c774c 100644 --- a/server/controllers/user.controller.js +++ b/server/controllers/user.controller.js @@ -30,13 +30,23 @@ export function createUser(req, res, next) { } export function updatePreferences(req, res) { - User.findByIdAndUpdate(req.user._id, - { - $set: req.body + User.findById(req.user.id, (err, user) => { + if (err) { + return res.status(500).json({error: err}); + } + if (!user){ + return res.status(404).json({error: 'Document not found'}); + } + + const preferences = Object.assign({}, user.preferences, req.body.preferences); + user.preferences = preferences; + + user.save((err) => { + if (err) { + return res.status(500).json({error: err}); + } + + return res.json(user.preferences); + }); }) - .populate('preferences') - .exec((err, updatedPreferences) => { - if (err) return res.json({ success: false }); - return res.json(updatedPreferences); - }); }