Skip to content

Commit 9ce24c8

Browse files
committed
Fixes for Save/Save as/Save all for GDrive, add in checks for instant sync GDrive to return when Github is active
1 parent 0e06fb1 commit 9ce24c8

File tree

1 file changed

+61
-11
lines changed

1 file changed

+61
-11
lines changed

src/commons/sagas/PersistenceSaga.tsx

Lines changed: 61 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { store } from '../../pages/createStore';
2424
import { OverallState } from '../application/ApplicationTypes';
2525
import { LOGIN_GOOGLE, LOGOUT_GOOGLE } from '../application/types/SessionTypes';
2626
import {
27+
isGithubSyncing,
2728
retrieveFilesInWorkspaceAsRecord,
2829
rmFilesInDirRecursively,
2930
writeFileRecursively
@@ -569,7 +570,7 @@ export function* persistenceSaga(): SagaIterator {
569570

570571

571572
// Case where playground PersistenceFile is in single file mode
572-
// Does nothing
573+
// Does nothing extra
573574
yield call(
574575
showSuccessMessage,
575576
`${response.value} successfully saved to Google Drive.`,
@@ -627,17 +628,23 @@ export function* persistenceSaga(): SagaIterator {
627628
};
628629

629630
if (!currFolderObject || !(currFolderObject as PersistenceFile).isFolder) {
631+
yield call(console.log, 'here');
630632
// Check if there is only a single top level folder
631633
const testPaths: Set<string> = new Set();
634+
let fileExistsInTopLevel = false;
632635
Object.keys(currFiles).forEach(e => {
633636
const regexResult = filePathRegex.exec(e)!;
637+
const testStr = regexResult![1].slice('/playground/'.length, -1).split('/')[0];
638+
if (testStr === '') {
639+
fileExistsInTopLevel = true;
640+
}
634641
testPaths.add(regexResult![1].slice('/playground/'.length, -1).split('/')[0]); //TODO hardcoded playground
635642
});
636-
if (testPaths.size !== 1) {
643+
if (testPaths.size !== 1 || fileExistsInTopLevel) {
637644
yield call(showSimpleErrorDialog, {
638645
title: 'Unable to Save All',
639646
contents: (
640-
<p>There must be exactly one top level folder present in order to use Save All.</p>
647+
<p>There must be only exactly one non-empty top level folder present to use Save All.</p>
641648
),
642649
label: 'OK'
643650
});
@@ -937,12 +944,24 @@ export function* persistenceSaga(): SagaIterator {
937944
);
938945

939946
try {
940-
if (activeEditorTabIndex === null && !playgroundPersistenceFile.isFolder) {
941-
yield call(showWarningMessage, `Please have ${name} open as the active editor tab.`, 1000);
947+
if (activeEditorTabIndex === null) {
948+
if (!playgroundPersistenceFile) yield call(showWarningMessage, `Please have an editor tab open.`, 1000);
949+
else if (!playgroundPersistenceFile.isFolder) {
950+
yield call(showWarningMessage, `Please have ${name} open as the active editor tab.`, 1000);
951+
} else {
952+
yield call(showWarningMessage, `Please have the file you want to save open as the active editor tab.`, 1000);
953+
}
942954
return;
943955
}
944956
const code = editorTabs[activeEditorTabIndex].value;
945957

958+
// check if editor is correct for single file mode
959+
if (playgroundPersistenceFile && !playgroundPersistenceFile.isFolder &&
960+
(editorTabs[activeEditorTabIndex] as EditorTabState).filePath !== playgroundPersistenceFile.path) {
961+
yield call(showWarningMessage, `Please have ${name} open as the active editor tab.`, 1000);
962+
return;
963+
}
964+
946965
const config: IPlaygroundConfig = {
947966
chapter,
948967
variant,
@@ -1000,11 +1019,6 @@ export function* persistenceSaga(): SagaIterator {
10001019
return;
10011020
}
10021021

1003-
if ((editorTabs[activeEditorTabIndex] as EditorTabState).filePath !== playgroundPersistenceFile.path) {
1004-
yield call(showWarningMessage, `Please have ${name} open as the active editor tab.`, 1000);
1005-
return;
1006-
}
1007-
10081022
toastKey = yield call(showMessage, {
10091023
message: `Saving as ${name}...`,
10101024
timeout: 0,
@@ -1032,6 +1046,8 @@ export function* persistenceSaga(): SagaIterator {
10321046
yield takeEvery(
10331047
PERSISTENCE_CREATE_FILE,
10341048
function* ({ payload }: ReturnType<typeof actions.persistenceCreateFile>) {
1049+
const bailNow: boolean = yield call(isGithubSyncing);
1050+
if (bailNow) return;
10351051
try {
10361052
yield call(store.dispatch, actions.disableFileSystemContextMenus());
10371053
const newFilePath = payload;
@@ -1106,6 +1122,8 @@ export function* persistenceSaga(): SagaIterator {
11061122
yield takeEvery(
11071123
PERSISTENCE_CREATE_FOLDER,
11081124
function* ({ payload }: ReturnType<typeof actions.persistenceCreateFolder>) {
1125+
const bailNow: boolean = yield call(isGithubSyncing);
1126+
if (bailNow) return;
11091127
try {
11101128
yield call(store.dispatch, actions.disableFileSystemContextMenus());
11111129
const newFolderPath = payload;
@@ -1176,6 +1194,8 @@ export function* persistenceSaga(): SagaIterator {
11761194
yield takeEvery(
11771195
PERSISTENCE_DELETE_FILE,
11781196
function* ({ payload }: ReturnType<typeof actions.persistenceDeleteFile>) {
1197+
const bailNow: boolean = yield call(isGithubSyncing);
1198+
if (bailNow) return;
11791199
try {
11801200
yield call(store.dispatch, actions.disableFileSystemContextMenus());
11811201
const filePath = payload;
@@ -1191,9 +1211,19 @@ export function* persistenceSaga(): SagaIterator {
11911211
return;
11921212
}
11931213
yield call(ensureInitialisedAndAuthorised);
1194-
yield call(deleteFileOrFolder, persistenceFile.id); // assume this succeeds all the time? TODO
1214+
yield call(deleteFileOrFolder, persistenceFile.id);
11951215
yield put(actions.deletePersistenceFile(persistenceFile));
11961216
yield call(store.dispatch, actions.updateRefreshFileViewKey());
1217+
1218+
// If the user comes here in single file mode, then the file they deleted
1219+
// must be the file they are tracking.
1220+
const [currFileObject] = yield select((state: OverallState) => [
1221+
state.playground.persistenceFile
1222+
]);
1223+
if (!currFileObject.isFolder) {
1224+
yield put(actions.playgroundUpdatePersistenceFile(undefined));
1225+
}
1226+
11971227
yield call(
11981228
showSuccessMessage,
11991229
`${persistenceFile.name} successfully deleted from Google Drive.`,
@@ -1211,6 +1241,8 @@ export function* persistenceSaga(): SagaIterator {
12111241
yield takeEvery(
12121242
PERSISTENCE_DELETE_FOLDER,
12131243
function* ({ payload }: ReturnType<typeof actions.persistenceDeleteFolder>) {
1244+
const bailNow: boolean = yield call(isGithubSyncing);
1245+
if (bailNow) return;
12141246
try {
12151247
yield call(store.dispatch, actions.disableFileSystemContextMenus());
12161248
const folderPath = payload;
@@ -1255,6 +1287,8 @@ export function* persistenceSaga(): SagaIterator {
12551287
function* ({
12561288
payload: { oldFilePath, newFilePath }
12571289
}: ReturnType<typeof actions.persistenceRenameFile>) {
1290+
const bailNow: boolean = yield call(isGithubSyncing);
1291+
if (bailNow) return;
12581292
try {
12591293
yield call(store.dispatch, actions.disableFileSystemContextMenus());
12601294
yield call(console.log, 'rename file ', oldFilePath, ' to ', newFilePath);
@@ -1275,13 +1309,27 @@ export function* persistenceSaga(): SagaIterator {
12751309
const regexResult = filePathRegex.exec(newFilePath)!;
12761310
const newFileName = regexResult[2] + regexResult[3];
12771311

1312+
// old name
1313+
const regexResult2 = filePathRegex.exec(oldFilePath)!;
1314+
const oldFileName = regexResult2[2] + regexResult2[3];
1315+
12781316
// call gapi
12791317
yield call(renameFileOrFolder, persistenceFile.id, newFileName);
12801318

12811319
// handle pers file
12821320
yield put(
12831321
actions.updatePersistenceFilePathAndNameByPath(oldFilePath, newFilePath, newFileName)
12841322
);
1323+
const [currFileObject] = yield select((state: OverallState) => [
1324+
state.playground.persistenceFile
1325+
]);
1326+
if (currFileObject.name === oldFileName) {
1327+
// update playground PersistenceFile
1328+
yield put(
1329+
actions.playgroundUpdatePersistenceFile({ ...currFileObject, name: newFileName})
1330+
);
1331+
}
1332+
12851333
yield call(store.dispatch, actions.updateRefreshFileViewKey());
12861334
yield call(
12871335
showSuccessMessage,
@@ -1302,6 +1350,8 @@ export function* persistenceSaga(): SagaIterator {
13021350
function* ({
13031351
payload: { oldFolderPath, newFolderPath }
13041352
}: ReturnType<typeof actions.persistenceRenameFolder>) {
1353+
const bailNow: boolean = yield call(isGithubSyncing);
1354+
if (bailNow) return;
13051355
try {
13061356
yield call(store.dispatch, actions.disableFileSystemContextMenus());
13071357
yield call(console.log, 'rename folder ', oldFolderPath, ' to ', newFolderPath);

0 commit comments

Comments
 (0)