add infra store

This commit is contained in:
snoop 2018-03-08 16:40:33 +09:00
parent d3bb6fc8a0
commit 098996fcf1
13 changed files with 275 additions and 34 deletions

View File

@ -0,0 +1,24 @@
import { NgModule } from '@angular/core';
import { StoreModule } from '@ngrx/store';
import { StoreDevtoolsModule } from '@ngrx/store-devtools';
import {
StoreRouterConnectingModule,
RouterStateSerializer,
} from '@ngrx/router-store';
import { EffectsModule } from '@ngrx/effects';
import { combineReducers, ActionReducer, ActionReducerMap, MetaReducer } from '@ngrx/store';
import {
REDUCERS,
EFFECTS,
} from './store';
import { MODULE } from './infra.constant';
@NgModule({
imports: [
StoreModule.forFeature(MODULE.name, REDUCERS),
EffectsModule.forFeature(EFFECTS),
],
})
export class InfraStoreModule { }

View File

@ -0,0 +1,3 @@
export const MODULE = {
name: 'infra'
};

View File

@ -3,18 +3,23 @@ import { CommonModule } from '@angular/common';
import { MaterialModule } from 'app/commons/ui/material/material.module'; import { MaterialModule } from 'app/commons/ui/material/material.module';
import { InfraStoreModule } from './infra-store.module';
import { COMPONENTS } from './component'; import { COMPONENTS } from './component';
import { SERVICES } from './service';
@NgModule({ @NgModule({
imports: [ imports: [
CommonModule, CommonModule,
MaterialModule MaterialModule,
InfraStoreModule
], ],
declarations: [ declarations: [
COMPONENTS, COMPONENTS,
], ],
exports: [ exports: [
COMPONENTS, COMPONENTS,
SERVICES
], ],
}) })
export class InfraModule { } export class InfraModule { }

View File

@ -0,0 +1,5 @@
import { InfraService } from './infra.service';
export const SERVICES = [
InfraService,
];

View File

@ -0,0 +1,15 @@
import { TestBed, inject } from '@angular/core/testing';
import { InfraService } from './infra.service';
describe('InfraService', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [InfraService]
});
});
it('should be created', inject([InfraService], (service: InfraService) => {
expect(service).toBeTruthy();
}));
});

View File

@ -1,32 +1,29 @@
// import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
// import { Observable } from 'rxjs/Observable'; import { Observable } from 'rxjs/Observable';
// import 'rxjs/add/operator/map'; import 'rxjs/add/operator/map';
// import { RESTService } from 'packages/commons/service/rest.service'; import { RESTService } from 'packages/commons/service/rest.service';
// import { DomainMember } from 'packages/domain/model';
// import { Infra } from '../model'; import { Infra } from '../model';
import { Page } from '../../../app/commons/model';
import { Domain } from '../../domain/model';
import { Probe } from '../../probe/model';
// @Injectable() @Injectable()
// export class MemberService { export class InfraService {
// public constructor( public constructor(
// private restService: RESTService, private restService: RESTService,
// ) { ) {
// } }
// public signin(email: string, password: string): Observable<DomainMember> { public readByDomain(domain: Domain): Observable<Page> {
// const body = { return this.restService.post<Page>('/account/signin', domain);
// signinId: email, }
// signinPw: password,
// };
// return this.restService.post<DomainMember>('/account/signin', body); public readByProbe(probe: Probe): Observable<Page> {
// } return this.restService.post<Page>('/account/signup', probe);
}
// public signup(member: Member): Observable<Infra> { }
// return this.restService.post<Member>('/account/signup', member);
// }
// }

View File

@ -0,0 +1,37 @@
import {
createSelector,
createFeatureSelector,
ActionReducerMap,
} from '@ngrx/store';
import { MODULE } from '../infra.constant';
import * as ReadByDomainStore from './readbydomain';
export interface State {
readbydomain: ReadByDomainStore.State;
}
export const REDUCERS = {
readbydomain: ReadByDomainStore.reducer,
};
export const EFFECTS = [
ReadByDomainStore.Effects,
];
export const selectInfraState = createFeatureSelector<State>(MODULE.name);
export const selectInfraReadByDomainState = createSelector(
selectInfraState,
(state: State) => state.readbydomain
);
export const AuthSelector = new ReadByDomainStore.StateSelector(selectInfraReadByDomainState);
export const selectInfraReadByDomianState = createSelector(
selectInfraState,
(state: State) => state.readbydomain
);

View File

@ -1,4 +1,4 @@
// export * from './readbydomain.action'; export * from './readbydomain.action';
// export * from './readbydomain.effect'; export * from './readbydomain.effect';
// export * from './readbydomain.reducer'; export * from './readbydomain.reducer';
// export * from './readbydomain.state'; export * from './readbydomain.state';

