Skip to content

Commit fb238b0

Browse files
committed
Add checks to update lastSaved for playground persistenceFile on appropriate save/save as
1 parent cac7c80 commit fb238b0

File tree

4 files changed

+80
-21
lines changed

4 files changed

+80
-21
lines changed

src/commons/fileSystemView/FileSystemViewDirectoryNode.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import React from 'react';
66
import { useDispatch } from 'react-redux';
77
import { githubCreateFile, githubDeleteFolder } from 'src/features/github/GitHubActions';
88
import {
9+
persistenceCreateFile,
910
persistenceCreateFolder,
1011
persistenceDeleteFolder
1112
} from 'src/features/persistence/PersistenceActions';
@@ -123,7 +124,7 @@ const FileSystemViewDirectoryNode: React.FC<Props> = ({
123124
if (err) {
124125
console.error(err);
125126
}
126-
// dispatch(persistenceCreateFile(newFilePath));
127+
dispatch(persistenceCreateFile(newFilePath));
127128
dispatch(githubCreateFile(newFilePath));
128129
forceRefreshFileSystemViewList();
129130
});
@@ -172,7 +173,7 @@ const FileSystemViewDirectoryNode: React.FC<Props> = ({
172173
// }
173174
// informUserGithubCannotCreateFolder();
174175
// dispatch(enableFileSystemContextMenus());
175-
// forceRefreshFileSystemViewList();
176+
forceRefreshFileSystemViewList();
176177
});
177178
});
178179
};

src/commons/sagas/GitHubPersistenceSaga.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ function* githubSaveAll(): any {
309309
}
310310

