138 lines
3.7 KiB
TypeScript
Raw Normal View History

2019-09-18 15:02:21 +09:00
import { Injectable, Inject } from '@angular/core';
import { Router } from '@angular/router';
2019-10-02 14:34:17 +09:00
import { of } from 'rxjs';
import { catchError, exhaustMap, map, tap, switchMap } from 'rxjs/operators';
2019-09-18 15:02:21 +09:00
2019-09-27 12:53:21 +09:00
import { Actions, ofType, createEffect } from '@ngrx/effects';
2019-10-02 14:34:17 +09:00
import { PiService, Login2Response } from '@ucap-webmessenger/pi';
2019-09-19 17:03:39 +09:00
import { NativeService, UCAP_NATIVE_SERVICE } from '@ucap-webmessenger/native';
import {
DialogService,
ConfirmDialogComponent,
ConfirmDialogData,
ConfirmDialogResult
} from '@ucap-webmessenger/ui';
2019-09-18 15:02:21 +09:00
import {
loginRedirect,
2019-09-19 17:03:39 +09:00
logout,
logoutConfirmation,
2019-09-24 09:23:30 +09:00
logoutConfirmationDismiss,
webLogin,
webLoginSuccess,
webLoginFailure
2019-09-18 15:02:21 +09:00
} from './actions';
2019-10-02 14:34:17 +09:00
import { LoginInfo } from '../../../types';
2019-09-19 18:22:13 +09:00
import { AppAuthenticationService } from '../../../services/authentication.service';
2019-09-27 12:53:21 +09:00
import { NGXLogger } from 'ngx-logger';
2019-09-18 15:02:21 +09:00
@Injectable()
export class Effects {
2019-10-02 14:34:17 +09:00
webLogin$ = createEffect(() =>
this.actions$.pipe(
ofType(webLogin),
map(action => action),
exhaustMap((params: { loginInfo: LoginInfo; rememberMe: boolean }) =>
this.piService
.login2({
loginId: params.loginInfo.loginId,
loginPw: params.loginInfo.loginPw,
companyCode: params.loginInfo.companyCode
})
.pipe(
map((res: Login2Response) => {
if ('success' !== res.status.toLowerCase()) {
return webLoginFailure({ error: 'Failed' });
} else {
return webLoginSuccess({
loginInfo: params.loginInfo,
rememberMe: params.rememberMe,
login2Response: res
});
}
}),
catchError(error => of(webLoginFailure({ error })))
)
)
)
2019-09-18 15:02:21 +09:00
);
2019-09-24 09:23:30 +09:00
webLoginSuccess$ = createEffect(
2019-09-19 14:15:43 +09:00
() =>
this.actions$.pipe(
2019-09-24 09:23:30 +09:00
ofType(webLoginSuccess),
2019-09-24 18:42:53 +09:00
switchMap(params =>
this.nativeService.checkForUpdates().pipe(
map((update: boolean) => {
if (!update) {
this.appAuthenticationService.login(
params.loginInfo,
params.rememberMe
);
this.router.navigate(['/messenger']);
}
}),
catchError(error => of(error))
)
)
2019-09-19 14:15:43 +09:00
),
{ dispatch: false }
2019-09-18 15:02:21 +09:00
);
loginRedirect$ = createEffect(
() =>
this.actions$.pipe(
2019-09-19 17:03:39 +09:00
ofType(loginRedirect),
2019-09-18 15:02:21 +09:00
tap(authed => {
this.router.navigate(['/account/login']);
})
),
{ dispatch: false }
);
2019-09-19 17:03:39 +09:00
logout$ = createEffect(() =>
this.actions$.pipe(
ofType(logout),
map(action => action),
map(() => {
2019-09-19 18:22:13 +09:00
this.appAuthenticationService.logout();
2019-09-19 17:03:39 +09:00
return loginRedirect();
})
)
);
logoutConfirmation$ = createEffect(() =>
this.actions$.pipe(
ofType(logoutConfirmation),
exhaustMap(async () => {
const result = await this.dialogService.open<
ConfirmDialogComponent,
ConfirmDialogData,
ConfirmDialogResult
>(ConfirmDialogComponent, {
width: '220px',
data: {
title: 'Logout',
message: 'Logout ?'
}
});
return result.choice;
}),
map(result => (result ? logout() : logoutConfirmationDismiss()))
)
);
2019-09-18 15:02:21 +09:00
constructor(
private actions$: Actions,
2019-09-19 17:03:39 +09:00
private router: Router,
2019-09-18 15:02:21 +09:00
private piService: PiService,
2019-09-19 18:22:13 +09:00
private appAuthenticationService: AppAuthenticationService,
2019-09-19 17:03:39 +09:00
private dialogService: DialogService,
2019-09-27 12:53:21 +09:00
@Inject(UCAP_NATIVE_SERVICE) private nativeService: NativeService,
private logger: NGXLogger
2019-09-18 15:02:21 +09:00
) {}
}