2021-06-15 21:02:04 +02:00
|
|
|
import {
|
|
|
|
Directive,
|
|
|
|
OnInit,
|
|
|
|
} from '@angular/core';
|
|
|
|
import {
|
|
|
|
ActivatedRoute,
|
|
|
|
Router,
|
|
|
|
} from '@angular/router';
|
|
|
|
|
2021-10-15 00:59:43 +02:00
|
|
|
import { first } from 'rxjs/operators';
|
|
|
|
|
2021-06-15 21:02:04 +02:00
|
|
|
import { I18nService } from 'jslib-common/abstractions/i18n.service';
|
2021-12-07 20:41:45 +01:00
|
|
|
import { PlatformUtilsService } from 'jslib-common/abstractions/platformUtils.service';
|
2021-06-15 21:02:04 +02:00
|
|
|
import { StateService } from 'jslib-common/abstractions/state.service';
|
|
|
|
|
|
|
|
@Directive()
|
|
|
|
export abstract class BaseAcceptComponent implements OnInit {
|
|
|
|
loading = true;
|
|
|
|
authed = false;
|
|
|
|
email: string;
|
|
|
|
actionPromise: Promise<any>;
|
|
|
|
|
|
|
|
protected requiredParameters: string[] = [];
|
|
|
|
protected failedShortMessage = 'inviteAcceptFailedShort';
|
|
|
|
protected failedMessage = 'inviteAcceptFailed';
|
|
|
|
|
2021-12-07 20:41:45 +01:00
|
|
|
constructor(protected router: Router, protected platformUtilService: PlatformUtilsService,
|
2021-06-15 21:02:04 +02:00
|
|
|
protected i18nService: I18nService, protected route: ActivatedRoute,
|
2021-12-14 17:10:26 +01:00
|
|
|
protected stateService: StateService) { }
|
2021-06-15 21:02:04 +02:00
|
|
|
|
|
|
|
abstract authedHandler(qParams: any): Promise<void>;
|
|
|
|
abstract unauthedHandler(qParams: any): Promise<void>;
|
|
|
|
|
|
|
|
ngOnInit() {
|
2021-10-15 00:59:43 +02:00
|
|
|
this.route.queryParams.pipe(first()).subscribe(async qParams => {
|
2021-12-14 17:10:26 +01:00
|
|
|
await this.stateService.setLoginRedirect(null);
|
2021-06-15 21:02:04 +02:00
|
|
|
let error = this.requiredParameters.some(e => qParams?.[e] == null || qParams[e] === '');
|
|
|
|
let errorMessage: string = null;
|
|
|
|
if (!error) {
|
2021-12-14 17:10:26 +01:00
|
|
|
this.authed = await this.stateService.getIsAuthenticated();
|
2021-06-15 21:02:04 +02:00
|
|
|
|
|
|
|
if (this.authed) {
|
|
|
|
try {
|
|
|
|
await this.authedHandler(qParams);
|
|
|
|
} catch (e) {
|
|
|
|
error = true;
|
|
|
|
errorMessage = e.message;
|
|
|
|
}
|
|
|
|
} else {
|
2021-12-14 17:10:26 +01:00
|
|
|
await this.stateService.setLoginRedirect({
|
2021-06-15 21:02:04 +02:00
|
|
|
route: this.getRedirectRoute(),
|
|
|
|
qParams: qParams,
|
|
|
|
});
|
|
|
|
|
|
|
|
this.email = qParams.email;
|
|
|
|
await this.unauthedHandler(qParams);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error) {
|
2021-12-07 20:41:45 +01:00
|
|
|
const message = errorMessage != null ? this.i18nService.t(this.failedShortMessage, errorMessage) :
|
|
|
|
this.i18nService.t(this.failedMessage);
|
|
|
|
this.platformUtilService.showToast('error', null, message, {timeout: 10000});
|
2021-06-15 21:02:04 +02:00
|
|
|
this.router.navigate(['/']);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.loading = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getRedirectRoute() {
|
|
|
|
const urlTree = this.router.parseUrl(this.router.url);
|
|
|
|
urlTree.queryParams = {};
|
|
|
|
return urlTree.toString();
|
|
|
|
}
|
|
|
|
}
|