This commit is contained in:
crusader 2018-03-08 16:03:52 +09:00
parent 9dd4ba4ddc
commit 9d8ef9fc9a
9 changed files with 58 additions and 64 deletions

View File

@ -29,19 +29,18 @@
"@covalent/http": "^1.0.0-rc.4", "@covalent/http": "^1.0.0-rc.4",
"@covalent/markdown": "^1.0.0-rc.4", "@covalent/markdown": "^1.0.0-rc.4",
"@ngrx/core": "^1.2.0", "@ngrx/core": "^1.2.0",
"@ngrx/effects": "^5.0.1", "@ngrx/effects": "^5.2.0",
"@ngrx/router-store": "^5.0.1", "@ngrx/entity": "^5.2.0",
"@ngrx/store": "^5.0.0", "@ngrx/router-store": "^5.2.0",
"@ngrx/store-devtools": "^5.0.1", "@ngrx/store": "^5.2.0",
"@ngrx/store-devtools": "^5.2.0",
"@swimlane/ngx-charts": "^7.1.1", "@swimlane/ngx-charts": "^7.1.1",
"@types/url-join": "^0.8.2",
"core-js": "^2.5.3", "core-js": "^2.5.3",
"d3": "^4.13.0", "d3": "^4.13.0",
"hammerjs": "^2.0.8", "hammerjs": "^2.0.8",
"ng2-odometer": "^1.1.3", "ng2-odometer": "^1.1.3",
"ngx-perfect-scrollbar": "^5.3.1", "ngx-perfect-scrollbar": "^5.3.1",
"rxjs": "^5.5.6", "rxjs": "^5.5.6",
"url-join": "^4.0.0",
"zone.js": "^0.8.20", "zone.js": "^0.8.20",
"angular-l10n": "^4.1.5" "angular-l10n": "^4.1.5"
}, },

View File

