This commit is contained in:
crusader 2018-02-27 22:01:13 +09:00
parent bce371c211
commit 4ceaf533d2
24 changed files with 128 additions and 44 deletions

View File

@ -4,15 +4,19 @@ import { StoreDevtoolsModule } from '@ngrx/store-devtools';
import { import {
StoreRouterConnectingModule, StoreRouterConnectingModule,
RouterStateSerializer, RouterStateSerializer,
RouterReducerState,
} from '@ngrx/router-store'; } from '@ngrx/router-store';
import { EffectsModule } from '@ngrx/effects'; import { EffectsModule } from '@ngrx/effects';
import { combineReducers, ActionReducer, ActionReducerMap, MetaReducer } from '@ngrx/store'; import { combineReducers, ActionReducer, ActionReducerMap, MetaReducer } from '@ngrx/store';
import { environment } from '../environments/environment'; import { environment } from '../environments/environment';
import { AppState } from './app.state';
import { SimpleRouterStateSerializer } from './commons/router/state/serializer/simple-router-state-serializer'; import { SimpleRouterStateSerializer } from './commons/router/state/serializer/simple-router-state-serializer';
export interface AppState {
}
export const reducers: ActionReducerMap<AppState> = { export const reducers: ActionReducerMap<AppState> = {
}; };
@ -44,6 +48,7 @@ export const reducers: ActionReducerMap<AppState> = {
*/ */
StoreDevtoolsModule.instrument({ StoreDevtoolsModule.instrument({
name: 'overFlow WebApp DevTools', name: 'overFlow WebApp DevTools',
maxAge: 50,
logOnly: environment.production, logOnly: environment.production,
}), }),
EffectsModule.forRoot([]), EffectsModule.forRoot([]),
@ -58,4 +63,4 @@ export const reducers: ActionReducerMap<AppState> = {
], ],
}) })
export class AppReducerModule { } export class AppStoreModule { }

View File

@ -6,7 +6,7 @@ import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { HttpClientModule } from '@angular/common/http'; import { HttpClientModule } from '@angular/common/http';
import { AppRoutingModule } from './app-routing.module'; import { AppRoutingModule } from './app-routing.module';
import { AppReducerModule } from './app-reducer.module'; import { AppStoreModule } from './app-store.module';
import { MaterialModule } from './commons/ui/material/material.module'; import { MaterialModule } from './commons/ui/material/material.module';
import { CovalentModule } from './commons/ui/covalent/covalent.module'; import { CovalentModule } from './commons/ui/covalent/covalent.module';
@ -23,7 +23,7 @@ import { RPCService } from './commons/service/rpc.service';
BrowserModule, BrowserModule,
BrowserAnimationsModule, BrowserAnimationsModule,
AppRoutingModule, AppRoutingModule,
AppReducerModule, AppStoreModule,
MaterialModule, MaterialModule,
CovalentModule, CovalentModule,
HttpClientModule, HttpClientModule,

View File

@ -1,3 +0,0 @@
export interface AppState {
}

View File

@ -1,6 +1,9 @@
import { Component, OnInit } from '@angular/core'; import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router'; import { Router } from '@angular/router';
import { FormGroup, FormBuilder, Validators } from '@angular/forms'; import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { Store } from '@ngrx/store';
import * as AuthStore from '../store/auth';
@Component({ @Component({
selector: 'of-member-signin', selector: 'of-member-signin',
@ -17,6 +20,7 @@ export class SigninComponent implements OnInit {
constructor( constructor(
private router: Router, private router: Router,
private store: Store<AuthStore.State>,
private formBuilder: FormBuilder, private formBuilder: FormBuilder,
) { } ) { }

View File

@ -0,0 +1,15 @@
import { TestBed, async, inject } from '@angular/core/testing';
import { AuthGuard } from './auth.guard';
describe('AuthGuard', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [AuthGuard]
});
});
it('should ...', inject([AuthGuard], (guard: AuthGuard) => {
expect(guard).toBeTruthy();
}));
});

View File

