fix #254, stale project warning works correctly, removed unused actions and state for ide

This commit is contained in:
Cassie Tarakajian 2017-03-02 14:38:29 -05:00
parent 5982203b28
commit fe4c2641e3
7 changed files with 39 additions and 52 deletions

View file

@ -201,19 +201,6 @@ export function resetJustOpenedProject() {
}; };
} }
export function setProjectSavedTime(value) {
return {
type: ActionTypes.SET_PROJECT_SAVED_TIME,
value
};
}
export function resetProjectSavedTime() {
return {
type: ActionTypes.RESET_PROJECT_SAVED_TIME,
};
}
export function setPreviousPath(path) { export function setPreviousPath(path) {
return { return {
type: ActionTypes.SET_PREVIOUS_PATH, type: ActionTypes.SET_PREVIOUS_PATH,

View file

@ -1,39 +1,21 @@
import { browserHistory } from 'react-router'; import { browserHistory } from 'react-router';
import axios from 'axios'; import axios from 'axios';
import objectID from 'bson-objectid'; import objectID from 'bson-objectid';
import moment from 'moment';
import * as ActionTypes from '../../../constants'; import * as ActionTypes from '../../../constants';
import { showToast, setToastText } from './toast'; import { showToast, setToastText } from './toast';
import { setUnsavedChanges, import { setUnsavedChanges,
justOpenedProject, justOpenedProject,
resetJustOpenedProject, resetJustOpenedProject,
setProjectSavedTime,
resetProjectSavedTime,
showErrorModal } from './ide'; showErrorModal } from './ide';
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';
export function getProject(id) { export function setProject(project) {
return (dispatch, getState) => { return {
const state = getState();
dispatch(justOpenedProject());
if (state.ide.justOpenedProject) {
dispatch(resetProjectSavedTime());
}
axios.get(`${ROOT_URL}/projects/${id}`, { withCredentials: true })
.then((response) => {
dispatch({
type: ActionTypes.SET_PROJECT, type: ActionTypes.SET_PROJECT,
project: response.data, project,
files: response.data.files, files: project.files,
owner: response.data.user owner: project.user
});
dispatch(setUnsavedChanges(false));
})
.catch(response => dispatch({
type: ActionTypes.ERROR,
error: response.data
}));
}; };
} }
@ -44,6 +26,21 @@ export function setProjectName(name) {
}; };
} }
export function getProject(id) {
return (dispatch, getState) => {
dispatch(justOpenedProject());
axios.get(`${ROOT_URL}/projects/${id}`, { withCredentials: true })
.then((response) => {
dispatch(setProject(response.data));
dispatch(setUnsavedChanges(false));
})
.catch(response => dispatch({
type: ActionTypes.ERROR,
error: response.data
}));
};
}
export function saveProject(autosave = false) { export function saveProject(autosave = false) {
return (dispatch, getState) => { return (dispatch, getState) => {
const state = getState(); const state = getState();
@ -54,9 +51,9 @@ export function saveProject(autosave = false) {
formParams.files = [...state.files]; formParams.files = [...state.files];
if (state.project.id) { if (state.project.id) {
axios.put(`${ROOT_URL}/projects/${state.project.id}`, formParams, { withCredentials: true }) axios.put(`${ROOT_URL}/projects/${state.project.id}`, formParams, { withCredentials: true })
.then(() => { .then((response) => {
dispatch(setUnsavedChanges(false)); dispatch(setUnsavedChanges(false));
dispatch(setProjectSavedTime(moment().format())); dispatch(setProject(response.data));
dispatch({ dispatch({
type: ActionTypes.PROJECT_SAVE_SUCCESS type: ActionTypes.PROJECT_SAVE_SUCCESS
}); });
@ -88,7 +85,7 @@ export function saveProject(autosave = false) {
axios.post(`${ROOT_URL}/projects`, formParams, { withCredentials: true }) axios.post(`${ROOT_URL}/projects`, formParams, { withCredentials: true })
.then((response) => { .then((response) => {
dispatch(setUnsavedChanges(false)); dispatch(setUnsavedChanges(false));
dispatch(setProjectSavedTime(moment().format())); dispatch(setProject(response.data));
browserHistory.push(`/${response.data.user.username}/sketches/${response.data.id}`); browserHistory.push(`/${response.data.user.username}/sketches/${response.data.id}`);
dispatch({ dispatch({
type: ActionTypes.NEW_PROJECT, type: ActionTypes.NEW_PROJECT,

View file

@ -114,6 +114,7 @@ class Editor extends React.Component {
if (this.props.files.length !== nextProps.files.length) { if (this.props.files.length !== nextProps.files.length) {
this.initializeDocuments(nextProps.files); this.initializeDocuments(nextProps.files);
} }
console.log(nextProps.projectSavedTime);
} }
componentDidUpdate(prevProps) { componentDidUpdate(prevProps) {

View file

@ -304,7 +304,7 @@ class IDEView extends React.Component {
stopSketch={this.props.stopSketch} stopSketch={this.props.stopSketch}
autorefresh={this.props.preferences.autorefresh} autorefresh={this.props.preferences.autorefresh}
unsavedChanges={this.props.ide.unsavedChanges} unsavedChanges={this.props.ide.unsavedChanges}
projectSavedTime={this.props.ide.projectSavedTime} projectSavedTime={this.props.project.updatedAt}
isExpanded={this.props.ide.sidebarIsExpanded} isExpanded={this.props.ide.sidebarIsExpanded}
expandSidebar={this.props.expandSidebar} expandSidebar={this.props.expandSidebar}
collapseSidebar={this.props.collapseSidebar} collapseSidebar={this.props.collapseSidebar}
@ -486,7 +486,8 @@ IDEView.propTypes = {
owner: PropTypes.shape({ owner: PropTypes.shape({
username: PropTypes.string, username: PropTypes.string,
id: PropTypes.string id: PropTypes.string
}) }),
updatedAt: PropTypes.string
}).isRequired, }).isRequired,
setProjectName: PropTypes.func.isRequired, setProjectName: PropTypes.func.isRequired,
openPreferences: PropTypes.func.isRequired, openPreferences: PropTypes.func.isRequired,

View file

@ -17,7 +17,6 @@ const initialState = {
previewIsRefreshing: false, previewIsRefreshing: false,
infiniteLoopMessage: '', infiniteLoopMessage: '',
justOpenedProject: false, justOpenedProject: false,
projectSavedTime: '',
previousPath: '/', previousPath: '/',
errorType: undefined errorType: undefined
}; };
@ -86,10 +85,6 @@ const ide = (state = initialState, action) => {
return Object.assign({}, state, { justOpenedProject: true }); return Object.assign({}, state, { justOpenedProject: true });
case ActionTypes.RESET_JUST_OPENED_PROJECT: case ActionTypes.RESET_JUST_OPENED_PROJECT:
return Object.assign({}, state, { justOpenedProject: false }); return Object.assign({}, state, { justOpenedProject: false });
case ActionTypes.SET_PROJECT_SAVED_TIME:
return Object.assign({}, state, { projectSavedTime: action.value });
case ActionTypes.RESET_PROJECT_SAVED_TIME:
return Object.assign({}, state, { projectSavedTime: '' });
case ActionTypes.SET_PREVIOUS_PATH: case ActionTypes.SET_PREVIOUS_PATH:
return Object.assign({}, state, { previousPath: action.path }); return Object.assign({}, state, { previousPath: action.path });
case ActionTypes.SHOW_ERROR_MODAL: case ActionTypes.SHOW_ERROR_MODAL:

View file

@ -36,6 +36,8 @@ const project = (state, action) => {
return Object.assign({}, state, { isEditingName: true }); return Object.assign({}, state, { isEditingName: true });
case ActionTypes.HIDE_EDIT_PROJECT_NAME: case ActionTypes.HIDE_EDIT_PROJECT_NAME:
return Object.assign({}, state, { isEditingName: false }); return Object.assign({}, state, { isEditingName: false });
case ActionTypes.SET_PROJECT_SAVED_TIME:
return Object.assign({}, state, { updatedAt: action.value });
default: default:
return state; return state;
} }

View file

@ -1,5 +1,6 @@
import archiver from 'archiver'; import archiver from 'archiver';
import request from 'request'; import request from 'request';
import moment from 'moment';
import Project from '../models/project'; import Project from '../models/project';
import User from '../models/user'; import User from '../models/user';
@ -39,12 +40,15 @@ export function updateProject(req, res) {
res.status(403).send({ success: false, message: 'Session does not match owner of project.' }); res.status(403).send({ success: false, message: 'Session does not match owner of project.' });
return; return;
} }
// if (req.body.updatedAt && moment(req.body.updatedAt) < moment(project.updatedAt)) { if (req.body.updatedAt && moment(req.body.updatedAt) < moment(project.updatedAt)) {
// return res.status(409).send({ success: false, message: 'Attempted to save stale version of project.' }); return res.status(409).send({ success: false, message: 'Attempted to save stale version of project.' });
// } }
Project.findByIdAndUpdate(req.params.project_id, Project.findByIdAndUpdate(req.params.project_id,
{ {
$set: req.body $set: req.body
},
{
new: true
}) })
.populate('user', 'username') .populate('user', 'username')
.exec((updateProjectErr, updatedProject) => { .exec((updateProjectErr, updatedProject) => {
@ -60,13 +64,13 @@ export function updateProject(req, res) {
staleIds.forEach((staleId) => { staleIds.forEach((staleId) => {
updatedProject.files.id(staleId).remove(); updatedProject.files.id(staleId).remove();
}); });
updatedProject.save((innerErr) => { updatedProject.save((innerErr, savedProject) => {
if (innerErr) { if (innerErr) {
console.log(innerErr); console.log(innerErr);
res.json({ success: false }); res.json({ success: false });
return; return;
} }
res.json(updatedProject); res.json(savedProject);
}); });
} }
res.json(updatedProject); res.json(updatedProject);