put preferences to server, need to look at mongoose docs tho because is wrong

This commit is contained in:
catarak 2016-08-04 21:43:13 -04:00
parent 013b97d96d
commit c76b1353c3
6 changed files with 99 additions and 27 deletions

View file

@ -1,30 +1,84 @@
import * as ActionTypes from '../../../constants'; import * as ActionTypes from '../../../constants';
// import axios from 'axios'; import axios from 'axios';
// const ROOT_URL = location.href.indexOf('localhost') > 0 ? 'http://localhost:8000/api' : '/api'; const ROOT_URL = location.href.indexOf('localhost') > 0 ? 'http://localhost:8000/api' : '/api';
function updatePreferences(formParams, dispatch) {
axios.put(`${ROOT_URL}/preferences`, formParams, { withCredentials: true })
.then(() => {
})
.catch((response) => dispatch({
type: ActionTypes.ERROR,
error: response.data
}));
}
export function setFontSize(value) { export function setFontSize(value) {
return { return (dispatch, getState) => { // eslint-disable-line
dispatch({
type: ActionTypes.SET_FONT_SIZE, type: ActionTypes.SET_FONT_SIZE,
value value
});
const state = getState();
if (state.user.authenticated) {
const formParams = {
preferences: {
textSize: value
}
};
updatePreferences(formParams, dispatch);
}
}; };
} }
export function setIndentation(value) { export function setIndentation(value) {
return { return (dispatch, getState) => {
dispatch({
type: ActionTypes.SET_INDENTATION, type: ActionTypes.SET_INDENTATION,
value value
});
const state = getState();
if (state.user.authenticated) {
const formParams = {
preferences: {
indentationAmount: value
}
};
updatePreferences(formParams, dispatch);
}
}; };
} }
export function indentWithTab() { export function indentWithTab() {
return { return (dispatch, getState) => {
dispatch({
type: ActionTypes.INDENT_WITH_TAB type: ActionTypes.INDENT_WITH_TAB
});
const state = getState();
if (state.user.authenticated) {
const formParams = {
preferences: {
isTabIndent: true
}
};
updatePreferences(formParams, dispatch);
}
}; };
} }
export function indentWithSpace() { export function indentWithSpace() {
return { return (dispatch, getState) => {
dispatch({
type: ActionTypes.INDENT_WITH_SPACE type: ActionTypes.INDENT_WITH_SPACE
});
const state = getState();
if (state.user.authenticated) {
const formParams = {
preferences: {
isTabIndent: false
}
};
updatePreferences(formParams, dispatch);
}
}; };
} }

View file

@ -1,9 +1,9 @@
import React, { PropTypes } from 'react'; import React, { PropTypes } from 'react';
import InlineSVG from 'react-inlinesvg'; import InlineSVG from 'react-inlinesvg';
import classNames from 'classnames'; import classNames from 'classnames';
import { bindActionCreators } from 'redux'; // import { bindActionCreators } from 'redux';
import { connect } from 'react-redux'; // import { connect } from 'react-redux';
import * as PreferencesActions from '../actions/preferences'; // import * as PreferencesActions from '../actions/preferences';
const exitUrl = require('../../../images/exit.svg'); const exitUrl = require('../../../images/exit.svg');
const plusUrl = require('../../../images/plus.svg'); const plusUrl = require('../../../images/plus.svg');
@ -111,16 +111,6 @@ class Preferences extends React.Component {
} }
} }
function mapStateToProps(state) {
return {
...state.preferences
};
}
function mapDispatchToProps(dispatch) {
return bindActionCreators(PreferencesActions, dispatch);
}
Preferences.propTypes = { Preferences.propTypes = {
isVisible: PropTypes.bool.isRequired, isVisible: PropTypes.bool.isRequired,
closePreferences: PropTypes.func.isRequired, closePreferences: PropTypes.func.isRequired,
@ -133,4 +123,4 @@ Preferences.propTypes = {
setFontSize: PropTypes.func.isRequired setFontSize: PropTypes.func.isRequired
}; };
export default connect(mapStateToProps, mapDispatchToProps)(Preferences); export default Preferences;

View file

@ -12,6 +12,7 @@ import { connect } from 'react-redux';
import * as FileActions from '../actions/files'; import * as FileActions from '../actions/files';
import * as IDEActions from '../actions/ide'; import * as IDEActions from '../actions/ide';
import * as ProjectActions from '../actions/project'; import * as ProjectActions from '../actions/project';
import * as PreferencesActions from '../actions/preferences';
import { getFile, getHTMLFile, getJSFiles, getCSSFiles, setSelectedFile } from '../reducers/files'; import { getFile, getHTMLFile, getJSFiles, getCSSFiles, setSelectedFile } from '../reducers/files';
class IDEView extends React.Component { class IDEView extends React.Component {
@ -60,6 +61,13 @@ class IDEView extends React.Component {
<Preferences <Preferences
isVisible={this.props.ide.preferencesIsVisible} isVisible={this.props.ide.preferencesIsVisible}
closePreferences={this.props.closePreferences} closePreferences={this.props.closePreferences}
fontSize={this.props.preferences.fontSize}
indentationAmount={this.props.preferences.indentationAmount}
setIndentation={this.props.setIndentation}
indentWithSpace={this.props.indentWithSpace}
indentWithTab={this.props.indentWithTab}
isTabIndent={this.props.preferences.isTabIndent}
setFontSize={this.props.setFontSize}
/> />
<div className="editor-preview-container"> <div className="editor-preview-container">
<Sidebar <Sidebar
@ -158,6 +166,10 @@ IDEView.propTypes = {
isTabIndent: PropTypes.bool.isRequired isTabIndent: PropTypes.bool.isRequired
}).isRequired, }).isRequired,
closePreferences: PropTypes.func.isRequired, closePreferences: PropTypes.func.isRequired,
setFontSize: PropTypes.func.isRequired,
setIndentation: PropTypes.func.isRequired,
indentWithTab: PropTypes.func.isRequired,
indentWithSpace: PropTypes.func.isRequired,
files: PropTypes.array.isRequired, files: PropTypes.array.isRequired,
updateFileContent: PropTypes.func.isRequired, updateFileContent: PropTypes.func.isRequired,
selectedFile: PropTypes.shape({ selectedFile: PropTypes.shape({
@ -203,7 +215,8 @@ function mapDispatchToProps(dispatch) {
return bindActionCreators(Object.assign({}, return bindActionCreators(Object.assign({},
FileActions, FileActions,
ProjectActions, ProjectActions,
IDEActions), IDEActions,
PreferencesActions),
dispatch); dispatch);
} }

View file

@ -69,6 +69,7 @@
.preference__vertical-list { .preference__vertical-list {
display: flex; display: flex;
flex-direction: column; flex-direction: column;
width: #{90 / $base-font-size}rem;
} }
.preference__option { .preference__option {

View file

@ -28,3 +28,15 @@ export function createUser(req, res, next) {
}); });
}); });
} }
export function updatePreferences(req, res) {
User.findByIdAndUpdate(req.user._id,
{
$set: req.body
})
.populate('preferences')
.exec((err, updatedPreferences) => {
if (err) return res.json({ success: false });
return res.json(updatedPreferences);
});
}

View file

@ -4,4 +4,6 @@ const router = new Router();
router.route('/signup').post(UserController.createUser); router.route('/signup').post(UserController.createUser);
router.route('/preferences').put(UserController.updatePreferences);
export default router; export default router;