From 9d8ef9fc9acbfd3a215b457f185c84238bb44ef1 Mon Sep 17 00:00:00 2001 From: crusader Date: Thu, 8 Mar 2018 16:03:52 +0900 Subject: [PATCH] ing --- package.json | 11 +++--- src/app/commons/guard/auth.guard.ts | 12 +++---- src/packages/commons/util/ngrx/store/index.ts | 1 + .../commons/util/ngrx/store/selectors.ts | 16 +++++++++ .../component/signin/signin.component.ts | 7 ++-- src/packages/member/store/auth/auth.state.ts | 24 +++---------- src/packages/member/store/index.ts | 7 ++-- .../member/store/signup/signup.state.ts | 8 ++--- yarn.lock | 36 +++++++++---------- 9 files changed, 58 insertions(+), 64 deletions(-) create mode 100644 src/packages/commons/util/ngrx/store/index.ts create mode 100644 src/packages/commons/util/ngrx/store/selectors.ts diff --git a/package.json b/package.json index ff512ee..dc6b74c 100644 --- a/package.json +++ b/package.json @@ -29,19 +29,18 @@ "@covalent/http": "^1.0.0-rc.4", "@covalent/markdown": "^1.0.0-rc.4", "@ngrx/core": "^1.2.0", - "@ngrx/effects": "^5.0.1", - "@ngrx/router-store": "^5.0.1", - "@ngrx/store": "^5.0.0", - "@ngrx/store-devtools": "^5.0.1", + "@ngrx/effects": "^5.2.0", + "@ngrx/entity": "^5.2.0", + "@ngrx/router-store": "^5.2.0", + "@ngrx/store": "^5.2.0", + "@ngrx/store-devtools": "^5.2.0", "@swimlane/ngx-charts": "^7.1.1", - "@types/url-join": "^0.8.2", "core-js": "^2.5.3", "d3": "^4.13.0", "hammerjs": "^2.0.8", "ng2-odometer": "^1.1.3", "ngx-perfect-scrollbar": "^5.3.1", "rxjs": "^5.5.6", - "url-join": "^4.0.0", "zone.js": "^0.8.20", "angular-l10n": "^4.1.5" }, diff --git a/src/app/commons/guard/auth.guard.ts b/src/app/commons/guard/auth.guard.ts index 6b45245..6f85e17 100644 --- a/src/app/commons/guard/auth.guard.ts +++ b/src/app/commons/guard/auth.guard.ts @@ -22,9 +22,9 @@ export class AuthGuard implements CanActivate, CanActivateChild { canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable { return this.store - .select(AuthSelector.isSignin) - .map(isSignin => { - if (!isSignin) { + .select(AuthSelector.select('signined')) + .map(signined => { + if (!signined) { this.store.dispatch(new AuthStore.SigninRedirect()); return false; } @@ -36,9 +36,9 @@ export class AuthGuard implements CanActivate, CanActivateChild { canActivateChild(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable { return this.store - .select(AuthSelector.isSignin) - .map(isSignin => { - if (!isSignin) { + .select(AuthSelector.select('signined')) + .map(signined => { + if (!signined) { this.store.dispatch(new AuthStore.SigninRedirect()); return false; } diff --git a/src/packages/commons/util/ngrx/store/index.ts b/src/packages/commons/util/ngrx/store/index.ts new file mode 100644 index 0000000..8c9698f --- /dev/null +++ b/src/packages/commons/util/ngrx/store/index.ts @@ -0,0 +1 @@ +export * from './selectors'; diff --git a/src/packages/commons/util/ngrx/store/selectors.ts b/src/packages/commons/util/ngrx/store/selectors.ts new file mode 100644 index 0000000..7f4e1f6 --- /dev/null +++ b/src/packages/commons/util/ngrx/store/selectors.ts @@ -0,0 +1,16 @@ +import { + createSelector, + MemoizedSelector, +} from '@ngrx/store'; + +export class StateSelector { + public constructor(private _selector: MemoizedSelector) { + } + + public select = (propertyPath: string) => createSelector(this._selector, (state: State) => { + return propertyPath.split('.').reduce( + (prev, curr) => { + return prev ? prev[curr] : undefined; + }, state); + }) +} diff --git a/src/packages/member/component/signin/signin.component.ts b/src/packages/member/component/signin/signin.component.ts index e0d2da0..4ac23a2 100644 --- a/src/packages/member/component/signin/signin.component.ts +++ b/src/packages/member/component/signin/signin.component.ts @@ -3,6 +3,8 @@ import { Router } from '@angular/router'; import { FormGroup, FormBuilder, Validators } from '@angular/forms'; import { Store, select } from '@ngrx/store'; +import { ErrorResponse } from 'packages/commons/service/error-response'; + import * as AuthStore from '../../store/auth'; import { AuthSelector } from '../../store'; @@ -12,9 +14,8 @@ import { AuthSelector } from '../../store'; styleUrls: ['./signin.component.scss'] }) export class SigninComponent implements OnInit { - - pending$ = this.store.pipe(select(AuthSelector.isPending)); - error$ = this.store.pipe(select(AuthSelector.getError)); + pending$ = this.store.pipe(select(AuthSelector.select('pending'))); + error$ = this.store.pipe(select(AuthSelector.select('error'))); errorMessage: string | null; diff --git a/src/packages/member/store/auth/auth.state.ts b/src/packages/member/store/auth/auth.state.ts index 40898da..2c13e17 100644 --- a/src/packages/member/store/auth/auth.state.ts +++ b/src/packages/member/store/auth/auth.state.ts @@ -1,35 +1,19 @@ -import { - createSelector, - MemoizedSelector, -} from '@ngrx/store'; - import { ErrorResponse } from 'packages/commons/service/error-response'; import { Domain } from 'packages/domain/model'; import { Member } from '../../model'; export interface State { - isSignin: boolean; + signined: boolean; error: ErrorResponse | null; - isPending: boolean; + pending: boolean; member: Member | null; domain: Domain | null; } export const initialState: State = { - isSignin: false, + signined: false, error: null, - isPending: false, + pending: false, member: null, domain: null, }; - -export class StateSelector { - public constructor(private _selector: MemoizedSelector) { - } - - 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); -} diff --git a/src/packages/member/store/index.ts b/src/packages/member/store/index.ts index 7cbac22..fc38454 100644 --- a/src/packages/member/store/index.ts +++ b/src/packages/member/store/index.ts @@ -4,6 +4,8 @@ import { ActionReducerMap, } from '@ngrx/store'; +import { StateSelector } from 'packages/commons/util/ngrx/store'; + import { MODULE } from '../member.constant'; import * as AuthStore from './auth'; @@ -30,11 +32,10 @@ export const selectMemberAuthState = createSelector( selectMemberState, (state: State) => state.auth ); - -export const AuthSelector = new AuthStore.StateSelector(selectMemberAuthState); - +export const AuthSelector = new StateSelector(selectMemberAuthState); export const selectMemberSignupState = createSelector( selectMemberState, (state: State) => state.signup ); +export const SignupSelector = new StateSelector(selectMemberSignupState); diff --git a/src/packages/member/store/signup/signup.state.ts b/src/packages/member/store/signup/signup.state.ts index 4b9e1fd..f0ef9f0 100644 --- a/src/packages/member/store/signup/signup.state.ts +++ b/src/packages/member/store/signup/signup.state.ts @@ -4,16 +4,12 @@ import { Member } from '../../model'; export interface State { error: ErrorResponse | null; - isPending: boolean; + pending: boolean; member: Member | null; } export const initialState: State = { error: null, - isPending: false, + pending: false, member: null, }; - -export const getMember = (state: State) => state.member; -export const getError = (state: State) => state.error; -export const isPending = (state: State) => state.isPending; diff --git a/yarn.lock b/yarn.lock index cff6d60..d6eb506 100644 --- a/yarn.lock +++ b/yarn.lock @@ -206,21 +206,25 @@ version "1.2.0" resolved "https://registry.yarnpkg.com/@ngrx/core/-/core-1.2.0.tgz#882b46abafa2e0e6d887cb71a1b2c2fa3e6d0dc6" -"@ngrx/effects@^5.0.1": - version "5.0.1" - resolved "https://registry.yarnpkg.com/@ngrx/effects/-/effects-5.0.1.tgz#cf970a6b8e2f3d3647e795e7040e09a2e7d157ed" +"@ngrx/effects@^5.2.0": + version "5.2.0" + resolved "https://registry.yarnpkg.com/@ngrx/effects/-/effects-5.2.0.tgz#aa762b69cb6fd4644d724a1cecd265caa42baf09" -"@ngrx/router-store@^5.0.1": - version "5.0.1" - resolved "https://registry.yarnpkg.com/@ngrx/router-store/-/router-store-5.0.1.tgz#db872327bb958a2ebf296734c97de68672ec628a" +"@ngrx/entity@^5.2.0": + version "5.2.0" + resolved "https://registry.yarnpkg.com/@ngrx/entity/-/entity-5.2.0.tgz#68b90b3159bb46ec5b2d0de3fa0bf4e80e94972d" -"@ngrx/store-devtools@^5.0.1": - version "5.0.1" - resolved "https://registry.yarnpkg.com/@ngrx/store-devtools/-/store-devtools-5.0.1.tgz#f1bf5c54d4b2a512afd970e8114057b44e0d3c16" +"@ngrx/router-store@^5.2.0": + version "5.2.0" + resolved "https://registry.yarnpkg.com/@ngrx/router-store/-/router-store-5.2.0.tgz#bf4b174ce19a36eba8211fc1ddeaf1e35ae74368" -"@ngrx/store@^5.0.0": - version "5.0.0" - resolved "https://registry.yarnpkg.com/@ngrx/store/-/store-5.0.0.tgz#483a955ea99a63ee083675ce0596d505e5501747" +"@ngrx/store-devtools@^5.2.0": + version "5.2.0" + 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": version "1.1.0" @@ -291,10 +295,6 @@ version "0.0.30" 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: version "1.3.2" resolved "https://registry.yarnpkg.com/JSONStream/-/JSONStream-1.3.2.tgz#c102371b6ec3a7cf3b847ca00c20bb0fce4c6dea" @@ -7283,10 +7283,6 @@ urix@^0.1.0: version "0.1.0" 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: version "0.6.2" resolved "https://registry.yarnpkg.com/url-loader/-/url-loader-0.6.2.tgz#a007a7109620e9d988d14bce677a1decb9a993f7"