2018-07-12 22:05:42 +02:00
|
|
|
import {
|
|
|
|
Component,
|
|
|
|
OnInit,
|
|
|
|
} from '@angular/core';
|
|
|
|
import {
|
|
|
|
ActivatedRoute,
|
|
|
|
Router,
|
|
|
|
} from '@angular/router';
|
|
|
|
|
|
|
|
import {
|
|
|
|
Toast,
|
|
|
|
ToasterService,
|
|
|
|
} from 'angular2-toaster';
|
|
|
|
|
|
|
|
import { ApiService } from 'jslib/abstractions/api.service';
|
|
|
|
import { I18nService } from 'jslib/abstractions/i18n.service';
|
2018-07-13 16:51:52 +02:00
|
|
|
import { StateService } from 'jslib/abstractions/state.service';
|
2018-07-12 22:05:42 +02:00
|
|
|
import { UserService } from 'jslib/abstractions/user.service';
|
|
|
|
|
|
|
|
import { OrganizationUserAcceptRequest } from 'jslib/models/request/organizationUserAcceptRequest';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'app-accept-organization',
|
|
|
|
templateUrl: 'accept-organization.component.html',
|
|
|
|
})
|
|
|
|
export class AcceptOrganizationComponent implements OnInit {
|
|
|
|
loading = true;
|
|
|
|
authed = false;
|
|
|
|
orgName: string;
|
|
|
|
email: string;
|
|
|
|
actionPromise: Promise<any>;
|
|
|
|
|
|
|
|
constructor(private router: Router, private toasterService: ToasterService,
|
|
|
|
private i18nService: I18nService, private route: ActivatedRoute,
|
2018-07-13 16:51:52 +02:00
|
|
|
private apiService: ApiService, private userService: UserService,
|
|
|
|
private stateService: StateService) { }
|
2018-07-12 22:05:42 +02:00
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
let fired = false;
|
2021-02-03 18:41:33 +01:00
|
|
|
this.route.queryParams.subscribe(async qParams => {
|
2018-07-12 22:05:42 +02:00
|
|
|
if (fired) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fired = true;
|
2018-07-13 16:51:52 +02:00
|
|
|
await this.stateService.remove('orgInvitation');
|
|
|
|
let error = qParams.organizationId == null || qParams.organizationUserId == null || qParams.token == null;
|
2020-02-28 21:27:02 +01:00
|
|
|
let errorMessage: string = null;
|
2018-07-12 22:05:42 +02:00
|
|
|
if (!error) {
|
|
|
|
this.authed = await this.userService.isAuthenticated();
|
|
|
|
if (this.authed) {
|
|
|
|
const request = new OrganizationUserAcceptRequest();
|
|
|
|
request.token = qParams.token;
|
|
|
|
try {
|
|
|
|
this.actionPromise = this.apiService.postOrganizationUserAccept(qParams.organizationId,
|
|
|
|
qParams.organizationUserId, request);
|
|
|
|
await this.actionPromise;
|
|
|
|
const toast: Toast = {
|
|
|
|
type: 'success',
|
|
|
|
title: this.i18nService.t('inviteAccepted'),
|
|
|
|
body: this.i18nService.t('inviteAcceptedDesc'),
|
|
|
|
timeout: 10000,
|
|
|
|
};
|
|
|
|
this.toasterService.popAsync(toast);
|
|
|
|
this.router.navigate(['/vault']);
|
2020-02-28 21:27:02 +01:00
|
|
|
} catch (e) {
|
2018-07-12 22:05:42 +02:00
|
|
|
error = true;
|
2020-02-28 21:27:02 +01:00
|
|
|
errorMessage = e.message;
|
2018-07-12 22:05:42 +02:00
|
|
|
}
|
|
|
|
} else {
|
2018-07-13 16:51:52 +02:00
|
|
|
await this.stateService.save('orgInvitation', qParams);
|
2018-07-12 22:05:42 +02:00
|
|
|
this.email = qParams.email;
|
|
|
|
this.orgName = qParams.organizationName;
|
2018-07-25 15:10:24 +02:00
|
|
|
if (this.orgName != null) {
|
|
|
|
// Fix URL encoding of space issue with Angular
|
2018-07-25 15:13:46 +02:00
|
|
|
this.orgName = this.orgName.replace(/\+/g, ' ');
|
2018-07-25 15:10:24 +02:00
|
|
|
}
|
2018-07-12 22:05:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error) {
|
2020-02-28 21:27:02 +01:00
|
|
|
const toast: Toast = {
|
|
|
|
type: 'error',
|
|
|
|
title: null,
|
|
|
|
body: errorMessage != null ? this.i18nService.t('inviteAcceptFailedShort', errorMessage) :
|
|
|
|
this.i18nService.t('inviteAcceptFailed'),
|
|
|
|
timeout: 10000,
|
|
|
|
};
|
|
|
|
this.toasterService.popAsync(toast);
|
2018-07-12 22:05:42 +02:00
|
|
|
this.router.navigate(['/']);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.loading = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|