Skip to content
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

clean up deleted files #762

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
11 changes: 10 additions & 1 deletion src/actions/org.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,11 @@ const doSync = ({
.catch(() => {
dispatch(hideLoadingMessage());
dispatch(setIsLoading(false, path));
dispatch(setOrgFileErrorMessage(`File ${path} not found`));
// the file got deleted. clean it up
dispatch(removeOrgFile(path));
if (getState().org.present.get('path') === path) {
dispatch(setOrgFileErrorMessage(`File ${path} not found`));
}
});
};

Expand Down Expand Up @@ -710,3 +714,8 @@ export const deleteBookmark = (context, bookmark) => ({
context,
bookmark,
});

export const removeOrgFile = (path) => ({
type: 'REMOVE_ORG_FILE',
path,
});
8 changes: 6 additions & 2 deletions src/actions/sync_backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { ActionCreators } from 'redux-undo';

import { setLoadingMessage, hideLoadingMessage, clearModalStack, setIsLoading } from './base';
import { parseFile, setDirty, setLastSyncAt, setOrgFileErrorMessage } from './org';
import { parseFile, setDirty, setLastSyncAt, setOrgFileErrorMessage, removeOrgFile } from './org';
import { localStorageAvailable, persistField } from '../util/settings_persister';
import { createGitlabOAuth } from '../sync_backend_clients/gitlab_sync_backend_client';

Expand Down Expand Up @@ -127,7 +127,11 @@ export const downloadFile = (path) => {
.catch(() => {
dispatch(hideLoadingMessage());
dispatch(setIsLoading(false, path));
dispatch(setOrgFileErrorMessage(`File ${path} not found`));
// the file got deleted. clean it up
dispatch(removeOrgFile(path));
if (getState().org.present.get('path') === path) {
dispatch(setOrgFileErrorMessage(`File ${path} not found`));
}
});
};
};
15 changes: 14 additions & 1 deletion src/middleware/live_sync.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
import { sync } from '../actions/org';
import { persistIsDirty, saveFileToLocalStorage } from '../util/file_persister';
import {
persistIsDirty,
saveFileToLocalStorage,
removeFileFromLocalStorage,
} from '../util/file_persister';
import { determineAffectedFiles } from '../reducers/org';

export default (store) => (next) => (action) => {
// if a file got deleted in the back end, remove it and continue
if (action.type === 'REMOVE_ORG_FILE') {
setTimeout(() => {
removeFileFromLocalStorage(action.path);
}, 0);

return next(action);
}

// middleware is run before the reducer. to persist the result of the action,
// save and sync are done in a callback so they happen after the state is changed
setTimeout(() => {
Expand Down
10 changes: 10 additions & 0 deletions src/reducers/org.js
Original file line number Diff line number Diff line change
Expand Up @@ -1371,6 +1371,14 @@ const deleteBookmark = (state, { context, bookmark }) => {
);
};

const removeOrgFile = (state, { path }) =>
state
.update('files', (files) => files.delete(path))
.update('fileSettings', (fileSettings) =>
fileSettings.filter((fileSetting) => fileSetting.path !== path)
)
.update('opennessState', (opennessState) => opennessState.delete(path));

const addNewEmptyFileSetting = (state) =>
state.update('fileSettings', (settings) =>
settings.push(
Expand Down Expand Up @@ -1543,6 +1551,8 @@ const reducer = (state, action) => {
return saveBookmark(state, action);
case 'DELETE_BOOKMARK':
return deleteBookmark(state, action);
case 'REMOVE_ORG_FILE':
return removeOrgFile(state, action);
default:
return state;
}
Expand Down
2 changes: 1 addition & 1 deletion src/sync_backend_clients/dropbox_sync_backend_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export default (accessToken) => {
// https://github.com/200ok-ch/organice/issues/108
const objectContainsTagErrorP = (function () {
try {
return JSON.parse(error.error).error.path['.tag'] === 'not_found';
return error.error.error.path['.tag'] === 'not_found';
} catch (e) {
return false;
}
Expand Down
22 changes: 22 additions & 0 deletions src/util/file_persister.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,28 @@ export const saveFileContentsToLocalStorage = (path, contents) => {
}
};

export const removeFileFromLocalStorage = (path) => {
if (localStorageAvailable && !path.startsWith(STATIC_FILE_PREFIX)) {
let isDirty = JSON.parse(localStorage.getItem('isDirty')) || {};
delete isDirty[path];
localStorage.setItem('isDirty', JSON.stringify(isDirty));

let persistedFiles = JSON.parse(localStorage.getItem('persistedFiles')) || {};
delete persistedFiles[path];
localStorage.setItem('persistedFiles', JSON.stringify(persistedFiles));

let headerOpenness = JSON.parse(localStorage.getItem('headerOpenness')) || {};
delete headerOpenness[path];
localStorage.setItem('headerOpenness', JSON.stringify(headerOpenness));

let fileSettings = JSON.parse(localStorage.getItem('fileSettings')) || [];
fileSettings = fileSettings.filter((fileSetting) => fileSetting.path !== path);
localStorage.setItem('fileSettings', JSON.stringify(fileSettings));

localStorage.removeItem('files__' + path);
}
};

const saveFunctionToDebounce = (state, path) => {
if (localStorageAvailable && !path.startsWith(STATIC_FILE_PREFIX)) {
const persistedFiles = JSON.parse(localStorage.getItem('persistedFiles')) || {};
Expand Down