311311
function* githubCreateFile({ payload }: ReturnType<typeof actions.githubCreateFile>): any {
312-
yield call(store.dispatch, actions.disableFileSystemContextMenus());
312+
// yield call(store.dispatch, actions.disableFileSystemContextMenus());
313313

314314
const filePath = payload;
315315

@@ -421,7 +421,7 @@ function* githubDeleteFile({ payload }: ReturnType<typeof actions.githubDeleteFi
421421
}
422422

423423
function* githubDeleteFolder({ payload }: ReturnType<typeof actions.githubDeleteFolder>): any {
424-
yield call(store.dispatch, actions.disableFileSystemContextMenus());
424+
//yield call(store.dispatch, actions.disableFileSystemContextMenus());
425425

426426
const filePath = payload;
427427

src/commons/sagas/PersistenceSaga.tsx

Lines changed: 56 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ import {
4242
showSuccessMessage,
4343
showWarningMessage
4444
} from '../utils/notifications/NotificationsHelper';
45-
import { filePathRegex } from '../utils/PersistenceHelper';
45+
import { areAllFilesSavedGoogleDrive, filePathRegex } from '../utils/PersistenceHelper';
4646
import { AsyncReturnType } from '../utils/TypeHelper';
4747
import { EditorTabState } from '../workspace/WorkspaceTypes';
4848
import { safeTakeEvery as takeEvery, safeTakeLatest as takeLatest } from './SafeEffects';
@@ -299,7 +299,6 @@ export function* persistenceSaga(): SagaIterator {
299299
});
300300

301301
yield takeLatest(PERSISTENCE_SAVE_FILE_AS, function* (): any {
302-
// TODO wrap first part in try catch finally block
303302
let toastKey: string | undefined;
304303
const persistenceFileArray: PersistenceFile[] = yield select(
305304
(state: OverallState) => state.fileSystem.persistenceFileArray
@@ -425,6 +424,21 @@ export function* persistenceSaga(): SagaIterator {
425424
);
426425
yield call(writeFileRecursively, fileSystem, localFileTarget.path!, code);
427426
yield call(store.dispatch, actions.updateRefreshFileViewKey());
427+
428+
// Check if all files are now updated
429+
const updatedPersistenceFileArray: PersistenceFile[] = yield select(
430+
(state: OverallState) => state.fileSystem.persistenceFileArray
431+
);
432+
if (areAllFilesSavedGoogleDrive(updatedPersistenceFileArray)) {
433+
yield put(
434+
actions.playgroundUpdatePersistenceFolder({
435+
id: currPersistenceFile.id,
436+
name: currPersistenceFile.name,
437+
parentId: currPersistenceFile.parentId,
438+
lastSaved: new Date()
439+
})
440+
);
441+
}
428442
} else {
429443
yield call(updateFile, pickedFile.id, pickedFile.name, MIME_SOURCE, code, config);
430444
}
@@ -561,6 +575,7 @@ export function* persistenceSaga(): SagaIterator {
561575
let toastKey: string | undefined;
562576

563577
try {
578+
yield call(ensureInitialisedAndAuthorised);
564579
const [currFolderObject] = yield select((state: OverallState) => [
565580
state.playground.persistenceFile
566581
]);
@@ -806,6 +821,12 @@ export function* persistenceSaga(): SagaIterator {
806821
throw new Error('this file is not in persistenceFileArray: ' + currFullFilePath);
807822
}
808823

824+
if (!currPersistenceFile.lastEdit || (currPersistenceFile.lastSaved && currPersistenceFile.lastEdit < currPersistenceFile.lastSaved)) {
825+
// no need to update
826+
yield call(console.log, "No need to update", currPersistenceFile);
827+
continue;
828+
}
829+
809830
if (!currPersistenceFile.id || !currPersistenceFile.parentId) {
810831
// get folder
811832
throw new Error('this file does not have id/parentId: ' + currFullFilePath);
@@ -882,7 +903,6 @@ export function* persistenceSaga(): SagaIterator {
882903
let toastKey: string | undefined;
883904

884905
const [currFolderObject] = yield select(
885-
// TODO resolve type here?
886906
(state: OverallState) => [state.playground.persistenceFile]
887907
);
888908

@@ -941,6 +961,22 @@ export function* persistenceSaga(): SagaIterator {
941961
`${currPersistenceFile.name} successfully saved to Google Drive.`,
942962
1000
943963
);
964+
965+
// Check if all files are now updated
966+
const updatedPersistenceFileArray: PersistenceFile[] = yield select(
967+
(state: OverallState) => state.fileSystem.persistenceFileArray
968+
);
969+
if (areAllFilesSavedGoogleDrive(updatedPersistenceFileArray)) {
970+
yield put(
971+
actions.playgroundUpdatePersistenceFolder({
972+
id: currFolderObject.id,
973+
name: currFolderObject.name,
974+
parentId: currFolderObject.parentId,
975+
lastSaved: new Date()
976+
})
977+
);
978+
}
979+
944980
return;
945981
}
946982

@@ -968,13 +1004,12 @@ export function* persistenceSaga(): SagaIterator {
9681004
yield takeEvery(
9691005
PERSISTENCE_CREATE_FILE,
9701006
function* ({ payload }: ReturnType<typeof actions.persistenceCreateFile>) {
971-
yield call(store.dispatch, actions.disableFileSystemContextMenus());
972-
9731007
try {
1008+
yield call(store.dispatch, actions.disableFileSystemContextMenus());
9741009
const newFilePath = payload;
9751010
yield call(console.log, 'create file ', newFilePath);
9761011

977-
// look for parent folder persistenceFile TODO modify action so name is supplied?
1012+
// look for parent folder persistenceFile
9781013
const regexResult = filePathRegex.exec(newFilePath)!;
9791014
const parentFolderPath = regexResult ? regexResult[1].slice(0, -1) : undefined;
9801015
if (!parentFolderPath) {
@@ -992,6 +1027,7 @@ export function* persistenceSaga(): SagaIterator {
9921027
yield call(console.log, 'parent pers file missing');
9931028
return;
9941029
}
1030+
yield call(ensureInitialisedAndAuthorised);
9951031

9961032
yield call(
9971033
console.log,
@@ -1042,9 +1078,8 @@ export function* persistenceSaga(): SagaIterator {
10421078
yield takeEvery(
10431079
PERSISTENCE_CREATE_FOLDER,
10441080
function* ({ payload }: ReturnType<typeof actions.persistenceCreateFolder>) {
1045-
yield call(store.dispatch, actions.disableFileSystemContextMenus());
1046-
10471081
try {
1082+
yield call(store.dispatch, actions.disableFileSystemContextMenus());
10481083
const newFolderPath = payload;
10491084
yield call(console.log, 'create folder ', newFolderPath);
10501085

@@ -1067,6 +1102,7 @@ export function* persistenceSaga(): SagaIterator {
10671102
yield call(console.log, 'parent pers file missing');
10681103
return;
10691104
}
1105+
yield call(ensureInitialisedAndAuthorised);
10701106

10711107
yield call(
10721108
console.log,
@@ -1090,7 +1126,8 @@ export function* persistenceSaga(): SagaIterator {
10901126
path: newFolderPath,
10911127
id: newFolderId,
10921128
name: newFolderName,
1093-
parentId: parentFolderId
1129+
parentId: parentFolderId,
1130+
isFolder: true
10941131
})
10951132
);
10961133
yield call(store.dispatch, actions.updateRefreshFileViewKey());
@@ -1111,9 +1148,8 @@ export function* persistenceSaga(): SagaIterator {
11111148
yield takeEvery(
11121149
PERSISTENCE_DELETE_FILE,
11131150
function* ({ payload }: ReturnType<typeof actions.persistenceDeleteFile>) {
1114-
yield call(store.dispatch, actions.disableFileSystemContextMenus());
1115-
11161151
try {
1152+
yield call(store.dispatch, actions.disableFileSystemContextMenus());
11171153
const filePath = payload;
11181154
yield call(console.log, 'delete file ', filePath);
11191155

@@ -1126,6 +1162,7 @@ export function* persistenceSaga(): SagaIterator {
11261162
yield call(console.log, 'cannot find pers file for ', filePath);
11271163
return;
11281164
}
1165+
yield call(ensureInitialisedAndAuthorised);
11291166
yield call(deleteFileOrFolder, persistenceFile.id); // assume this succeeds all the time? TODO
11301167
yield put(actions.deletePersistenceFile(persistenceFile));
11311168
yield call(store.dispatch, actions.updateRefreshFileViewKey());
@@ -1146,9 +1183,8 @@ export function* persistenceSaga(): SagaIterator {
11461183
yield takeEvery(
11471184
PERSISTENCE_DELETE_FOLDER,
11481185
function* ({ payload }: ReturnType<typeof actions.persistenceDeleteFolder>) {
1149-
yield call(store.dispatch, actions.disableFileSystemContextMenus());
1150-
11511186
try {
1187+
yield call(store.dispatch, actions.disableFileSystemContextMenus());
11521188
const folderPath = payload;
11531189
yield call(console.log, 'delete folder ', folderPath);
11541190

@@ -1161,6 +1197,7 @@ export function* persistenceSaga(): SagaIterator {
11611197
yield call(console.log, 'cannot find pers file');
11621198
return;
11631199
}
1200+
yield call(ensureInitialisedAndAuthorised);
11641201
yield call(deleteFileOrFolder, persistenceFile.id);
11651202
yield put(actions.deletePersistenceFile(persistenceFile));
11661203
yield call(store.dispatch, actions.updateRefreshFileViewKey());
@@ -1183,9 +1220,8 @@ export function* persistenceSaga(): SagaIterator {
11831220
function* ({
11841221
payload: { oldFilePath, newFilePath }
11851222
}: ReturnType<typeof actions.persistenceRenameFile>) {
1186-
yield call(store.dispatch, actions.disableFileSystemContextMenus());
1187-
11881223
try {
1224+
yield call(store.dispatch, actions.disableFileSystemContextMenus());
11891225
yield call(console.log, 'rename file ', oldFilePath, ' to ', newFilePath);
11901226

11911227
// look for file
@@ -1198,6 +1234,8 @@ export function* persistenceSaga(): SagaIterator {
11981234
return;
11991235
}
12001236

1237+
yield call(ensureInitialisedAndAuthorised);
1238+
12011239
// new name TODO: modify action so name is supplied?
12021240
const regexResult = filePathRegex.exec(newFilePath)!;
12031241
const newFileName = regexResult[2] + regexResult[3];
@@ -1229,9 +1267,8 @@ export function* persistenceSaga(): SagaIterator {
12291267
function* ({
12301268
payload: { oldFolderPath, newFolderPath }
12311269
}: ReturnType<typeof actions.persistenceRenameFolder>) {
1232-
yield call(store.dispatch, actions.disableFileSystemContextMenus());
1233-
12341270
try {
1271+
yield call(store.dispatch, actions.disableFileSystemContextMenus());
12351272
yield call(console.log, 'rename folder ', oldFolderPath, ' to ', newFolderPath);
12361273

12371274
// look for folder
@@ -1244,6 +1281,8 @@ export function* persistenceSaga(): SagaIterator {
12441281
return;
12451282
}
12461283

1284+
yield call(ensureInitialisedAndAuthorised);
1285+
12471286
// new name TODO: modify action so name is supplied?
12481287
const regexResult = filePathRegex.exec(newFolderPath)!;
12491288
const newFolderName = regexResult[2] + regexResult[3];

src/commons/utils/PersistenceHelper.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { PersistenceFile } from '../../features/persistence/PersistenceTypes';
2+
13
/**
24
* Regex to get full parent path of a file path, and filename with file extension.
35
* Some examples of calling exec:
@@ -9,3 +11,20 @@
911
* 'asdf' -> ['asdf', undefined, 'asdf', '']
1012
*/
1113
export const filePathRegex = /^(.*[\\/])?(\.*.*?)(\.[^.]+?|)$/;
14+
15+
/**
16+
* Checks if any persistenceFile in a given persistenceFileArray
17+
* has a lastEdit that is more recent than lastSaved.
18+
* @param pf persistenceFileArray.
19+
* @returns boolean representing whether any file has yet to be updated.
20+
*/
21+
export const areAllFilesSavedGoogleDrive = (pf: PersistenceFile[]) => {
22+
for (const currPersistenceFile of pf) {
23+
if (!currPersistenceFile.lastEdit || (currPersistenceFile.lastSaved && currPersistenceFile.lastEdit < currPersistenceFile.lastSaved)) {
24+
continue;
25+
} else {
26+
return false;
27+
}
28+
}
29+
return true;
30+
}

0 commit comments

Comments
 (0)