144 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';
import { Actions, ofType, createEffect } from '@ngrx/effects';
import { of } from 'rxjs';
2019-09-19 17:03:39 +09:00
import { catchError, exhaustMap, map, tap, take } from 'rxjs/operators';
2019-09-18 15:02:21 +09:00
import {
PiService,
Login2Response,
ResponseStatus
} 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 {
login,
loginSuccess,
loginFailure,
loginRedirect,
2019-09-19 17:03:39 +09:00
logout,
logoutConfirmation,
logoutConfirmationDismiss
2019-09-18 15:02:21 +09:00
} from './actions';
import { LoginInfo } from '../../../types';
2019-09-19 18:22:13 +09:00
import { AppAuthenticationService } from '../../../services/authentication.service';
2019-09-18 15:02:21 +09:00
@Injectable()
export class Effects {
login$ = createEffect(() =>
this.actions$.pipe(
ofType(login),
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 (res.status === ResponseStatus.Fail) {
return loginFailure({ error: 'Failed' });
} else {
return loginSuccess({
loginInfo: params.loginInfo,
rememberMe: params.rememberMe,
login2Response: res
});
}
}),
catchError(error => of(loginFailure({ error })))
)
)
)
);
2019-09-19 14:15:43 +09:00
loginSuccess$ = createEffect(
() =>
this.actions$.pipe(
ofType(loginSuccess),
tap(params => {
this.nativeService
.checkForUpdates()
.pipe(
take(1),
map((update: boolean) => {
if (!update) {
2019-09-19 18:22:13 +09:00
this.appAuthenticationService.login(
2019-09-19 14:15:43 +09:00
params.loginInfo,
params.rememberMe
);
this.router.navigate(['/messenger']);
}
}),
catchError(error => of(error))
)
.subscribe();
})
),
{ 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,
@Inject(UCAP_NATIVE_SERVICE) private nativeService: NativeService
2019-09-18 15:02:21 +09:00
) {}
}