Skip to content

Fix needs saving mark for renamed: Fixes #576 #652

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 44 additions & 2 deletions client/modules/IDE/actions/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { browserHistory } from 'react-router';
import axios from 'axios';
import objectID from 'bson-objectid';
import each from 'async/each';
import { isEqual } from 'lodash';
import { isEqual, isPlainObject } from 'lodash';
import * as ActionTypes from '../../../constants';
import { showToast, setToastText } from './toast';
import { setUnsavedChanges,
Expand Down Expand Up @@ -71,6 +71,48 @@ export function clearPersistedState() {
};
}

/**
* Compare whether two project objects are the same. Used to check that the
* response returned from the server still matches the local copy.
*/
function projectIsEqual(base, other) {
const filter = ['isEditingName', 'isOptionsOpen'];

if (Array.isArray(base)) {
// It must be a collection of children (files or folders).
if (!Array.isArray(other) || other.length !== base.length) {
// If the other one is not also an array, or if the length is different
// then they cannot be the same.
return false;
}

// Check each of the items in the array for equality with the corresponding
// object in the other.
let allEqual = true;
for (let i = 0; i < base.length; i += 1) {
allEqual = allEqual && projectIsEqual(base[i], other[i]);
}
return allEqual;
} else if (isPlainObject(base)) {
if (!isPlainObject(other)) {
// If the other one is not also a plain object, then they cannot match.
return false;
}
// It must be either a file or a folder object.
// Check the individual object for equality.
let allEqual = true;
Object.keys(base).forEach((k, i) => {
// Only compare keys that are not in the filter (unless they are set true)
if (filter.indexOf(k) < 0 || base[k] === true) {
allEqual = allEqual && isEqual(base[k], other[k]);
}
});
return allEqual;
}
// Otherwise, we don't know what kind of object it is. Return false.
return false;
}

export function saveProject(autosave = false) {
return (dispatch, getState) => {
const state = getState();
Expand All @@ -84,7 +126,7 @@ export function saveProject(autosave = false) {
.then((response) => {
const currentState = getState();
const savedProject = Object.assign({}, response.data);
if (!isEqual(currentState.files, response.data.files)) {
if (!projectIsEqual(currentState.files, response.data.files)) {
savedProject.files = currentState.files;
dispatch(setUnsavedChanges(true));
} else {
Expand Down