Skip to content

Commit e18cfe9

Browse files
added case where user saves as into the same folder that is open
1 parent 128a34a commit e18cfe9

File tree

3 files changed

+49
-18
lines changed

3 files changed

+49
-18
lines changed

src/commons/gitHubOverlay/FileExplorerDialog.tsx

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,14 @@ const FileExplorerDialog: React.FC<FileExplorerDialogProps> = props => {
138138
);
139139

140140
if (canBeSaved) {
141+
const persistenceFile = getPersistenceFile('');
142+
if (persistenceFile === undefined) {
143+
throw new Error("persistence file not found for this filepath: " + '');
144+
}
145+
const parentFolderPath = persistenceFile.parentFolderPath;
146+
if (parentFolderPath === undefined) {
147+
throw new Error("repository name or parentfolderpath not found for this persistencefile: " + persistenceFile);
148+
}
141149
if (saveType === 'Overwrite' && (await checkIfUserAgreesToPerformOverwritingSave())) {
142150
performOverwritingSaveForSaveAs(
143151
props.octokit,
@@ -147,19 +155,12 @@ const FileExplorerDialog: React.FC<FileExplorerDialogProps> = props => {
147155
githubName,
148156
githubEmail,
149157
commitMessage,
150-
props.editorContent
158+
props.editorContent,
159+
parentFolderPath
151160
);
152161
}
153162

154163
if (saveType === 'Create') {
155-
const persistenceFile = getPersistenceFile(filePath);
156-
if (persistenceFile === undefined) {
157-
throw new Error("persistence file not found for this filepath: " + filePath);
158-
}
159-
const parentFolderPath = persistenceFile.parentFolderPath;
160-
if (parentFolderPath === undefined) {
161-
throw new Error("repository name or parentfolderpath not found for this persistencefile: " + persistenceFile);
162-
}
163164
performCreatingSave(
164165
props.octokit,
165166
githubLoginID,

src/commons/sagas/GitHubPersistenceSaga.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { getGitHubOctokitInstance } from '../../features/github/GitHubUtils';
2222
import { store } from '../../pages/createStore';
2323
import { OverallState } from '../application/ApplicationTypes';
2424
import { LOGIN_GITHUB, LOGOUT_GITHUB } from '../application/types/SessionTypes';
25-
import { getPersistenceFile, retrieveFilesInWorkspaceAsRecord } from '../fileSystem/FileSystemUtils';
25+
import { getPersistenceFile, retrieveFilesInWorkspaceAsRecord} from '../fileSystem/FileSystemUtils';
2626
import FileExplorerDialog, { FileExplorerDialogProps } from '../gitHubOverlay/FileExplorerDialog';
2727
import RepositoryDialog, { RepositoryDialogProps } from '../gitHubOverlay/RepositoryDialog';
2828
import { actions } from '../utils/ActionsHelper';
@@ -179,6 +179,7 @@ function* githubSaveFileAs(): any {
179179
type ListForAuthenticatedUserData = GetResponseDataTypeFromEndpointMethod<
180180
typeof octokit.repos.listForAuthenticatedUser
181181
>;
182+
182183
const userRepos: ListForAuthenticatedUserData = yield call(
183184
async () =>
184185
await octokit.paginate(octokit.repos.listForAuthenticatedUser, {
@@ -315,7 +316,7 @@ function* githubCreateFile({ payload }: ReturnType<typeof actions.githubCreateFi
315316
const githubLoginId = authUser.data.login;
316317
const persistenceFile = getPersistenceFile('');
317318
if (persistenceFile === undefined) {
318-
throw new Error("persistencefile not found for this filepath: " + filePath);
319+
throw new Error("persistencefile not found for this filepath: " + '');
319320
}
320321
const repoName = persistenceFile.repoName;
321322
const githubEmail = authUser.data.email;
@@ -418,9 +419,9 @@ function* githubDeleteFolder({ payload }: ReturnType<typeof actions.githubDelete
418419
const authUser: GetAuthenticatedResponse = yield call(octokit.users.getAuthenticated);
419420

420421
const githubLoginId = authUser.data.login;
421-
const persistenceFile = getPersistenceFile(filePath);
422+
const persistenceFile = getPersistenceFile('');
422423
if (persistenceFile === undefined) {
423-
throw new Error("persistence file not found for this filepath: " + filePath);
424+
throw new Error("persistence file not found for this filepath: " + '');
424425
}
425426
const repoName = persistenceFile.repoName;
426427
const parentFolderPath = persistenceFile.parentFolderPath;
@@ -515,9 +516,9 @@ function* githubRenameFolder({ payload }: ReturnType<typeof actions.githubRename
515516
const authUser: GetAuthenticatedResponse = yield call(octokit.users.getAuthenticated);
516517

517518
const githubLoginId = authUser.data.login;
518-
const persistenceFile = getPersistenceFile(oldFilePath);
519+
const persistenceFile = getPersistenceFile('');
519520
if (persistenceFile === undefined) {
520-
throw new Error("persistence file not found for this filepath: " + oldFilePath);
521+
throw new Error("persistence file not found for this filepath: " + '');
521522
}
522523
const repoName = persistenceFile.repoName;
523524
const parentFolderPath = persistenceFile.parentFolderPath;

src/features/github/GitHubUtils.tsx

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -542,11 +542,11 @@ export async function performMultipleOverwritingSave(
542542
octokit,
543543
repoOwner,
544544
repoName,
545-
filePath,
545+
filePath.slice(12),
546546
githubName,
547547
githubEmail,
548548
changes.commitMessage,
549-
changes.files[filePath].slice(12),
549+
changes.files[filePath],
550550
parentFolderPath
551551
);
552552
}
@@ -568,7 +568,8 @@ export async function performOverwritingSaveForSaveAs(
568568
githubName: string | null,
569569
githubEmail: string | null,
570570
commitMessage: string,
571-
content: string, // path of the parent of the opened subfolder in github
571+
content: string,
572+
parentFolderPath: string
572573
) {
573574
if (octokit === undefined) return;
574575

@@ -602,6 +603,34 @@ export async function performOverwritingSaveForSaveAs(
602603
}
603604

604605
const sha = files.sha;
606+
console.log("/playground/" + filePath.slice(parentFolderPath.length));
607+
const persistenceFile = getPersistenceFile("/playground/" + filePath.slice(parentFolderPath.length));
608+
if (persistenceFile !== undefined) {
609+
610+
//case where user saves as into the same folder
611+
const parentFolderPath = persistenceFile.parentFolderPath;
612+
const filePath = persistenceFile.path;
613+
if (parentFolderPath === undefined || filePath === undefined) {
614+
throw new Error("parentfolderpath or filepath not found for this persistencefile: " + persistenceFile);
615+
}
616+
617+
const fileSystem: FSModule | null = store.getState().fileSystem.inBrowserFileSystem;
618+
if (fileSystem === null) {
619+
console.log("no filesystem!");
620+
throw new Error("No filesystem");
621+
}
622+
623+
writeFileRecursively(fileSystem, filePath, content);
624+
625+
store.dispatch(actions.addGithubSaveInfo({
626+
id: '',
627+
name: '',
628+
repoName: repoName,
629+
path: filePath,
630+
lastSaved: new Date(),
631+
parentFolderPath: parentFolderPath
632+
}));
633+
}
605634

606635
await octokit.repos.createOrUpdateFileContents({
607636
owner: repoOwner,

0 commit comments

Comments
 (0)