set preferences when authenticating user

This commit is contained in:
catarak 2016-08-09 17:50:45 -04:00
parent 2f581a6a77
commit d9ea10c4c6
5 changed files with 18 additions and 4 deletions

View file

@ -52,6 +52,7 @@ export const SHOW_EDIT_FILE_NAME = 'SHOW_EDIT_FILE_NAME';
export const HIDE_EDIT_FILE_NAME = 'HIDE_EDIT_FILE_NAME';
export const SET_AUTOSAVE = 'SET_AUTOSAVE';
export const SET_PREFERENCES = 'SET_PREFERENCES';
// eventually, handle errors more specifically and better
export const ERROR = 'ERROR';

View file

@ -21,14 +21,20 @@ class IDEView extends React.Component {
const id = this.props.params.project_id;
this.props.getProject(id);
// if autosave is enabled
this.autosaveInterval = setInterval(this.props.saveProject, 30000);
if (this.props.preferences.autosave) {
this.autosaveInterval = setInterval(this.props.saveProject, 30000);
}
}
}
componentDidUpdate(prevProps) {
if (!this.autosaveInterval && this.props.project.id && !prevProps.project.id) {
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;
}
}

View file

@ -23,6 +23,8 @@ const preferences = (state = initialState, action) => {
});
case ActionTypes.SET_AUTOSAVE:
return Object.assign({}, state, { autosave: action.value });
case ActionTypes.SET_PREFERENCES:
return action.preferences;
default:
return state;
}

View file

@ -46,6 +46,10 @@ export function getUser() {
type: ActionTypes.AUTH_USER,
user: response.data
});
dispatch({
type: ActionTypes.SET_PREFERENCES,
preferences: response.data.preferences
});
})
.catch(response => dispatch(authError(response.data.error)));
};

View file

@ -21,7 +21,8 @@ export function getSession(req, res) {
if (req.user) {
return res.json({
email: req.user.email,
username: req.user.username
username: req.user.username,
preferences: req.user.preferences
});
}
return res.status(404).send({ message: 'Session does not exist' });