From 0ddadc8f4cea44f41878fdd2c04447e1e97f0ba1 Mon Sep 17 00:00:00 2001
From: Christoph Reiter <reiter.christoph@gmail.com>
Date: Wed, 27 Jan 2021 13:28:00 +0100
Subject: [PATCH] Remove dbp-auth-person-init event

---
 packages/auth/README.md                         |  4 ----
 packages/auth/src/auth-keycloak.js              |  7 +------
 packages/auth/src/login-button.js               |  1 -
 .../src/dbp-person-profile-demo.js              | 17 +++++++++++++----
 4 files changed, 14 insertions(+), 15 deletions(-)

diff --git a/packages/auth/README.md b/packages/auth/README.md
index 7b78c1e5..41364011 100644
--- a/packages/auth/README.md
+++ b/packages/auth/README.md
@@ -36,10 +36,6 @@ npm i @dbp-toolkit/auth
 - `silent-check-sso-redirect-uri` (optional): URI or path to a separate page for checking the login session in an iframe, see https://www.keycloak.org/docs/latest/securing_apps/#_javascript_adapter
 - `scope` (optional): Space separated list of scopes to request. These scopes get added in addition to the default ones, assuming the scope is in the optional scopes list of the Keycloak client in use.
 
-### Events to listen to
-
-- `dbp-auth-person-init`: Keycloak person init event - the person entity was loaded from the server
-
 ## Login Button
 
 ### Usage
diff --git a/packages/auth/src/auth-keycloak.js b/packages/auth/src/auth-keycloak.js
index 160ea895..2be63569 100644
--- a/packages/auth/src/auth-keycloak.js
+++ b/packages/auth/src/auth-keycloak.js
@@ -16,8 +16,7 @@ import {AdapterLitElement} from "@dbp-toolkit/provider/src/adapter-lit-element";
  *   window.DBPAuthTokenParsed: Keycloak token content
  *   window.DBPUserFullName: Full name of the user
  *   window.DBPPersonId: Person identifier of the user
- *   window.DBPPerson: Person json object of the user (optional, enable by setting the `load-person` attribute,
- *                     which will dispatch a `dbp-auth-person-init` event when loaded)
+ *   window.DBPPerson: Person json object of the user (optional, enable by setting the `load-person` attribute)
  */
 export class AuthKeycloak extends AdapterLitElement {
     constructor() {
@@ -43,9 +42,6 @@ export class AuthKeycloak extends AdapterLitElement {
         this.scope = null;
         this.idpHint = '';
 
-        // Create the events
-        this.personInitEvent = new CustomEvent("dbp-auth-person-init", { "detail": "KeyCloak person init event", bubbles: true, composed: true });
-
         this._onKCChanged = this._onKCChanged.bind(this);
     }
 
@@ -127,7 +123,6 @@ export class AuthKeycloak extends AdapterLitElement {
                 .then((person) => {
                     that.person = person;
                     window.DBPPerson = person;
-                    that.dispatchEvent(that.personInitEvent);
                     this._setLoginStatus(this._loginStatus, true);
                 });
             }, {}, that.lang);
diff --git a/packages/auth/src/login-button.js b/packages/auth/src/login-button.js
index 435916e4..8465c587 100644
--- a/packages/auth/src/login-button.js
+++ b/packages/auth/src/login-button.js
@@ -88,7 +88,6 @@ export class LoginButton extends ScopedElementsMixin(LitElement) {
 
         this._bus = new EventBus();
         this._bus.subscribe('auth-update', (data) => {
-            console.log(data);
             this._loginData = data;
         });
     }
diff --git a/packages/person-profile/src/dbp-person-profile-demo.js b/packages/person-profile/src/dbp-person-profile-demo.js
index 375636e5..4328ef6a 100644
--- a/packages/person-profile/src/dbp-person-profile-demo.js
+++ b/packages/person-profile/src/dbp-person-profile-demo.js
@@ -8,6 +8,7 @@ import * as commonUtils from '@dbp-toolkit/common/utils';
 import * as commonStyles from '@dbp-toolkit/common/styles';
 import $ from 'jquery';
 import {PersonSelect} from '@dbp-toolkit/person-select';
+import {EventBus} from '@dbp-toolkit/common';
 
 export class PersonProfileDemo extends ScopedElementsMixin(DBPLitElement) {
     constructor() {
@@ -43,11 +44,14 @@ export class PersonProfileDemo extends ScopedElementsMixin(DBPLitElement) {
         i18n.changeLanguage(this.lang);
         const that = this;
 
-        this.updateComplete.then(()=>{
-            window.addEventListener("dbp-auth-person-init", () => {
-                that.person = window.DBPPersonId;
-            });
+        this._bus = new EventBus();
+        this._bus.subscribe('auth-update', (data) => {
+            if (data.person) {
+                this.person = data.person.identifier;
+            }
+        });
 
+        this.updateComplete.then(()=>{
             const personSelect = that._(this.constructor.getScopedTagName('dbp-person-select'));
             personSelect.onchange = function () {
                 that.selectedPerson = $(this).data("object").identifier;
@@ -56,6 +60,11 @@ export class PersonProfileDemo extends ScopedElementsMixin(DBPLitElement) {
         });
     }
 
+    disconnectedCallback() {
+        this._bus.close();
+        super.disconnectedCallback();
+    }
+
     static get styles() {
         // language=css
         return css`
-- 
GitLab