From eb99fe58dd1dfb7f24dc5ee45f4af13c52053cac Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Thu, 24 Jan 2019 08:54:33 -0500 Subject: [PATCH] refresh token and UI when license updated --- .../layouts/organization-layout.component.ts | 25 +++++++++++++++++-- .../organization-billing.component.ts | 8 +++--- src/app/settings/update-license.component.ts | 9 +++++-- 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/src/app/layouts/organization-layout.component.ts b/src/app/layouts/organization-layout.component.ts index 792cf613ae..1f80788a9d 100644 --- a/src/app/layouts/organization-layout.component.ts +++ b/src/app/layouts/organization-layout.component.ts @@ -1,23 +1,30 @@ import { Component, + NgZone, + OnDestroy, OnInit, } from '@angular/core'; import { ActivatedRoute } from '@angular/router'; +import { BroadcasterService } from 'jslib/angular/services/broadcaster.service'; + import { UserService } from 'jslib/abstractions/user.service'; import { Organization } from 'jslib/models/domain/organization'; +const BroadcasterSubscriptionId = 'OrganizationLayoutComponent'; + @Component({ selector: 'app-organization-layout', templateUrl: 'organization-layout.component.html', }) -export class OrganizationLayoutComponent implements OnInit { +export class OrganizationLayoutComponent implements OnInit, OnDestroy { organization: Organization; private organizationId: string; - constructor(private route: ActivatedRoute, private userService: UserService) { } + constructor(private route: ActivatedRoute, private userService: UserService, + private broadcasterService: BroadcasterService, private ngZone: NgZone) { } ngOnInit() { document.body.classList.remove('layout_frontend'); @@ -25,6 +32,20 @@ export class OrganizationLayoutComponent implements OnInit { this.organizationId = params.organizationId; await this.load(); }); + + this.broadcasterService.subscribe(BroadcasterSubscriptionId, (message: any) => { + this.ngZone.run(async () => { + switch (message.command) { + case 'updatedOrgLicense': + await this.load(); + break; + } + }); + }); + } + + ngOnDestroy() { + this.broadcasterService.unsubscribe(BroadcasterSubscriptionId); } async load() { diff --git a/src/app/organizations/settings/organization-billing.component.ts b/src/app/organizations/settings/organization-billing.component.ts index 45956dfc93..c3f95d2a31 100644 --- a/src/app/organizations/settings/organization-billing.component.ts +++ b/src/app/organizations/settings/organization-billing.component.ts @@ -14,6 +14,7 @@ import { OrganizationBillingResponse } from 'jslib/models/response/organizationB import { ApiService } from 'jslib/abstractions/api.service'; import { I18nService } from 'jslib/abstractions/i18n.service'; +import { MessagingService } from 'jslib/abstractions/messaging.service'; import { PlatformUtilsService } from 'jslib/abstractions/platformUtils.service'; import { TokenService } from 'jslib/abstractions/token.service'; @@ -48,7 +49,7 @@ export class OrganizationBillingComponent implements OnInit { constructor(private tokenService: TokenService, private apiService: ApiService, private platformUtilsService: PlatformUtilsService, private i18nService: I18nService, private analytics: Angulartics2, private toasterService: ToasterService, - private route: ActivatedRoute) { + private messagingService: MessagingService, private route: ActivatedRoute) { this.selfHosted = platformUtilsService.isSelfHost(); } @@ -159,10 +160,11 @@ export class OrganizationBillingComponent implements OnInit { } catch { } } - closeUpdateLicense(load: boolean) { + closeUpdateLicense(updated: boolean) { this.showUpdateLicense = false; - if (load) { + if (updated) { this.load(); + this.messagingService.send('updatedOrgLicense'); } } diff --git a/src/app/settings/update-license.component.ts b/src/app/settings/update-license.component.ts index 0c52998584..e9f5ca071a 100644 --- a/src/app/settings/update-license.component.ts +++ b/src/app/settings/update-license.component.ts @@ -38,12 +38,17 @@ export class UpdateLicenseComponent { const fd = new FormData(); fd.append('license', files[0]); + let updatePromise: Promise = null; if (this.organizationId == null) { - this.formPromise = this.apiService.postAccountLicense(fd); + updatePromise = this.apiService.postAccountLicense(fd); } else { - this.formPromise = this.apiService.postOrganizationLicenseUpdate(this.organizationId, fd); + updatePromise = this.apiService.postOrganizationLicenseUpdate(this.organizationId, fd); } + this.formPromise = updatePromise.then(() => { + return this.apiService.refreshIdentityToken(); + }); + await this.formPromise; this.analytics.eventTrack.next({ action: 'Updated License' }); this.toasterService.popAsync('success', null, this.i18nService.t('updatedLicense'));