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-24 18:42:53 +09:00
|
|
|
import {
|
|
|
|
catchError,
|
|
|
|
exhaustMap,
|
|
|
|
map,
|
|
|
|
tap,
|
|
|
|
take,
|
|
|
|
switchMap
|
|
|
|
} 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 {
|
|
|
|
loginSuccess,
|
|
|
|
loginFailure,
|
|
|
|
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';
|
|
|
|
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 {
|
2019-09-24 09:23:30 +09:00
|
|
|
webLogin$ = createEffect(() =>
|
2019-09-18 15:02:21 +09:00
|
|
|
this.actions$.pipe(
|
2019-09-24 09:23:30 +09:00
|
|
|
ofType(webLogin),
|
2019-09-18 15:02:21 +09:00
|
|
|
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) => {
|
2019-09-24 14:53:22 +09:00
|
|
|
if ('success' !== res.status.toLowerCase()) {
|
2019-09-24 09:23:30 +09:00
|
|
|
return webLoginFailure({ error: 'Failed' });
|
2019-09-18 15:02:21 +09:00
|
|
|
} else {
|
2019-09-24 09:23:30 +09:00
|
|
|
return webLoginSuccess({
|
2019-09-18 15:02:21 +09:00
|
|
|
loginInfo: params.loginInfo,
|
|
|
|
rememberMe: params.rememberMe,
|
|
|
|
login2Response: res
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}),
|
2019-09-24 09:23:30 +09:00
|
|
|
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,
|
|
|
|
@Inject(UCAP_NATIVE_SERVICE) private nativeService: NativeService
|
2019-09-18 15:02:21 +09:00
|
|
|
) {}
|
|
|
|
}
|