From 58ab1d8b966a555c3c8ae4b0c81361fd02f5d43b Mon Sep 17 00:00:00 2001
From: Patrizio Bekerle <patrizio@bekerle.com>
Date: Tue, 23 Mar 2021 07:32:13 +0100
Subject: [PATCH] Add context attribute to organization select

---
 packages/organization-select/README.md                   | 4 ++++
 .../src/dbp-organization-select-demo.js                  | 2 +-
 packages/organization-select/src/organization-select.js  | 9 ++++++++-
 3 files changed, 13 insertions(+), 2 deletions(-)

diff --git a/packages/organization-select/README.md b/packages/organization-select/README.md
index 4bd463aa..c6e6b576 100644
--- a/packages/organization-select/README.md
+++ b/packages/organization-select/README.md
@@ -22,6 +22,10 @@ npm i @dbp-toolkit/organization-select
 - `value` (optional): api path of organization to preload the selector with
     - example `<dbp-organization-select value="/people/testuser"></dbp-organization-select>`
     - the `value` will also be set automatically when an organization is chosen in the selector
+- `context` (optional): the context of the returned organizations
+    - example `<dbp-organization-select context="libray-manager"></dbp-organization-select>` returns all organizations
+      where the current user is library manager
+    - if no `context` is set all organizations are returned that are in a relation with the current user
 - `data-object` (read-only): when an organization is selected the organization object will be set as json string
     - example `<dbp-organization-select data-object="{"@id":"/organizations/1190-F2050","@type":"http://schema.org/Organization","identifier":"1190-F2050","name":"Institut für Stahlbau","url":"https://online.tugraz.at/tug_online/wborg.display?pOrgNr=1190","alternateName":"F2050"}"></dbp-organization-select>`
 - `auth` object: you need to set that object property for the auth token
diff --git a/packages/organization-select/src/dbp-organization-select-demo.js b/packages/organization-select/src/dbp-organization-select-demo.js
index 6960e575..3cd14e4b 100644
--- a/packages/organization-select/src/dbp-organization-select-demo.js
+++ b/packages/organization-select/src/dbp-organization-select-demo.js
@@ -81,7 +81,7 @@ export class OrganizationSelectDemo extends ScopedElementsMixin(DBPLitElement) {
                         <div class="field">
                             <label class="label">Organization 2</label>
                             <div class="control">
-                                <dbp-organization-select subscribe="auth" lang="${this.lang}" entry-point-url="${this.entryPointUrl}"></dbp-organization-select>
+                                <dbp-organization-select subscribe="auth" lang="${this.lang}" entry-point-url="${this.entryPointUrl}" context="test"></dbp-organization-select>
                             </div>
                         </div>
                     </form>
diff --git a/packages/organization-select/src/organization-select.js b/packages/organization-select/src/organization-select.js
index e7019bae..f368d8f2 100644
--- a/packages/organization-select/src/organization-select.js
+++ b/packages/organization-select/src/organization-select.js
@@ -24,6 +24,7 @@ export class OrganizationSelect extends AdapterLitElement {
         this.selectId = 'select-organization-' + commonUtils.makeId(24);
         this.cache = {};
         this.value = '';
+        this.context = '';
     }
 
     static get properties() {
@@ -32,6 +33,7 @@ export class OrganizationSelect extends AdapterLitElement {
             lang: {type: String},
             entryPointUrl: { type: String, attribute: 'entry-point-url' },
             value: {type: String, reflect: true},
+            context: {type: String},
             auth: { type: Object },
         };
     }
@@ -186,6 +188,11 @@ export class OrganizationSelect extends AdapterLitElement {
                     this.fireEvent();
                     break;
                 }
+                case "context": {
+                    delete this.cache[this.lang];
+                    this.updateSelect2();
+                    break;
+                }
                 case "entryPointUrl":
                     JSONLD.initialize(this.entryPointUrl, (jsonld) => {
                         this.jsonld = jsonld;
@@ -227,7 +234,7 @@ export class OrganizationSelect extends AdapterLitElement {
         let orgUrl = this.entryPointUrl + '/people/' +
             encodeURIComponent(this.auth.person.identifier) +
             '/organizations' +
-            '?context=' + encodeURIComponent('library-manager') +
+            '?context=' + encodeURIComponent(this.context) +
             '&lang=' + encodeURIComponent(this.lang);
 
         let response = await fetch(orgUrl, {
-- 
GitLab