@ -0,0 +1,49 @@
import { Injectable } from '@angular/core';
import {
CanActivate,
CanActivateChild,
ActivatedRouteSnapshot,
RouterStateSnapshot
} from '@angular/router';
import { Store } from '@ngrx/store';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/take';
import 'rxjs/add/operator/map';
import * as AuthStore from '../store/auth';
@Injectable()
export class AuthGuard implements CanActivate, CanActivateChild {
constructor(
private store: Store<AuthStore.State>
) { }
canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
return this.store
.select(AuthStore.isSignin)
.map(isSignin => {
if (!isSignin) {
this.store.dispatch(new AuthStore.SigninRedirect());
return false;
}
return true;
})
.take(1);
}
canActivateChild(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
return this.store
.select(AuthStore.isSignin)
.map(isSignin => {
if (!isSignin) {
this.store.dispatch(new AuthStore.SigninRedirect());
return false;
}
return true;
})
.take(1);
}
}

View File

@ -8,28 +8,15 @@ import {
import { EffectsModule } from '@ngrx/effects'; import { EffectsModule } from '@ngrx/effects';
import { combineReducers, ActionReducer, ActionReducerMap, MetaReducer } from '@ngrx/store'; import { combineReducers, ActionReducer, ActionReducerMap, MetaReducer } from '@ngrx/store';
import * as AuthStore from './store/auth'; import { State as MemberState } from './store/state';
import * as SignupStore from './store/signup'; import { reducers as memberReducers } from './store/reducer';
import { EFFECTS } from './store/effect';
import { MODULE } from './member.constant';
export interface State {
auth: AuthStore.State;
signup: SignupStore.Signup;
}
export const reducers = {
auth: AuthStore.reducer,
signup: SignupStore.reducer,
};
export const EFFECTS = [
AuthStore.Effects,
SignupStore.Effects,
];
@NgModule({ @NgModule({
imports: [ imports: [
StoreModule.forFeature('', reducers), StoreModule.forFeature(MODULE.name, memberReducers),
EffectsModule.forFeature(EFFECTS), EffectsModule.forFeature(EFFECTS),
], ],
}) })

View File

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

View File

@ -10,6 +10,7 @@ import { SigninComponent } from './component/signin.component';
import { SignupComponent } from './component/signup.component'; import { SignupComponent } from './component/signup.component';
import { ResetPasswordComponent } from './component/reset-password.component'; import { ResetPasswordComponent } from './component/reset-password.component';
import { MemberService } from './service/member.service'; import { MemberService } from './service/member.service';
import { AuthGuard } from './guard/auth.guard';
import { MemberStoreModule } from './member-store.module'; import { MemberStoreModule } from './member-store.module';
@ -32,6 +33,7 @@ export const COMPONENTS = [
declarations: COMPONENTS, declarations: COMPONENTS,
exports: COMPONENTS, exports: COMPONENTS,
providers: [ providers: [
AuthGuard,
MemberService, MemberService,
], ],
}) })

View File

