Move core logic to core/ inner folder and import changes

This commit is contained in:
Abhas
2018-10-08 08:00:28 +05:30
parent f9c7177bfd
commit 11917f9520
8 changed files with 202 additions and 182 deletions
+60 -48
View File
@@ -20,7 +20,14 @@ import {
OwnerFromGetContributors, OwnerFromGetContributors,
ResponseFromGetContributors, ResponseFromGetContributors,
} from './types'; } from './types';
import { createAppStore, ActionsType, ApplicationState, ActionNames, FilterSteps, FilterStatus } from './store'; import {
createAppStore,
ActionsType,
ApplicationState,
ActionNames,
FilterSteps,
FilterStatus,
} from './store';
import { Store } from 'redux'; import { Store } from 'redux';
// +++ General init +++ // +++ General init +++
@@ -43,20 +50,20 @@ octokit.authenticate({
}); });
// --- End General init --- // --- End General init ---
const defaultStore: Store<ApplicationState, ActionsType> = createAppStore();
class GithubDetectUnusedRepos { class GithubDetectUnusedRepos {
private username: string; private username: string;
reduxStore: Store<ApplicationState, ActionsType>;
constructor(username: string) { constructor(username: string) {
this.username = username; this.username = username;
this.reduxStore = createAppStore();
// Update username in redux store // Update username in redux store
this.reduxStore.dispatch({ defaultStore.dispatch({
type: ActionNames.InitUserName, type: ActionNames.InitUserName,
payload: { payload: {
userName: username userName: username,
} },
}); });
} }
@@ -80,12 +87,12 @@ class GithubDetectUnusedRepos {
// Update all repo names in redux store // Update all repo names in redux store
repos.data.forEach(repoData => { repos.data.forEach(repoData => {
this.reduxStore.dispatch({ defaultStore.dispatch({
type: ActionNames.InitRepo, type: ActionNames.InitRepo,
payload: { payload: {
repoName: repoData.name repoName: repoData.name,
} },
}) });
}); });
const forkedRepoNames = repos.data const forkedRepoNames = repos.data
@@ -98,28 +105,26 @@ class GithubDetectUnusedRepos {
// Update fork filter in redux store // Update fork filter in redux store
repos.data.forEach(repoData => { repos.data.forEach(repoData => {
if (forkedRepoNames.includes(repoData.name)) { if (forkedRepoNames.includes(repoData.name)) {
defaultStore.dispatch({
this.reduxStore.dispatch({
type: ActionNames.AddRepoFilterStatus, type: ActionNames.AddRepoFilterStatus,
payload: { payload: {
repoName: repoData.name, repoName: repoData.name,
filterStep: FilterSteps.forked, filterStep: FilterSteps.forked,
filterState: { filterState: {
status: FilterStatus.pass status: FilterStatus.pass,
} },
} },
}); });
} else { } else {
this.reduxStore.dispatch({ defaultStore.dispatch({
type: ActionNames.AddRepoFilterStatus, type: ActionNames.AddRepoFilterStatus,
payload: { payload: {
repoName: repoData.name, repoName: repoData.name,
filterStep: FilterSteps.forked, filterStep: FilterSteps.forked,
filterState: { filterState: {
status: FilterStatus.fail status: FilterStatus.fail,
} },
} },
}); });
} }
}); });
@@ -217,48 +222,42 @@ class GithubDetectUnusedRepos {
{ {
// Update redux state for these 3 filters // Update redux state for these 3 filters
allRepoWithFlagFromCommitAhead.forEach(tmp => { allRepoWithFlagFromCommitAhead.forEach(tmp => {
this.reduxStore.dispatch( defaultStore.dispatch({
{
type: ActionNames.AddRepoFilterStatus, type: ActionNames.AddRepoFilterStatus,
payload: { payload: {
repoName: tmp.repoName, repoName: tmp.repoName,
filterStep: FilterSteps.eachBranchBehindOrEven, filterStep: FilterSteps.eachBranchBehindOrEven,
filterState: { filterState: {
status: tmp.unused ? FilterStatus.pass : FilterStatus.fail status: tmp.unused ? FilterStatus.pass : FilterStatus.fail,
} },
} },
} });
)
}); });
allRepoWithFlagFromForkContrib.forEach(tmp => { allRepoWithFlagFromForkContrib.forEach(tmp => {
this.reduxStore.dispatch( defaultStore.dispatch({
{
type: ActionNames.AddRepoFilterStatus, type: ActionNames.AddRepoFilterStatus,
payload: { payload: {
repoName: tmp.repoName, repoName: tmp.repoName,
filterStep: FilterSteps.notForkContributor, filterStep: FilterSteps.notForkContributor,
filterState: { filterState: {
status: tmp.unused ? FilterStatus.pass : FilterStatus.fail status: tmp.unused ? FilterStatus.pass : FilterStatus.fail,
} },
} },
} });
)
}); });
allRepoWithFlagFromParentContrib.forEach(tmp => { allRepoWithFlagFromParentContrib.forEach(tmp => {
this.reduxStore.dispatch( defaultStore.dispatch({
{
type: ActionNames.AddRepoFilterStatus, type: ActionNames.AddRepoFilterStatus,
payload: { payload: {
repoName: tmp.repoName, repoName: tmp.repoName,
filterStep: FilterSteps.notParentContributor, filterStep: FilterSteps.notParentContributor,
filterState: { filterState: {
status: tmp.unused ? FilterStatus.pass : FilterStatus.fail status: tmp.unused ? FilterStatus.pass : FilterStatus.fail,
} },
} },
} });
)
}); });
} }
@@ -298,6 +297,21 @@ class GithubDetectUnusedRepos {
tmpObjFromForkContrib.unused && tmpObjFromForkContrib.unused &&
tmpObjFromParentContrib.unused, tmpObjFromParentContrib.unused,
}); });
allRepoWithFlagTillStep4.forEach(tmp => {
defaultStore.dispatch(
{
type: ActionNames.AddRepoFilterStatus,
payload: {
repoName: tmp.repoName,
filterStep: FilterSteps.noCommits,
filterState: {
status: tmp.unused ? FilterStatus.pass : FilterStatus.fail
}
}
}
)
});
} }
} }
} }
@@ -502,20 +516,18 @@ class GithubDetectUnusedRepos {
} }
} }
export function runMain(username: string = 'bendtherules') { function runMain(username: string = 'bendtherules') {
const instance = new GithubDetectUnusedRepos(username); const instance = new GithubDetectUnusedRepos(username);
instance instance.fetchUnusedForkedRepos().then(unusedRepoNames => {
.fetchUnusedForkedRepos()
.then(unusedRepoNames => {
// tslint:disable-next-line:no-console // tslint:disable-next-line:no-console
console.log(unusedRepoNames); console.log(unusedRepoNames);
console.log(instance.reduxStore.getState()); console.log(defaultStore.getState());
}); });
} }
export { defaultStore, runMain };
// Next steps // Next steps
// //
// 1. Get all forked repos for a user // 1. Get all forked repos for a user
@@ -29,6 +29,6 @@ export interface AddRepoFilterStatusArgs
} }
export type ActionsType = export type ActionsType =
InitUserNameArgs | InitUserNameArgs
| InitRepoArgs | InitRepoArgs
| AddRepoFilterStatusArgs; | AddRepoFilterStatusArgs;
@@ -59,7 +59,7 @@ export type RepoFilterStateMap = Map<
{ {
filterState: RepoFilterState; filterState: RepoFilterState;
} }
>; >;
export interface ApplicationState { export interface ApplicationState {
username: string; username: string;
+79
View File
@@ -0,0 +1,79 @@
import { Reducer, createStore, Store } from 'redux';
import { ApplicationState, FilterStatus, RepoFilterState } from './model';
import {
ActionNames,
InitUserNameArgs,
InitRepoArgs,
AddRepoFilterStatusArgs,
ActionsType,
} from './actions';
export const initialState: ApplicationState = {
username: '',
repoStateMap: new Map(),
};
export const reducer: Reducer<ApplicationState, ActionsType> = (
state: ApplicationState = initialState,
action
) => {
// We'll augment the action type on the switch case to make sure we have
// all the cases handled.
switch (action.type) {
case ActionNames.InitUserName:
return { ...state, username: action.payload.userName };
case ActionNames.InitRepo: {
const clonedAppState = new Map(state.repoStateMap);
clonedAppState.set(action.payload.repoName, {
filterState: {
forked: {
status: FilterStatus.waiting,
},
eachBranchBehindOrEven: {
status: FilterStatus.waiting,
},
notForkContributor: {
status: FilterStatus.waiting,
},
notParentContributor: {
status: FilterStatus.waiting,
},
noCommits: {
status: FilterStatus.waiting,
},
},
});
return { ...state, repoStateMap: clonedAppState };
}
case ActionNames.AddRepoFilterStatus: {
const clonedAppState = new Map(state.repoStateMap);
let clonedRepoState = JSON.parse(
JSON.stringify(clonedAppState.get(action.payload.repoName))
) as
| {
filterState: RepoFilterState;
}
| undefined;
if (clonedRepoState !== undefined) {
clonedRepoState.filterState[action.payload.filterStep] =
action.payload.filterState;
clonedAppState.set(action.payload.repoName, clonedRepoState);
return { ...state, repoStateMap: clonedAppState };
} else {
return state;
}
}
default:
return state;
}
};
export function createAppStore(): Store<ApplicationState, ActionsType> {
return (createStore<ApplicationState, ActionsType, {}, {}>(
reducer,
initialState
) as any) as Store<ApplicationState, ActionsType>;
}
+1 -1
View File
@@ -1,6 +1,6 @@
import 'babel-polyfill'; import 'babel-polyfill';
// tslint:disable-next-line:ordered-imports // tslint:disable-next-line:ordered-imports
import { runMain } from './gh'; import { runMain } from './core/gh';
// tslint:disable-next-line:only-arrow-functions // tslint:disable-next-line:only-arrow-functions
(function(currentWindow: Window) { (function(currentWindow: Window) {
-71
View File
@@ -1,71 +0,0 @@
import { Reducer, createStore, Store } from 'redux';
import { ApplicationState, FilterStatus, RepoFilterState } from './model';
import { ActionNames, InitUserNameArgs, InitRepoArgs, AddRepoFilterStatusArgs, ActionsType } from './actions';
export const initialState: ApplicationState = {
username: '',
repoStateMap: new Map()
};
export const reducer: Reducer<ApplicationState, ActionsType> = (state: ApplicationState = initialState, action) => {
// We'll augment the action type on the switch case to make sure we have
// all the cases handled.
switch (action.type) {
case ActionNames.InitUserName:
return { ...state, username: action.payload.userName };
case ActionNames.InitRepo:
{
const clonedAppState = new Map(state.repoStateMap);
clonedAppState.set(action.payload.repoName, {
filterState: {
forked: {
status: FilterStatus.waiting
},
eachBranchBehindOrEven: {
status: FilterStatus.waiting
},
notForkContributor: {
status: FilterStatus.waiting
},
notParentContributor: {
status: FilterStatus.waiting
},
noCommits: {
status: FilterStatus.waiting
}
}
});
return { ...state, repoStateMap: clonedAppState };
}
case ActionNames.AddRepoFilterStatus:
{
const clonedAppState = new Map(state.repoStateMap);
let clonedRepoState = JSON.parse(JSON.stringify(clonedAppState.get(action.payload.repoName))) as (
{
filterState: RepoFilterState;
} | undefined
);
if (clonedRepoState !== undefined) {
clonedRepoState.filterState[action.payload.filterStep] = action.payload.filterState;
clonedAppState.set(action.payload.repoName, clonedRepoState);
return { ...state, repoStateMap: clonedAppState };
} else {
return state;
}
}
default:
return state;
}
};
export function createAppStore(): Store<ApplicationState, ActionsType> {
return createStore<
ApplicationState, ActionsType, {}, {}
>(
reducer,
initialState
) as any as Store<ApplicationState, ActionsType>;
};