Skip to content

Commit a38e838

Browse files
Merge branch 'folders-public-wip' of github.com:source-academy/frontend into folders-public-wip
2 parents 83ba7ff + 3d3e8bf commit a38e838

File tree

2 files changed

+103
-2
lines changed

2 files changed

+103
-2
lines changed

src/commons/controlBar/ControlBarGoogleDriveButtons.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export const ControlBarGoogleDriveButtons: React.FC<Props> = props => {
6565
label="Save As"
6666
icon={IconNames.SEND_TO}
6767
onClick={props.onClickSaveAs}
68-
isDisabled={props.accessToken ? props.isFolderModeEnabled : true}
68+
isDisabled={props.accessToken ? false : true}
6969
/>
7070
);
7171
const saveAllButton = (

src/commons/sagas/PersistenceSaga.tsx

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,13 @@ export function* persistenceSaga(): SagaIterator {
258258

259259
yield takeLatest(PERSISTENCE_SAVE_FILE_AS, function* (): any {
260260
let toastKey: string | undefined;
261+
const persistenceFileArray: PersistenceFile[] = yield select((state: OverallState) => state.fileSystem.persistenceFileArray);
262+
const [currPersistenceFile] = yield select(
263+
(state: OverallState) => [
264+
state.playground.persistenceFile
265+
]
266+
);
267+
yield call(console.log, "currpersfile ", currPersistenceFile);
261268
try {
262269
yield call(ensureInitialisedAndAuthorised);
263270

@@ -313,6 +320,58 @@ export function* persistenceSaga(): SagaIterator {
313320
if (!reallyOverwrite) {
314321
return;
315322
}
323+
324+
yield call(store.dispatch, actions.disableFileSystemContextMenus());
325+
// Case: Picked a file to overwrite
326+
if (currPersistenceFile && currPersistenceFile.isFolder) {
327+
yield call(console.log, "folder opened, handling save_as differently! overwriting file");
328+
// First case: Chosen location is within TLRF - so need to call methods to update PersistenceFileArray
329+
// Other case: Chosen location is outside TLRF - don't care
330+
331+
yield call(console.log, "curr pers file ", currPersistenceFile, " pickedDir ", pickedDir, " pickedFile ", pickedFile);
332+
const localFileTarget = persistenceFileArray.find(e => e.id === pickedFile.id);
333+
if (localFileTarget) {
334+
toastKey = yield call(showMessage, {
335+
message: `Saving as ${localFileTarget.name}...`,
336+
timeout: 0,
337+
intent: Intent.PRIMARY
338+
});
339+
// identical to just saving a file locally
340+
const fileSystem: FSModule | null = yield select(
341+
(state: OverallState) => state.fileSystem.inBrowserFileSystem
342+
);
343+
if (fileSystem === null) {
344+
yield call(console.log, "no filesystem!");
345+
throw new Error("No filesystem");
346+
}
347+
348+
// Save to GDrive
349+
const [chapter, variant, external] = yield select(
350+
(state: OverallState) => [
351+
state.workspaces.playground.context.chapter,
352+
state.workspaces.playground.context.variant,
353+
state.workspaces.playground.externalLibrary
354+
]
355+
);
356+
const config: IPlaygroundConfig = {
357+
chapter,
358+
variant,
359+
external
360+
};
361+
362+
yield call(updateFile, localFileTarget.id, localFileTarget.name, MIME_SOURCE, code, config);
363+
364+
yield put(actions.addPersistenceFile({ ...localFileTarget, lastSaved: new Date(), lastEdit: undefined }));
365+
yield call(writeFileRecursively, fileSystem, localFileTarget.path!, code);
366+
yield call(store.dispatch, actions.updateRefreshFileViewKey());
367+
}
368+
yield call(
369+
showSuccessMessage,
370+
`${pickedFile.name} successfully saved to Google Drive.`,
371+
1000
372+
);
373+
return;
374+
}
316375
yield put(actions.playgroundUpdatePersistenceFile(pickedFile));
317376
yield put(actions.persistenceSaveFile(pickedFile));
318377
} else {
@@ -340,6 +399,8 @@ export function* persistenceSaga(): SagaIterator {
340399
return;
341400
}
342401

402+
yield call(store.dispatch, actions.disableFileSystemContextMenus());
403+
343404
const config: IPlaygroundConfig = {
344405
chapter,
345406
variant,
@@ -352,7 +413,7 @@ export function* persistenceSaga(): SagaIterator {
352413
intent: Intent.PRIMARY
353414
});
354415

355-
const newFile = yield call(
416+
const newFile: PersistenceFile = yield call(
356417
createFile,
357418
response.value,
358419
saveToDir.id,
@@ -361,6 +422,45 @@ export function* persistenceSaga(): SagaIterator {
361422
config
362423
);
363424

425+
//Case: Chose to save as a new file
426+
if (currPersistenceFile && currPersistenceFile.isFolder) {
427+
yield call(console.log, "folder opened, handling save_as differently! saving as new file");
428+
// First case: Chosen location is within TLRF - so need to call methods to update PersistenceFileArray
429+
// Other case: Chosen location is outside TLRF - don't care
430+
431+
yield call(console.log, "curr persFileArr ", persistenceFileArray, " pickedDir ", pickedDir, " pickedFile ", pickedFile, " saveToDir ", saveToDir);
432+
let needToUpdateLocal = false;
433+
let localFolderTarget: PersistenceFile;
434+
for (let i = 0; i < persistenceFileArray.length; i++) {
435+
if (persistenceFileArray[i].isFolder && persistenceFileArray[i].id === saveToDir.id) {
436+
needToUpdateLocal = true;
437+
localFolderTarget = persistenceFileArray[i];
438+
break;
439+
}
440+
}
441+
442+
if (needToUpdateLocal) {
443+
const fileSystem: FSModule | null = yield select(
444+
(state: OverallState) => state.fileSystem.inBrowserFileSystem
445+
);
446+
if (fileSystem === null) {
447+
yield call(console.log, "no filesystem!");
448+
throw new Error("No filesystem");
449+
}
450+
const newPath = localFolderTarget!.path + "/" + response.value;
451+
yield put(actions.addPersistenceFile({ ...newFile, lastSaved: new Date(), path: newPath }));
452+
yield call(writeFileRecursively, fileSystem, newPath, code);
453+
yield call(store.dispatch, actions.updateRefreshFileViewKey());
454+
}
455+
456+
yield call(
457+
showSuccessMessage,
458+
`${response.value} successfully saved to Google Drive.`,
459+
1000
460+
);
461+
return;
462+
}
463+
364464
yield put(actions.playgroundUpdatePersistenceFile({ ...newFile, lastSaved: new Date() }));
365465
yield call(
366466
showSuccessMessage,
@@ -375,6 +475,7 @@ export function* persistenceSaga(): SagaIterator {
375475
if (toastKey) {
376476
dismiss(toastKey);
377477
}
478+
yield call(store.dispatch, actions.enableFileSystemContextMenus());
378479
}
379480
});
380481

0 commit comments

Comments
 (0)