@ -1,6 +1,6 @@
import { Action } from '@ngrx/store'; import { Action } from '@ngrx/store';
import { Member } from '../model/member'; import { Member } from '../../model/member';
export enum ActionType { export enum ActionType {
Signin = '[member.auth] Signin', Signin = '[member.auth] Signin',
@ -21,7 +21,7 @@ export class Signin implements Action {
export class SigninSuccess implements Action { export class SigninSuccess implements Action {
readonly type = ActionType.SigninSuccess; readonly type = ActionType.SigninSuccess;
constructor(public payload: { member: Member }) {} constructor(public payload: Member) {}
} }
export class SigninFailure implements Action { export class SigninFailure implements Action {

View File

@ -13,8 +13,8 @@ import 'rxjs/add/operator/exhaustMap';
import 'rxjs/add/operator/map'; import 'rxjs/add/operator/map';
import 'rxjs/add/operator/take'; import 'rxjs/add/operator/take';
import { Member } from '../model/member'; import { Member } from '../../model/member';
import { MemberService } from '../service/member.service'; import { MemberService } from '../../service/member.service';
import { import {
Signin, Signin,
@ -39,7 +39,7 @@ export class Effects {
.exhaustMap(payload => .exhaustMap(payload =>
this.memberService this.memberService
.signin(payload.email, payload.password) .signin(payload.email, payload.password)
.map(member => new SigninSuccess({ member })) .map(member => new SigninSuccess(member))
.catch(error => of(new SigninFailure(error))) .catch(error => of(new SigninFailure(error)))
); );

View File

@ -8,7 +8,7 @@ import {
initialState, initialState,
} from './auth.state'; } from './auth.state';
import { Member } from '../model/member'; import { Member } from '../../model/member';
export function reducer(state = initialState, action: Actions): State { export function reducer(state = initialState, action: Actions): State {
switch (action.type) { switch (action.type) {
@ -26,7 +26,7 @@ export function reducer(state = initialState, action: Actions): State {
isSignin: true, isSignin: true,
error: null, error: null,
isPending: false, isPending: false,
member: action.payload.member, member: action.payload,
}; };
} }

View File

@ -1,4 +1,4 @@
import { Member } from '../model/member'; import { Member } from '../../model/member';
export interface State { export interface State {
isSignin: boolean; isSignin: boolean;

View File

@ -0,0 +1,7 @@
import * as AuthStore from './auth';
import * as SignupStore from './signup';
export const EFFECTS = [
AuthStore.Effects,
SignupStore.Effects,
];

View File

@ -0,0 +1,7 @@
import * as AuthStore from './auth';
import * as SignupStore from './signup';
export const reducers = {
auth: AuthStore.reducer,
signup: SignupStore.reducer,
};

View File

@ -1,6 +1,6 @@
import { Action } from '@ngrx/store'; import { Action } from '@ngrx/store';
import { Member } from '../model/member'; import { Member } from '../../model/member';
export enum ActionType { export enum ActionType {
Signup = '[member.signup] Signup', Signup = '[member.signup] Signup',
@ -11,13 +11,13 @@ export enum ActionType {
export class Signup implements Action { export class Signup implements Action {
readonly type = ActionType.Signup; readonly type = ActionType.Signup;
constructor(public payload: {member: Member}) {} constructor(public payload: Member) {}
} }
export class SignupSuccess implements Action { export class SignupSuccess implements Action {
readonly type = ActionType.SignupSuccess; readonly type = ActionType.SignupSuccess;
constructor(public payload: { member: Member }) {} constructor(public payload: Member) {}
} }
export class SignupFailure implements Action { export class SignupFailure implements Action {

View File

@ -13,8 +13,8 @@ import 'rxjs/add/operator/exhaustMap';
import 'rxjs/add/operator/map'; import 'rxjs/add/operator/map';
import 'rxjs/add/operator/take'; import 'rxjs/add/operator/take';
import { Member } from '../model/member'; import { Member } from '../../model/member';
import { MemberService } from '../service/member.service'; import { MemberService } from '../../service/member.service';
import { import {
Signup, Signup,
@ -36,10 +36,10 @@ export class Effects {
signup$: Observable<Action> = this.actions$ signup$: Observable<Action> = this.actions$
.ofType(ActionType.Signup) .ofType(ActionType.Signup)
.map((action: Signup) => action.payload) .map((action: Signup) => action.payload)
.exhaustMap(payload => .exhaustMap(member =>
this.memberService this.memberService
.signup(payload.member) .signup(member)
.map(member => new SignupSuccess({ member })) .map(_member => new SignupSuccess(_member))
.catch(error => of(new SignupFailure(error))) .catch(error => of(new SignupFailure(error)))
); );

View File

@ -8,7 +8,7 @@ import {
initialState, initialState,
} from './signup.state'; } from './signup.state';
import { Member } from '../model/member'; import { Member } from '../../model/member';
export function reducer(state = initialState, action: Actions): State { export function reducer(state = initialState, action: Actions): State {
switch (action.type) { switch (action.type) {
@ -25,7 +25,7 @@ export function reducer(state = initialState, action: Actions): State {
...state, ...state,
error: null, error: null,
isPending: false, isPending: false,
member: action.payload.member, member: action.payload,
}; };
} }

View File

@ -1,4 +1,4 @@
import { Member } from '../model/member'; import { Member } from '../../model/member';
export interface State { export interface State {
error: string | null; error: string | null;

View File

@ -0,0 +1,8 @@
import * as AuthStore from './auth';
import * as SignupStore from './signup';
export interface State {
auth: AuthStore.State;
signup: SignupStore.Signup;
}