Skip to content

Commit 9b83a73

Browse files
committed
GDrive instantaneous folder delete - delete all persistenceFileArray entries of deleted folder as well
1 parent 12cbe8b commit 9b83a73

File tree

4 files changed

+48
-6
lines changed

4 files changed

+48
-6
lines changed

src/commons/fileSystem/FileSystemActions.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
DELETE_ALL_PERSISTENCE_FILES,
1010
DELETE_GITHUB_SAVE_INFO,
1111
DELETE_PERSISTENCE_FILE,
12+
DELETE_PERSISTENCE_FOLDER_AND_CHILDREN,
1213
SET_IN_BROWSER_FILE_SYSTEM,
1314
SET_PERSISTENCE_FILE_LAST_EDIT_BY_PATH,
1415
UPDATE_LAST_EDITED_FILE_PATH,
@@ -46,6 +47,11 @@ export const deletePersistenceFile = createAction(
4647
(persistenceFile: PersistenceFile) => ({ payload: persistenceFile })
4748
);
4849

50+
export const deletePersistenceFolderAndChildren = createAction(
51+
DELETE_PERSISTENCE_FOLDER_AND_CHILDREN,
52+
(persistenceFile: PersistenceFile) => ({ payload: persistenceFile })
53+
);
54+
4955
export const updatePersistenceFilePathAndNameByPath = createAction(
5056
UPDATE_PERSISTENCE_FILE_PATH_AND_NAME_BY_PATH,
5157
(oldPath: string, newPath: string, newFileName: string) => ({

src/commons/fileSystem/FileSystemReducer.ts

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
deleteAllPersistenceFiles,
1212
deleteGithubSaveInfo,
1313
deletePersistenceFile,
14+
deletePersistenceFolderAndChildren,
1415
setInBrowserFileSystem,
1516
setPersistenceFileLastEditByPath,
1617
updateLastEditedFilePath,
@@ -133,6 +134,33 @@ export const FileSystemReducer: Reducer<FileSystemState, SourceActionType> = cre
133134
state.persistenceFileArray = newPersistenceFileArray;
134135
}
135136
})
137+
.addCase(deletePersistenceFolderAndChildren, (state, action) => { // check if github is syncing?
138+
const newPersistenceFileArray = state['persistenceFileArray'].filter(
139+
e => e.id !== action.payload.id
140+
);
141+
// get children
142+
// get current level of folder
143+
const regexResult = filePathRegex.exec(action.payload.path!)!;
144+
145+
const currFolderSplit: string[] = regexResult[0].slice(1).split('/');
146+
const folderName = regexResult[2];
147+
const currFolderLevel = currFolderSplit.length - 1;
148+
149+
state.persistenceFileArray = newPersistenceFileArray
150+
.filter(e => e.path)
151+
.filter(e => {
152+
const r = filePathRegex.exec(e.path!)!;
153+
const currParentFolders = r[0].slice(1).split('/');
154+
console.log('currParentFolders', currParentFolders, 'folderLevel', currFolderLevel);
155+
if (currParentFolders.length <= currFolderLevel) {
156+
return true; // not a child of folder
157+
}
158+
if (currParentFolders[currFolderLevel] !== folderName) {
159+
return true; // not a child of folder
160+
}
161+
return false;
162+
});
163+
})
136164
.addCase(deleteAllPersistenceFiles, (state, action) => {
137165
state.persistenceFileArray = [];
138166
})
@@ -158,7 +186,7 @@ export const FileSystemReducer: Reducer<FileSystemState, SourceActionType> = cre
158186
const regexResult = filePathRegex.exec(action.payload.newPath)!;
159187

160188
const currFolderSplit: string[] = regexResult[0].slice(1).split('/');
161-
const currFolderIndex = currFolderSplit.length - 1;
189+
const currFolderLevel = currFolderSplit.length - 1;
162190

163191
// /fold1/ becomes ["fold1"]
164192
// /fold1/fold2/ becomes ["fold1", "fold2"]
@@ -172,15 +200,15 @@ export const FileSystemReducer: Reducer<FileSystemState, SourceActionType> = cre
172200
.map(e => {
173201
const r = filePathRegex.exec(e.path!)!;
174202
const currParentFolders = r[0].slice(1).split('/');
175-
console.log('currParentFolders', currParentFolders, 'folderLevel', currFolderIndex);
176-
if (currParentFolders.length <= currFolderIndex) {
203+
console.log('currParentFolders', currParentFolders, 'folderLevel', currFolderLevel);
204+
if (currParentFolders.length <= currFolderLevel) {
177205
return e; // not a child of folder
178206
}
179-
if (currParentFolders[currFolderIndex] !== action.payload.oldFolderName) {
207+
if (currParentFolders[currFolderLevel] !== action.payload.oldFolderName) {
180208
return e; // not a child of folder
181209
}
182210
// only children remain
183-
currParentFolders[currFolderIndex] = action.payload.newFolderName;
211+
currParentFolders[currFolderLevel] = action.payload.newFolderName;
184212
currParentFolders[0] = '/' + currParentFolders[0];
185213
const newPath = currParentFolders.join('/');
186214
console.log('from', e.path, 'to', newPath);

src/commons/fileSystem/FileSystemTypes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export const ADD_GITHUB_SAVE_INFO = 'ADD_GITHUB_SAVE_INFO';
66
export const ADD_PERSISTENCE_FILE = 'ADD_PERSISTENCE_FILE';
77
export const DELETE_GITHUB_SAVE_INFO = 'DELETE_GITHUB_SAVE_INFO';
88
export const DELETE_PERSISTENCE_FILE = 'DELETE_PERSISTENCE_FILE';
9+
export const DELETE_PERSISTENCE_FOLDER_AND_CHILDREN = 'DELETE_PERSISTENCE_FOLDER_AND_CHILDREN';
910
export const DELETE_ALL_GITHUB_SAVE_INFO = 'DELETE_ALL_GITHUB_SAVE_INFO';
1011
export const DELETE_ALL_PERSISTENCE_FILES = 'DELETE_ALL_PERSISTENCE_FILES';
1112
export const UPDATE_PERSISTENCE_FILE_PATH_AND_NAME_BY_PATH =

src/commons/sagas/PersistenceSaga.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1199,13 +1199,20 @@ export function* persistenceSaga(): SagaIterator {
11991199
}
12001200
yield call(ensureInitialisedAndAuthorised);
12011201
yield call(deleteFileOrFolder, persistenceFile.id);
1202-
yield put(actions.deletePersistenceFile(persistenceFile));
1202+
yield put(actions.deletePersistenceFolderAndChildren(persistenceFile));
12031203
yield call(store.dispatch, actions.updateRefreshFileViewKey());
12041204
yield call(
12051205
showSuccessMessage,
12061206
`Folder ${persistenceFile.name} successfully deleted from Google Drive.`,
12071207
1000
12081208
);
1209+
// Check if user deleted the whole folder
1210+
const updatedPersistenceFileArray: PersistenceFile[] = yield select(
1211+
(state: OverallState) => state.fileSystem.persistenceFileArray
1212+
);
1213+
if (updatedPersistenceFileArray.length === 0) {
1214+
yield put(actions.playgroundUpdatePersistenceFile(undefined));
1215+
}
12091216
} catch (ex) {
12101217
console.error(ex);
12111218
yield call(showWarningMessage, `Error while deleting folder.`, 1000);

0 commit comments

Comments
 (0)