Skip to content

Commit 8754f60

Browse files
added instantaneous syncing for github
1 parent 247a376 commit 8754f60

File tree

6 files changed

+308
-48
lines changed

6 files changed

+308
-48
lines changed

src/commons/fileSystem/FileSystemReducer.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,7 @@ export const FileSystemReducer: Reducer<FileSystemState, SourceActionType> = cre
5454
state.persistenceFileArray = persistenceFileArray;
5555
})
5656
.addCase(deleteGithubSaveInfo, (state, action) => { // TODO rewrite - refer to deletePersistenceFile below
57-
const newPersistenceFileArray = state['persistenceFileArray'].filter(e => {
58-
return e.path != action.payload.path &&
59-
e.lastSaved != action.payload.lastSaved &&
60-
e.repoName != action.payload.repoName
61-
});
57+
const newPersistenceFileArray = state['persistenceFileArray'].filter(e => e.path !== action.payload.path);
6258
const isGDriveSyncing = action.payload.id ? true: false;
6359
if (isGDriveSyncing) {
6460
const newPersFile = { id: action.payload.id, path: action.payload.path, repoName: '', name: action.payload.name};
@@ -108,10 +104,12 @@ export const FileSystemReducer: Reducer<FileSystemState, SourceActionType> = cre
108104
})
109105
.addCase(updatePersistenceFolderPathAndNameByPath, (state, action) => {
110106
const filesState = state['persistenceFileArray'];
111-
const persistenceFileFindIndex = filesState.findIndex(e => e.path === action.payload.oldPath);
112-
if (persistenceFileFindIndex === -1) {
113-
return;
114-
}
107+
//const persistenceFileFindIndex = filesState.findIndex(e => e.path === action.payload.oldPath);
108+
console.log(action.payload);
109+
filesState.forEach(e => console.log(e.path));
110+
// if (persistenceFileFindIndex === -1) {
111+
// return;
112+
// }
115113
// get current level of folder
116114
const regexResult = /^(.*[\\\/])?(\.*.*?)(\.[^.]+?|)$/.exec(action.payload.newPath)!;
117115

src/commons/fileSystemView/FileSystemViewFileName.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
renameEditorTabsForDirectory
1212
} from '../workspace/WorkspaceActions';
1313
import { WorkspaceLocation } from '../workspace/WorkspaceTypes';
14+
import { githubRenameFile, githubRenameFolder } from 'src/features/github/GitHubActions';
1415

1516
type Props = {
1617
workspaceLocation: WorkspaceLocation;
@@ -71,9 +72,11 @@ const FileSystemViewFileName: React.FC<Props> = ({
7172

7273
if (isDirectory) {
7374
dispatch(persistenceRenameFolder({oldFolderPath: oldPath, newFolderPath: newPath}));
75+
dispatch(githubRenameFolder(oldPath, newPath));
7476
dispatch(renameEditorTabsForDirectory(workspaceLocation, oldPath, newPath));
7577
} else {
7678
dispatch(persistenceRenameFile({oldFilePath: oldPath, newFilePath: newPath}));
79+
dispatch(githubRenameFile(oldPath, newPath));
7780
dispatch(renameEditorTabForFile(workspaceLocation, oldPath, newPath));
7881
}
7982
refreshDirectory();

src/commons/sagas/GitHubPersistenceSaga.ts

Lines changed: 90 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ import {
1313
GITHUB_SAVE_FILE_AS,
1414
GITHUB_CREATE_FILE,
1515
GITHUB_DELETE_FILE,
16-
GITHUB_DELETE_FOLDER
16+
GITHUB_DELETE_FOLDER,
17+
GITHUB_RENAME_FILE,
18+
GITHUB_RENAME_FOLDER,
1719
} from '../../features/github/GitHubTypes';
1820
import * as GitHubUtils from '../../features/github/GitHubUtils';
1921
import { getGitHubOctokitInstance } from '../../features/github/GitHubUtils';
@@ -32,14 +34,15 @@ import { EditorTabState } from '../workspace/WorkspaceTypes';
3234
export function* GitHubPersistenceSaga(): SagaIterator {
3335
yield takeLatest(LOGIN_GITHUB, githubLoginSaga);
3436
yield takeLatest(LOGOUT_GITHUB, githubLogoutSaga);
35-
3637
yield takeLatest(GITHUB_OPEN_FILE, githubOpenFile);
3738
yield takeLatest(GITHUB_SAVE_FILE, githubSaveFile);
3839
yield takeLatest(GITHUB_SAVE_FILE_AS, githubSaveFileAs);
3940
yield takeLatest(GITHUB_SAVE_ALL, githubSaveAll);
4041
yield takeLatest(GITHUB_CREATE_FILE, githubCreateFile);
4142
yield takeLatest(GITHUB_DELETE_FILE, githubDeleteFile);
4243
yield takeLatest(GITHUB_DELETE_FOLDER, githubDeleteFolder);
44+
yield takeLatest(GITHUB_RENAME_FILE, githubRenameFile);
45+
yield takeLatest(GITHUB_RENAME_FOLDER, githubRenameFolder);
4346
}
4447

4548
function* githubLoginSaga() {
@@ -308,13 +311,12 @@ function* githubDeleteFile({ payload }: ReturnType<typeof actions.githubDeleteFi
308311
const authUser: GetAuthenticatedResponse = yield call(octokit.users.getAuthenticated);
309312

310313
const githubLoginId = authUser.data.login;
311-
const githubSaveInfo = getGithubSaveInfo();
312-
const repoName = githubSaveInfo.repoName;
313-
const lastSaved = githubSaveInfo.lastSaved;
314+
const repoName = getGithubSaveInfo().repoName;
314315
const githubEmail = authUser.data.email;
315316
const githubName = authUser.data.name;
316317
const commitMessage = 'Changes made from Source Academy';
317318

319+
318320
if (repoName === '') {
319321
yield call(console.log, "not synced to github");
320322
return;
@@ -329,14 +331,6 @@ function* githubDeleteFile({ payload }: ReturnType<typeof actions.githubDeleteFi
329331
githubName,
330332
commitMessage,
331333
);
332-
333-
const persistenceFileArray = store.getState().fileSystem.persistenceFileArray;
334-
const persistenceFile = persistenceFileArray.find(e =>
335-
e.repoName === repoName &&
336-
e.path === filePath &&
337-
e.lastSaved === lastSaved) || {id: '', name: ''};
338-
store.dispatch(actions.deleteGithubSaveInfo(persistenceFile));
339-
340334

341335
yield call(store.dispatch, actions.enableFileSystemContextMenus());
342336
yield call(store.dispatch, actions.updateRefreshFileViewKey());
@@ -375,7 +369,89 @@ function* githubDeleteFolder({ payload }: ReturnType<typeof actions.githubDelete
375369
githubName,
376370
commitMessage
377371
);
378-
372+
373+
yield call(store.dispatch, actions.enableFileSystemContextMenus());
374+
yield call(store.dispatch, actions.updateRefreshFileViewKey());
375+
}
376+
377+
function* githubRenameFile({ payload }: ReturnType<typeof actions.githubRenameFile>): any {
378+
yield call(store.dispatch, actions.disableFileSystemContextMenus());
379+
380+
const newFilePath = payload.newFilePath;
381+
const oldFilePath = payload.oldFilePath;
382+
383+
const octokit = getGitHubOctokitInstance();
384+
if (octokit === undefined) return;
385+
386+
type GetAuthenticatedResponse = GetResponseTypeFromEndpointMethod<
387+
typeof octokit.users.getAuthenticated
388+
>;
389+
const authUser: GetAuthenticatedResponse = yield call(octokit.users.getAuthenticated);
390+
391+
const githubLoginId = authUser.data.login;
392+
const githubSaveInfo = getGithubSaveInfo();
393+
const repoName = githubSaveInfo.repoName;
394+
const githubEmail = authUser.data.email;
395+
const githubName = authUser.data.name;
396+
const commitMessage = 'Changes made from Source Academy';
397+
398+
if (repoName === '' || repoName === undefined) {
399+
yield call(console.log, "not synced to github");
400+
return;
401+
}
402+
403+
GitHubUtils.performFileRenaming(
404+
octokit,
405+
githubLoginId,
406+
repoName,
407+
oldFilePath.slice(12),
408+
githubName,
409+
githubEmail,
410+
commitMessage,
411+
newFilePath.slice(12)
412+
)
413+
414+
yield call(store.dispatch, actions.enableFileSystemContextMenus());
415+
yield call(store.dispatch, actions.updateRefreshFileViewKey());
416+
}
417+
418+
function* githubRenameFolder({ payload }: ReturnType<typeof actions.githubRenameFile>): any {
419+
yield call(store.dispatch, actions.disableFileSystemContextMenus());
420+
421+
const newFilePath = payload.newFilePath;
422+
const oldFilePath = payload.oldFilePath;
423+
424+
const octokit = getGitHubOctokitInstance();
425+
if (octokit === undefined) return;
426+
427+
type GetAuthenticatedResponse = GetResponseTypeFromEndpointMethod<
428+
typeof octokit.users.getAuthenticated
429+
>;
430+
const authUser: GetAuthenticatedResponse = yield call(octokit.users.getAuthenticated);
431+
432+
const githubLoginId = authUser.data.login;
433+
const githubSaveInfo = getGithubSaveInfo();
434+
const repoName = githubSaveInfo.repoName;
435+
const githubEmail = authUser.data.email;
436+
const githubName = authUser.data.name;
437+
const commitMessage = 'Changes made from Source Academy';
438+
439+
if (repoName === '' || repoName === undefined) {
440+
yield call(console.log, "not synced to github");
441+
return;
442+
}
443+
444+
GitHubUtils.performFolderRenaming(
445+
octokit,
446+
githubLoginId,
447+
repoName,
448+
oldFilePath.slice(12),
449+
githubName,
450+
githubEmail,
451+
commitMessage,
452+
newFilePath.slice(12)
453+
)
454+
379455
yield call(store.dispatch, actions.enableFileSystemContextMenus());
380456
yield call(store.dispatch, actions.updateRefreshFileViewKey());
381457
}

src/features/github/GitHubActions.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import {
77
GITHUB_SAVE_FILE,
88
GITHUB_SAVE_FILE_AS,
99
GITHUB_DELETE_FILE,
10-
GITHUB_DELETE_FOLDER} from './GitHubTypes';
10+
GITHUB_DELETE_FOLDER,
11+
GITHUB_RENAME_FOLDER,
12+
GITHUB_RENAME_FILE} from './GitHubTypes';
1113

1214
export const githubOpenFile = createAction(GITHUB_OPEN_FILE, () => ({ payload: {} }));
1315

@@ -22,3 +24,7 @@ export const githubCreateFile = createAction(GITHUB_CREATE_FILE, (filePath: stri
2224
export const githubDeleteFile = createAction(GITHUB_DELETE_FILE, (filePath: string) => ({ payload: filePath }));
2325

2426
export const githubDeleteFolder = createAction(GITHUB_DELETE_FOLDER, (filePath: string) => ({ payload: filePath}));
27+
28+
export const githubRenameFile = createAction(GITHUB_RENAME_FILE, (oldFilePath: string, newFilePath: string) => ({ payload: {oldFilePath, newFilePath} }));
29+
30+
export const githubRenameFolder = createAction(GITHUB_RENAME_FOLDER, (oldFilePath: string, newFilePath: string) => ({ payload: {oldFilePath, newFilePath} }));

src/features/github/GitHubTypes.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ export const GITHUB_SAVE_ALL = 'GITHUB_SAVE_ALL';
55
export const GITHUB_CREATE_FILE = 'GITHUB_CREATE_FILE';
66
export const GITHUB_DELETE_FILE = 'GITHUB_DELETE_FILE';
77
export const GITHUB_DELETE_FOLDER = 'GITHUB_DELETE_FOLDER';
8+
export const GITHUB_RENAME_FOLDER = 'GITHUB_RENAME_FOLDER';
9+
export const GITHUB_RENAME_FILE = 'GITHUB_RENAME_FILE';
810

911
export type GitHubSaveInfo = {
1012
repoName?: string;

0 commit comments

Comments
 (0)