Add redux store and package

This commit is contained in:
Abhas
2018-07-11 19:44:03 +05:30
parent 69d9b8081d
commit 4fd92d9130
7 changed files with 263 additions and 114 deletions
+34
View File
@@ -0,0 +1,34 @@
import { Action } from 'redux';
import { FilterSteps, RepoFilterState } from './model';
export enum ActionNames {
InitUserName = 'InitUserName',
InitRepo = 'InitRepo',
AddRepoFilterStatus = 'AddRepoFilterStatus',
}
export interface InitUserNameArgs extends Action<ActionNames.InitUserName> {
payload: {
userName: string;
};
}
export interface InitRepoArgs extends Action<ActionNames.InitRepo> {
payload: {
repoName: string;
};
}
export interface AddRepoFilterStatusArgs
extends Action<ActionNames.AddRepoFilterStatus> {
payload: {
repoName: string;
filterStep: FilterSteps;
filterState: RepoFilterState[FilterSteps];
};
}
export type ActionsType =
InitUserNameArgs
| InitRepoArgs
| AddRepoFilterStatusArgs;
+3 -44
View File
@@ -1,44 +1,3 @@
import { Store, createStore, applyMiddleware } from 'redux';
export namespace FailReasons {
export enum EachBranchBehindOrEven {
'remote branch doesn\'t exist',
'forked branch ahead'
};
export enum NotContributor {
'contributor in self repo',
'contributor in parent repo'
};
export enum NoCommits {
'commit in self repo',
'commit in parent repo'
};
}
export interface GithubRepoFilterState {
eachBranchBehindOrEven: {
fail: boolean,
failReason: FailReasons.EachBranchBehindOrEven
failBranchName: string
},
notContributor: {
fail: boolean,
failReason: FailReasons.NotContributor
},
noCommits: {
fail: boolean,
failReason: FailReasons.NoCommits
}
}
export type GithubRepoFilterStates = Map<string, {
filterState: GithubRepoFilterState
}>;
export interface ApplicationState {
username: string;
repoStates: GithubRepoFilterStates;
}
export * from './model';
export * from './actions';
export * from './reducers';
+58
View File
@@ -0,0 +1,58 @@
import { Store, createStore, applyMiddleware } from 'redux';
export namespace FailReasons {
export enum EachBranchBehindOrEven {
"remote branch doesn't exist",
'forked branch ahead',
}
export enum NotContributor {
'contributor in self repo',
'contributor in parent repo',
}
export enum NoCommits {
'commit in self repo',
'commit in parent repo',
}
}
export enum FilterSteps {
eachBranchBehindOrEven = 'eachBranchBehindOrEven',
notContributor = 'notContributor',
noCommits = 'noCommits',
}
export enum FilterStatus {
'pass',
'fail',
'waiting',
}
export interface RepoFilterState {
[FilterSteps.eachBranchBehindOrEven]: {
status: FilterStatus;
failReason?: FailReasons.EachBranchBehindOrEven;
failBranchName?: string;
};
[FilterSteps.notContributor]: {
status: FilterStatus;
failReason?: FailReasons.NotContributor;
};
[FilterSteps.noCommits]: {
status: FilterStatus;
failReason?: FailReasons.NoCommits;
};
}
export type RepoFilterStateMap = Map<
string,
{
filterState: RepoFilterState;
}
>;
export interface ApplicationState {
username: string;
repoStateMap: RepoFilterStateMap;
}
+65
View File
@@ -0,0 +1,65 @@
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: {
eachBranchBehindOrEven: {
status: FilterStatus.waiting
},
notContributor: {
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>;
};
+80 -69
View File
@@ -2,140 +2,151 @@ import * as Octokit from '@octokit/rest';
// +++ All typescript definitions +++
interface RequestOptions {
method: string;
url: string;
headers: any;
query?: string;
variables?: Variables;
method: string;
url: string;
headers: any;
query?: string;
variables?: Variables;
}
interface Result {
headers: {
status: string;
};
headers: {
status: string;
};
}
interface OctokitError {
code: number;
status: string;
code: number;
status: string;
}
export interface OctokitMod extends Octokit {
// The following are added because Octokit does not expose the hook.error, hook.before, and hook.after methods
hook: {
error: (when: 'request', callback: (error: OctokitError, options: RequestOptions) => void) => void;
before: (when: 'request', callback: (result: Result, options: RequestOptions) => void) => void;
after: (when: 'request', callback: (result: Result, options: RequestOptions) => void) => void;
};
// The following are added because Octokit does not expose the hook.error, hook.before, and hook.after methods
hook: {
error: (
when: 'request',
callback: (error: OctokitError, options: RequestOptions) => void
) => void;
before: (
when: 'request',
callback: (result: Result, options: RequestOptions) => void
) => void;
after: (
when: 'request',
callback: (result: Result, options: RequestOptions) => void
) => void;
};
}
interface Variables {
[key: string]: any;
[key: string]: any;
}
export interface ResponseWithDataArray<T> {
data: T[];
data: T[];
}
interface ResponseWithMetaLink {
meta: {
link: string;
};
meta: {
link: string;
};
}
export interface ResponseWithDataArrayAndMeta<T> extends ResponseWithDataArray<T>, ResponseWithMetaLink {
}
export interface ResponseWithDataArrayAndMeta<T>
extends ResponseWithDataArray<T>,
ResponseWithMetaLink {}
export interface ResponseFromGetUserRepo extends ResponseWithMetaLink {
data: RepoFromGetUserRepo[];
data: RepoFromGetUserRepo[];
}
export interface ResponseFromGetBranches extends ResponseWithMetaLink {
data: BranchFromGetBranches[];
data: BranchFromGetBranches[];
}
export interface ResponseFromGetRepo {
data: RepoFromGetRepo;
meta: {};
data: RepoFromGetRepo;
meta: {};
}
export interface ResponseFromCompareCommits {
data: {
status: string;
ahead_by: number;
behind_by: number;
total_commits: number;
};
data: {
status: string;
ahead_by: number;
behind_by: number;
total_commits: number;
};
}
export interface ResponseFromGetContributors extends ResponseWithMetaLink {
data: OwnerFromGetContributors[] | undefined;
data: OwnerFromGetContributors[] | undefined;
}
export interface RepoFromGetUserRepo {
default_branch: string;
description: string;
fork: boolean;
full_name: string;
homepage: string;
id: number;
language: string;
name: string;
owner: OwnerFromGetUserRepo;
private: boolean;
pushed_at: string;
url: string;
default_branch: string;
description: string;
fork: boolean;
full_name: string;
homepage: string;
id: number;
language: string;
name: string;
owner: OwnerFromGetUserRepo;
private: boolean;
pushed_at: string;
url: string;
}
interface OwnerFromGetUserRepo {
avatar_url: string;
gravatar_id: string;
html_url: string;
id: number;
login: string;
repos_url: string;
type: UserType;
avatar_url: string;
gravatar_id: string;
html_url: string;
id: number;
login: string;
repos_url: string;
type: UserType;
}
export type OwnerFromGetContributors = OwnerFromGetUserRepo;
enum UserType {
User = 'User'
User = 'User',
}
export interface BranchFromGetBranches {
name: string;
commit: CommitFromGetBranches;
name: string;
commit: CommitFromGetBranches;
}
interface CommitFromGetBranches {
sha: string;
url: string;
sha: string;
url: string;
}
interface RepoFromGetRepo {
parent: RepoFromGetUserRepo;
parent: RepoFromGetUserRepo;
}
export interface RepoNameWithUnusedFlag {
repoName: string;
unused: boolean;
repoName: string;
unused: boolean;
}
export interface RepoNameWithBranches {
repoName: string;
branches: BranchFromGetBranches[];
repoName: string;
branches: BranchFromGetBranches[];
}
export interface RepoNameWithParentRepo {
repoName: string;
parentRepo: RepoFromGetUserRepo;
repoName: string;
parentRepo: RepoFromGetUserRepo;
}
export interface RepoNameWithBranchesAndParent extends RepoNameWithBranches, RepoNameWithParentRepo {
}
export interface RepoNameWithBranchesAndParent
extends RepoNameWithBranches,
RepoNameWithParentRepo {}
export interface ObjectWithPerPage {
per_page?: number;
per_page?: number;
}
// --- End all typescript definitions ---
// --- End all typescript definitions ---