This commit is contained in:
crusader 2018-03-07 18:59:33 +09:00
parent 47428f8ec5
commit 82bd4ef887
10 changed files with 105 additions and 39 deletions

View File

@ -12,6 +12,7 @@ import 'rxjs/add/operator/take';
import 'rxjs/add/operator/map'; import 'rxjs/add/operator/map';
import * as AuthStore from 'packages/member/store/auth'; import * as AuthStore from 'packages/member/store/auth';
import { AuthSelector } from 'packages/member/store';
@Injectable() @Injectable()
export class AuthGuard implements CanActivate, CanActivateChild { export class AuthGuard implements CanActivate, CanActivateChild {
@ -21,7 +22,7 @@ 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(AuthStore.isSignin) .select(AuthSelector.isSignin)
.map(isSignin => { .map(isSignin => {
if (!isSignin) { if (!isSignin) {
this.store.dispatch(new AuthStore.SigninRedirect()); this.store.dispatch(new AuthStore.SigninRedirect());
@ -35,7 +36,7 @@ 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(AuthStore.isSignin) .select(AuthSelector.isSignin)
.map(isSignin => { .map(isSignin => {
if (!isSignin) { if (!isSignin) {
this.store.dispatch(new AuthStore.SigninRedirect()); this.store.dispatch(new AuthStore.SigninRedirect());

View File

@ -19,6 +19,9 @@
{{ formErrors.password }} {{ formErrors.password }}
</div> </div>
<!--[disabled]="!signinForm.valid"--> <!--[disabled]="!signinForm.valid"-->
<p *ngIf="errorMessage">
{{ errorMessage }}
</p>
<button mat-raised-button color="accent" type="submit">Sign In</button> <button mat-raised-button color="accent" type="submit">Sign In</button>
</form> </form>
</mat-card-content> </mat-card-content>

View File

@ -1,9 +1,10 @@
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 { Store, select } from '@ngrx/store';
import * as AuthStore from '../../store/auth'; import * as AuthStore from '../../store/auth';
import { AuthSelector } from '../../store';
@Component({ @Component({
selector: 'of-member-signin', selector: 'of-member-signin',
@ -12,6 +13,11 @@ import * as AuthStore from '../../store/auth';
}) })
export class SigninComponent implements OnInit { export class SigninComponent implements OnInit {
pending$ = this.store.pipe(select(AuthSelector.isPending));
error$ = this.store.pipe(select(AuthSelector.getError));
errorMessage: string | null;
signinForm: FormGroup; signinForm: FormGroup;
formErrors = { formErrors = {
'email': '', 'email': '',
@ -27,16 +33,36 @@ export class SigninComponent implements OnInit {
ngOnInit() { ngOnInit() {
this.initForm(); this.initForm();
this.pending$.subscribe((pending: boolean) => {
if (pending) {
this.signinForm.disable();
} else {
this.signinForm.enable();
}
});
this.error$.subscribe((error) => {
if (error) {
this.errorMessage = error.exception;
} else {
this.errorMessage = null;
}
});
} }
initForm() { initForm() {
this.signinForm = this.formBuilder.group({ this.signinForm = this.formBuilder.group({
'email': ['', [ 'email': [
'',
[
Validators.required, Validators.required,
Validators.email Validators.email
] ]
], ],
'password': ['', [ 'password': [
'',
[
Validators.pattern('^(?=.*[0-9])(?=.*[a-zA-Z])([a-zA-Z0-9]+)$'), Validators.pattern('^(?=.*[0-9])(?=.*[a-zA-Z])([a-zA-Z0-9]+)$'),
Validators.minLength(6), Validators.minLength(6),
Validators.maxLength(25) Validators.maxLength(25)

View File

@ -5,8 +5,6 @@ import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { MaterialModule } from 'app/commons/ui/material/material.module'; import { MaterialModule } from 'app/commons/ui/material/material.module';
// import { AuthGuard } from './guard/auth.guard';
import { MemberStoreModule } from './member-store.module'; import { MemberStoreModule } from './member-store.module';
import { COMPONENTS } from './component'; import { COMPONENTS } from './component';

View File

@ -1,3 +1,5 @@
import { MetaMemberStatus } from 'packages/meta/model';
export interface Member { export interface Member {
id?: number; id?: number;
email?: string; email?: string;
@ -6,5 +8,5 @@ export interface Member {
phone?: string; phone?: string;
companyName?: string; companyName?: string;
createDate?: Date; createDate?: Date;
status?: string; status?: MetaMemberStatus;
} }

View File

@ -0,0 +1,6 @@
export enum MemberStatus {
NOAUTH = 1,
NORMAL = 2,
DORMANCY = 3,
WITHDRAWAL = 4,
}

View File

@ -47,18 +47,6 @@ export class Effects {
return of(new SigninFailure(error)); return of(new SigninFailure(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 }) @Effect({ dispatch: false })
signinSuccess$ = this.actions$ signinSuccess$ = this.actions$
.ofType(ActionType.SigninSuccess) .ofType(ActionType.SigninSuccess)

View File

@ -1,5 +1,10 @@
import { ErrorResponse } from 'packages/commons/service/error-response'; import {
createSelector,
MemoizedSelector,
} from '@ngrx/store';
import { ErrorResponse } from 'packages/commons/service/error-response';
import { Domain } from 'packages/domain/model';
import { Member } from '../../model'; import { Member } from '../../model';
export interface State { export interface State {
@ -7,6 +12,7 @@ export interface State {
error: ErrorResponse | null; error: ErrorResponse | null;
isPending: boolean; isPending: boolean;
member: Member | null; member: Member | null;
domain: Domain | null;
} }
export const initialState: State = { export const initialState: State = {
@ -14,9 +20,16 @@ export const initialState: State = {
error: null, error: null,
isPending: false, isPending: false,
member: null, member: null,
domain: null,
}; };
export const isSignin = (state: State) => state.isSignin; export class StateSelector {
export const getMember = (state: State) => state.member; public constructor(private _selector: MemoizedSelector<any, any>) {
export const getError = (state: State) => state.error; }
export const isPending = (state: State) => state.isPending;
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

@ -1,3 +1,11 @@
import {
createSelector,
createFeatureSelector,
ActionReducerMap,
} from '@ngrx/store';
import { MODULE } from '../member.constant';
import * as AuthStore from './auth'; import * as AuthStore from './auth';
import * as SignupStore from './signup'; import * as SignupStore from './signup';
@ -15,3 +23,18 @@ export const EFFECTS = [
AuthStore.Effects, AuthStore.Effects,
SignupStore.Effects, SignupStore.Effects,
]; ];
export const selectMemberState = createFeatureSelector<State>(MODULE.name);
export const selectMemberAuthState = createSelector(
selectMemberState,
(state: State) => state.auth
);
export const AuthSelector = new AuthStore.StateSelector(selectMemberAuthState);
export const selectMemberSignupState = createSelector(
selectMemberState,
(state: State) => state.signup
);

View File

@ -1,26 +1,32 @@
import { Component, OnInit } from '@angular/core'; import { Component, OnInit, AfterContentInit } from '@angular/core';
import { Router } from '@angular/router';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { Store } from '@ngrx/store';
import { Router } from '@angular/router';
import { Store } from '@ngrx/store';
import * as NoAuthProbeStore from '../../store/noauth-probe'; import * as NoAuthProbeStore from '../../store/noauth-probe';
import { Domain } from 'packages/domain/model';
@Component({ @Component({
selector: 'of-noauth-list', selector: 'of-noauth-list',
templateUrl: './list.component.html', templateUrl: './list.component.html',
styleUrls: ['./list.component.scss'] styleUrls: ['./list.component.scss']
}) })
export class ListComponent implements OnInit { export class ListComponent implements OnInit, AfterContentInit {
constructor( constructor(
private router: Router, private router: Router,
private store: Store<NoAuthProbeStore.State>, private store: Store<NoAuthProbeStore.State>,
private formBuilder: FormBuilder,
) { } ) { }
ngOnInit() { ngOnInit() {
} }
ngAfterContentInit() {
const domain: Domain = {
id: 1,
};
this.store.dispatch(new NoAuthProbeStore.ReadAllByDomain(domain));
}
} }