View File

@ -0,0 +1,36 @@
import { Action } from '@ngrx/store';
import { ErrorResponse } from 'packages/commons/service/error-response';
import { Infra } from '../../model';
import { Page } from '../../../../app/commons/model';
export enum ActionType {
Readbydomain = '[infra.readbydomain] Readbydomain',
ReadbydomainSuccess = '[infra.readbydomain] ReadbydomainSuccess',
ReadbydomainFailure = '[infra.readbydomain] ReadbydomainFailure',
}
export class Readbydomain implements Action {
readonly type = ActionType.Readbydomain;
constructor(public payload: Infra) {}
}
export class ReadbydomainSuccess implements Action {
readonly type = ActionType.ReadbydomainSuccess;
constructor(public payload: Page) {}
}
export class ReadbydomainFailure implements Action {
readonly type = ActionType.ReadbydomainFailure;
constructor(public payload: ErrorResponse) {}
}
export type Actions =
| Readbydomain
| ReadbydomainSuccess
| ReadbydomainFailure
;

View File

@ -0,0 +1,15 @@
import { TestBed, inject } from '@angular/core/testing';
import { Effects } from './readbydomain.effect';
describe('Signup.Effects', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [Effects]
});
});
it('should be created', inject([Effects], (effects: Effects) => {
expect(effects).toBeTruthy();
}));
});

View File

@ -0,0 +1,54 @@
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { Effect, Actions, ofType } from '@ngrx/effects';
import { Action } from '@ngrx/store';
import { Observable } from 'rxjs/Observable';
import { of } from 'rxjs/observable/of';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/do';
import 'rxjs/add/operator/exhaustMap';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/take';
import { ErrorResponse } from 'packages/commons/service/error-response';
import { Infra } from '../../model';
import { InfraService } from '../../service/infra.service';
import {
Readbydomain,
ReadbydomainSuccess,
ReadbydomainFailure,
ActionType,
} from './readbydomain.action';
import { Domain } from 'packages/domain/model';
@Injectable()
export class Effects {
constructor(
private actions$: Actions,
private infraService: InfraService,
private router: Router
) { }
@Effect()
readbydomain$: Observable<Action> = this.actions$
.ofType(ActionType.Readbydomain)
.map((action: Readbydomain) => action.payload)
.exhaustMap(member =>
this.infraService
.readByDomain(member)
.map(_member => new ReadbydomainSuccess(_member))
.catch(error => of(new ReadbydomainFailure(error)))
);
@Effect({ dispatch: false })
readbydomainSuccess$ = this.actions$
.ofType(ActionType.ReadbydomainSuccess)
.do(() => this.router.navigate(['/']));
}

View File

@ -0,0 +1,39 @@
import { Actions, ActionType } from './readbydomain.action';
import { State, initialState } from './readbydomain.state';
import { Infra } from '../../model';
export function reducer(state = initialState, action: Actions): State {
switch (action.type) {
case ActionType.Readbydomain: {
return {
...state,
error: null,
isPending: true
};
}
case ActionType.ReadbydomainSuccess: {
return {
...state,
error: null,
isPending: false,
infraList: action.payload.content
};
}
case ActionType.ReadbydomainFailure: {
return {
...state,
error: action.payload,
isPending: false,
infraList: null
};
}
default: {
return state;
}
}
}

View File

@ -1,20 +1,31 @@
import { ErrorResponse } from 'packages/commons/service/error-response'; import {
createSelector,
MemoizedSelector,
} from '@ngrx/store';
import { ErrorResponse } from 'packages/commons/service/error-response';
import { Infra } from '../../model'; import { Infra } from '../../model';
import { Page } from '../../../../app/commons/model'; import { Page } from '../../../../app/commons/model';
export interface State { export interface State {
isSignin: boolean;
error: ErrorResponse | null; error: ErrorResponse | null;
isPending: boolean; isPending: boolean;
infraList: Infra[] | null; infraList: Page | null;
} }
export const initialState: State = { export const initialState: State = {
isSignin: false,
error: null, error: null,
isPending: false, isPending: false,
infraList: null, infraList: null,
}; };
export const getInfraList = (state: State) => state.infraList; export class StateSelector {
export const getError = (state: State) => state.error; public constructor(private _selector: MemoizedSelector<any, any>) {
export const isPending = (state: State) => state.isPending; }
public getInfraList = createSelector(this._selector, (state: State) => state.infraList);
public getError = createSelector(this._selector, (state: State) => state.error);
public isPending = createSelector(this._selector, (state: State) => state.isPending);
}