@ -22,9 +22,9 @@ export class AuthGuard implements CanActivate, CanActivateChild {
canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> { canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
return this.store return this.store
.select(AuthSelector.isSignin) .select(AuthSelector.select('signined'))
.map(isSignin => { .map(signined => {
if (!isSignin) { if (!signined) {
this.store.dispatch(new AuthStore.SigninRedirect()); this.store.dispatch(new AuthStore.SigninRedirect());
return false; return false;
} }
@ -36,9 +36,9 @@ export class AuthGuard implements CanActivate, CanActivateChild {
canActivateChild(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> { canActivateChild(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
return this.store return this.store
.select(AuthSelector.isSignin) .select(AuthSelector.select('signined'))
.map(isSignin => { .map(signined => {
if (!isSignin) { if (!signined) {
this.store.dispatch(new AuthStore.SigninRedirect()); this.store.dispatch(new AuthStore.SigninRedirect());
return false; return false;
} }

View File

@ -0,0 +1 @@
export * from './selectors';

View File

@ -0,0 +1,16 @@
import {
createSelector,
MemoizedSelector,
} from '@ngrx/store';
export class StateSelector<State> {
public constructor(private _selector: MemoizedSelector<object, State>) {
}
public select = (propertyPath: string) => createSelector(this._selector, (state: State) => {
return propertyPath.split('.').reduce(
(prev, curr) => {
return prev ? prev[curr] : undefined;
}, state);
})
}

View File

@ -3,6 +3,8 @@ import { Router } from '@angular/router';
import { FormGroup, FormBuilder, Validators } from '@angular/forms'; import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { Store, select } from '@ngrx/store'; import { Store, select } from '@ngrx/store';
import { ErrorResponse } from 'packages/commons/service/error-response';
import * as AuthStore from '../../store/auth'; import * as AuthStore from '../../store/auth';
import { AuthSelector } from '../../store'; import { AuthSelector } from '../../store';
@ -12,9 +14,8 @@ import { AuthSelector } from '../../store';
styleUrls: ['./signin.component.scss'] styleUrls: ['./signin.component.scss']
}) })
export class SigninComponent implements OnInit { export class SigninComponent implements OnInit {
pending$ = this.store.pipe(select(AuthSelector.select('pending')));
pending$ = this.store.pipe(select(AuthSelector.isPending)); error$ = this.store.pipe(select(AuthSelector.select('error')));
error$ = this.store.pipe(select(AuthSelector.getError));
errorMessage: string | null; errorMessage: string | null;

View File

@ -1,35 +1,19 @@
import {
createSelector,
MemoizedSelector,
} from '@ngrx/store';
import { ErrorResponse } from 'packages/commons/service/error-response'; import { ErrorResponse } from 'packages/commons/service/error-response';
import { Domain } from 'packages/domain/model'; import { Domain } from 'packages/domain/model';
import { Member } from '../../model'; import { Member } from '../../model';
export interface State { export interface State {
isSignin: boolean; signined: boolean;
error: ErrorResponse | null; error: ErrorResponse | null;
isPending: boolean; pending: boolean;
member: Member | null; member: Member | null;
domain: Domain | null; domain: Domain | null;
} }
export const initialState: State = { export const initialState: State = {
isSignin: false, signined: false,
error: null, error: null,
isPending: false, pending: false,
member: null, member: null,
domain: null, domain: null,
}; };
export class StateSelector {
public constructor(private _selector: MemoizedSelector<any, any>) {
}
public isSignin = createSelector(this._selector, (state: State) => state.isSignin);
public getMember = createSelector(this._selector, (state: State) => state.member);
public getDomain = createSelector(this._selector, (state: State) => state.domain);
public getError = createSelector(this._selector, (state: State) => state.error);
public isPending = createSelector(this._selector, (state: State) => state.isPending);
}

View File

@ -4,6 +4,8 @@ import {
ActionReducerMap, ActionReducerMap,
} from '@ngrx/store'; } from '@ngrx/store';
import { StateSelector } from 'packages/commons/util/ngrx/store';
import { MODULE } from '../member.constant'; import { MODULE } from '../member.constant';
import * as AuthStore from './auth'; import * as AuthStore from './auth';
@ -30,11 +32,10 @@ export const selectMemberAuthState = createSelector(
selectMemberState, selectMemberState,
(state: State) => state.auth (state: State) => state.auth
); );
export const AuthSelector = new StateSelector<AuthStore.State>(selectMemberAuthState);
export const AuthSelector = new AuthStore.StateSelector(selectMemberAuthState);
export const selectMemberSignupState = createSelector( export const selectMemberSignupState = createSelector(
selectMemberState, selectMemberState,
(state: State) => state.signup (state: State) => state.signup
); );
export const SignupSelector = new StateSelector<SignupStore.Signup>(selectMemberSignupState);

View File

@ -4,16 +4,12 @@ import { Member } from '../../model';
export interface State { export interface State {
error: ErrorResponse | null; error: ErrorResponse | null;
isPending: boolean; pending: boolean;
member: Member | null; member: Member | null;
} }
export const initialState: State = { export const initialState: State = {
error: null, error: null,
isPending: false, pending: false,
member: null, member: null,
}; };
export const getMember = (state: State) => state.member;
export const getError = (state: State) => state.error;
export const isPending = (state: State) => state.isPending;

View File

@ -206,21 +206,25 @@
version "1.2.0" version "1.2.0"
resolved "https://registry.yarnpkg.com/@ngrx/core/-/core-1.2.0.tgz#882b46abafa2e0e6d887cb71a1b2c2fa3e6d0dc6" resolved "https://registry.yarnpkg.com/@ngrx/core/-/core-1.2.0.tgz#882b46abafa2e0e6d887cb71a1b2c2fa3e6d0dc6"
"@ngrx/effects@^5.0.1": "@ngrx/effects@^5.2.0":
version "5.0.1" version "5.2.0"
resolved "https://registry.yarnpkg.com/@ngrx/effects/-/effects-5.0.1.tgz#cf970a6b8e2f3d3647e795e7040e09a2e7d157ed" resolved "https://registry.yarnpkg.com/@ngrx/effects/-/effects-5.2.0.tgz#aa762b69cb6fd4644d724a1cecd265caa42baf09"
"@ngrx/router-store@^5.0.1": "@ngrx/entity@^5.2.0":
version "5.0.1" version "5.2.0"
resolved "https://registry.yarnpkg.com/@ngrx/router-store/-/router-store-5.0.1.tgz#db872327bb958a2ebf296734c97de68672ec628a" resolved "https://registry.yarnpkg.com/@ngrx/entity/-/entity-5.2.0.tgz#68b90b3159bb46ec5b2d0de3fa0bf4e80e94972d"
"@ngrx/store-devtools@^5.0.1": "@ngrx/router-store@^5.2.0":
version "5.0.1" version "5.2.0"
resolved "https://registry.yarnpkg.com/@ngrx/store-devtools/-/store-devtools-5.0.1.tgz#f1bf5c54d4b2a512afd970e8114057b44e0d3c16" resolved "https://registry.yarnpkg.com/@ngrx/router-store/-/router-store-5.2.0.tgz#bf4b174ce19a36eba8211fc1ddeaf1e35ae74368"
"@ngrx/store@^5.0.0": "@ngrx/store-devtools@^5.2.0":
version "5.0.0" version "5.2.0"
resolved "https://registry.yarnpkg.com/@ngrx/store/-/store-5.0.0.tgz#483a955ea99a63ee083675ce0596d505e5501747" resolved "https://registry.yarnpkg.com/@ngrx/store-devtools/-/store-devtools-5.2.0.tgz#2fff916a9aa349375826772b359dbb64b9e5d622"
"@ngrx/store@^5.2.0":
version "5.2.0"
resolved "https://registry.yarnpkg.com/@ngrx/store/-/store-5.2.0.tgz#627ed74c9cd95462930485d912a557117b23903e"
"@ngtools/json-schema@1.1.0", "@ngtools/json-schema@^1.1.0": "@ngtools/json-schema@1.1.0", "@ngtools/json-schema@^1.1.0":
version "1.1.0" version "1.1.0"
@ -291,10 +295,6 @@
version "0.0.30" version "0.0.30"
resolved "https://registry.yarnpkg.com/@types/strip-json-comments/-/strip-json-comments-0.0.30.tgz#9aa30c04db212a9a0649d6ae6fd50accc40748a1" resolved "https://registry.yarnpkg.com/@types/strip-json-comments/-/strip-json-comments-0.0.30.tgz#9aa30c04db212a9a0649d6ae6fd50accc40748a1"
"@types/url-join@^0.8.2":
version "0.8.2"
resolved "https://registry.yarnpkg.com/@types/url-join/-/url-join-0.8.2.tgz#1181ecbe1d97b7034e0ea1e35e62e86cc26b422d"
JSONStream@^1.0.3: JSONStream@^1.0.3:
version "1.3.2" version "1.3.2"
resolved "https://registry.yarnpkg.com/JSONStream/-/JSONStream-1.3.2.tgz#c102371b6ec3a7cf3b847ca00c20bb0fce4c6dea" resolved "https://registry.yarnpkg.com/JSONStream/-/JSONStream-1.3.2.tgz#c102371b6ec3a7cf3b847ca00c20bb0fce4c6dea"
@ -7283,10 +7283,6 @@ urix@^0.1.0:
version "0.1.0" version "0.1.0"
resolved "https://registry.yarnpkg.com/urix/-/urix-0.1.0.tgz#da937f7a62e21fec1fd18d49b35c2935067a6c72" resolved "https://registry.yarnpkg.com/urix/-/urix-0.1.0.tgz#da937f7a62e21fec1fd18d49b35c2935067a6c72"
url-join@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/url-join/-/url-join-4.0.0.tgz#4d3340e807d3773bda9991f8305acdcc2a665d2a"
url-loader@^0.6.2: url-loader@^0.6.2:
version "0.6.2" version "0.6.2"
resolved "https://registry.yarnpkg.com/url-loader/-/url-loader-0.6.2.tgz#a007a7109620e9d988d14bce677a1decb9a993f7" resolved "https://registry.yarnpkg.com/url-loader/-/url-loader-0.6.2.tgz#a007a7109620e9d988d14bce677a1decb9a993f7"