added discovery modules

This commit is contained in:
snoop 2018-03-07 17:24:33 +09:00
parent a66411663d
commit b970bac4bd
14 changed files with 285 additions and 2 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 './discovery.constant';
@NgModule({
imports: [
StoreModule.forFeature(MODULE.name, REDUCERS),
EffectsModule.forFeature(EFFECTS),
],
})
export class MemberStoreModule { }

View File

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

View File

@ -4,10 +4,15 @@ import { CommonModule } from '@angular/common';
import { COMPONENTS } from './component';
import { MaterialModule } from 'app/commons/ui/material/material.module';
import { MemberStoreModule } from './discovery-store.module';
import { SERVICES } from './service';
@NgModule({
imports: [
CommonModule,
MaterialModule
MaterialModule,
MemberStoreModule
],
declarations: [
COMPONENTS
@ -15,5 +20,8 @@ import { MaterialModule } from 'app/commons/ui/material/material.module';
exports: [
COMPONENTS,
],
providers: [
SERVICES,
],
})
export class DiscoveryModule { }

View File

@ -0,0 +1,7 @@
export * from './DiscoveryStartInfo';
export * from './Host';
export * from './Port';
export * from './PortType';
export * from './Service';
export * from './Zone';

View File

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

View File

@ -5,7 +5,7 @@ import 'rxjs/add/operator/map';
import { RESTService } from 'packages/commons/service/rest.service';
import { DiscoveryStartInfo } from '../model/DiscoveryStartInfo';
import { DiscoveryStartInfo } from '../model';
@Injectable()
export class DiscoveryService {

View File

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

View File

@ -0,0 +1,14 @@
import * as SettingStore from './setting';
export interface State {
setting: SettingStore.State;
}
export const REDUCERS = {
setting: SettingStore.reducer,
};
export const EFFECTS = [
SettingStore.Effects,
];

View File

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

View File

@ -0,0 +1,42 @@
import { Action } from '@ngrx/store';
import { ErrorResponse } from 'packages/commons/service/error-response';
import { DiscoveryStartInfo } from '../../model';
export enum ActionType {
Setting = '[discovery.setting] Setting',
SettingSuccess = '[discovery.setting] SettingSuccess',
SettingFailure = '[discovery.setting] SettingFailure',
SettingRedirect = '[discovery.setting] SettingRedirect',
}
export class Setting implements Action {
readonly type = ActionType.Setting;
constructor(public payload: { payload: DiscoveryStartInfo}) {}
}
export class SettingSuccess implements Action {
readonly type = ActionType.SettingSuccess;
constructor(public payload: DiscoveryStartInfo) {}
}
export class SettingFailure implements Action {
readonly type = ActionType.SettingFailure;
constructor(public payload: ErrorResponse) {}
}
export class SettingRedirect implements Action {
readonly type = ActionType.SettingRedirect;
}
export type Actions =
| Setting
| SettingSuccess
| SettingFailure
| SettingRedirect
;

View File

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

View File

@ -0,0 +1,74 @@
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/switchMap';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/take';
import { ErrorResponse } from 'packages/commons/service/error-response';
import { DiscoveryStartInfo } from '../../model';
import { DiscoveryService } from '../../service/discovery.service';
import {
Setting,
SettingSuccess,
SettingFailure,
ActionType,
} from './setting.action';
@Injectable()
export class Effects {
constructor(
private actions$: Actions,
private discoveryService: DiscoveryService,
private router: Router
) { }
@Effect()
signin$: Observable<Action> = this.actions$
.ofType(ActionType.Setting)
.map((action: Setting) => action.payload)
.switchMap(payload => this.discoveryService.start(payload.payload))
.map(discoveryStartInfo => {
return new SettingSuccess(discoveryStartInfo);
})
.catch((error: ErrorResponse) => {
return of(new SettingFailure(error));
});
// .map((action: Signin) => action.payload)
// .exhaustMap(payload =>
// this.memberService
// .signin(payload.email, payload.password)
// .map(member => {
// return new SigninSuccess(member);
// })
// .catch((error) => {
// return of(new SigninFailure(error.message));
// })
// );
@Effect({ dispatch: false })
signinSuccess$ = this.actions$
.ofType(ActionType.SettingSuccess)
.do(() => this.router.navigate(['/']));
@Effect({ dispatch: false })
signinRedirect$ = this.actions$
.ofType(ActionType.SettingRedirect)
.do(authed => {
this.router.navigate(['/login']);
});
}

View File

@ -0,0 +1,50 @@
import { ErrorResponse } from 'packages/commons/service/error-response';
import {
Actions,
ActionType,
Setting,
} from './setting.action';
import {
State,
initialState,
} from './setting.state';
import { DiscoveryStartInfo } from '../../model';
export function reducer(state = initialState, action: Actions): State {
switch (action.type) {
case ActionType.Setting: {
return {
...state,
error: null,
isPending: true,
};
}
case ActionType.SettingSuccess: {
return {
...state,
isStart: true,
error: null,
isPending: false,
discoveryStartInfo: action.payload,
};
}
case ActionType.SettingFailure: {
return {
...state,
isStart: false,
error: action.payload,
isPending: false,
discoveryStartInfo: null,
};
}
default: {
return state;
}
}
}

View File

@ -0,0 +1,22 @@
import { ErrorResponse } from 'packages/commons/service/error-response';
import { DiscoveryStartInfo } from '../../model';
export interface State {
isStart: boolean;
error: ErrorResponse | null;
isPending: boolean;
discoveryStartInfo: DiscoveryStartInfo | null;
}
export const initialState: State = {
isStart: false,
error: null,
isPending: false,
discoveryStartInfo: null,
};
export const isStart = (state: State) => state.isStart;
export const getDiscoveryStartInfo = (state: State) => state.discoveryStartInfo;
export const getError = (state: State) => state.error;
export const isPending = (state: State) => state.isPending;