Skip to content

Commit 21ad96d

Browse files
author
Dennis Labordus
authored
Merge pull request #63 from com-pas/userinfo
Added User Info functionality
2 parents 27d5bee + 12e2219 commit 21ad96d

File tree

7 files changed

+79
-5
lines changed

7 files changed

+79
-5
lines changed

src/Hosting.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ import { LoggingElement } from './Logging.js';
1515
import { Plugin, PluggingElement, pluginIcons } from './Plugging.js';
1616
import { SettingElement } from './Setting.js';
1717

18+
import { UserInfoEvent } from './foundation.js';
19+
import { string } from 'fast-check';
20+
1821
interface MenuItem {
1922
icon: string;
2023
name: string;
@@ -49,6 +52,9 @@ export function Hosting<
4952
@property({ attribute: false })
5053
validated: Promise<unknown> = Promise.resolve();
5154

55+
@property({ type: string})
56+
username: string | undefined;
57+
5258
@internalProperty()
5359
statusNumber = 0;
5460

@@ -200,6 +206,10 @@ export function Hosting<
200206
];
201207
}
202208

209+
private onUserInfo(event: UserInfoEvent) {
210+
this.username = event.detail.name;
211+
}
212+
203213
constructor(...args: any[]) {
204214
super(...args);
205215

@@ -216,6 +226,8 @@ export function Hosting<
216226
})
217227
);
218228
});
229+
230+
this.addEventListener('userinfo', this.onUserInfo);
219231
}
220232

221233
renderMenuItem(me: MenuItem | 'divider'): TemplateResult {
@@ -288,6 +300,9 @@ export function Hosting<
288300
@click=${() => (this.menuUI.open = true)}
289301
></mwc-icon-button>
290302
<div slot="title" id="title">${this.docName}</div>
303+
${this.username != undefined
304+
? html`<span id="userField" slot="actionItems">${translate('userinfo.loggedInAs', {name: this.username})}</span>`
305+
: ``}
291306
${this.menu.map(this.renderActionItem)}
292307
${this.doc
293308
? html`<mwc-tab-bar
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import {CompasSettings} from "../compas/CompasSettingsElement.js";
2+
3+
export function CompasUserInfoService() {
4+
5+
function getCompasSettings() {
6+
return CompasSettings().compasSettings;
7+
}
8+
9+
return {
10+
getCompasUserInfo(): Promise<Document> {
11+
const userInfoUrl = getCompasSettings().sclDataServiceUrl + '/common/v1/userinfo';
12+
return fetch(userInfoUrl)
13+
.then(response => response.text())
14+
.then(str => new DOMParser().parseFromString(str, 'application/xml'));
15+
}
16+
}
17+
}

src/compas/foundation.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {get} from "lit-translate";
2-
import {newOpenDocEvent} from "../foundation.js";
2+
import { CompasUserInfoService } from "../compas-services/CompasUserInfoService.js";
3+
import {newOpenDocEvent, newUserInfoEvent} from "../foundation.js";
34
import {OpenSCD} from "../open-scd.js";
45

56
const FILE_EXTENSION_LENGTH = 3;
@@ -38,3 +39,13 @@ export function updateDocumentInOpenSCD(doc: Document): void {
3839

3940
getOpenScdElement().dispatchEvent(newOpenDocEvent(doc, docName, {detail: {docId: id}}));
4041
}
42+
43+
export async function showOptionalUserInfo(): Promise<void> {
44+
await CompasUserInfoService().getCompasUserInfo()
45+
.then(response => {
46+
const name = response.querySelectorAll("Name").item(0)?.textContent;
47+
if (name != null)
48+
getOpenScdElement().dispatchEvent(newUserInfoEvent(name));
49+
});
50+
}
51+
showOptionalUserInfo();

src/foundation.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,23 @@ export function newOpenDocEvent(
345345
});
346346
}
347347

348+
/** Represents user information from a backend. */
349+
export interface UserInfoDetail {
350+
name: string;
351+
}
352+
export type UserInfoEvent = CustomEvent<UserInfoDetail>;
353+
export function newUserInfoEvent(
354+
name: string,
355+
eventInitDict?: CustomEventInit<Partial<ValidateDetail>>
356+
): UserInfoEvent {
357+
return new CustomEvent<UserInfoDetail>('userinfo', {
358+
bubbles: true,
359+
composed: true,
360+
...eventInitDict,
361+
detail: { name, ...eventInitDict?.detail },
362+
});
363+
}
364+
348365
/** @returns a reference to `element` with segments delimited by '/'. */
349366
// TODO(c-dinkel): replace with identity (FIXME)
350367
export function referencePath(element: Element): string {
@@ -2547,6 +2564,7 @@ declare global {
25472564
['pending-state']: PendingStateEvent;
25482565
['editor-action']: EditorActionEvent<EditorAction>;
25492566
['open-doc']: OpenDocEvent;
2567+
['userinfo']: UserInfoEvent;
25502568
['wizard']: WizardEvent;
25512569
['validate']: ValidateEvent;
25522570
['log']: LogEvent;

src/translations/de.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,9 @@ export const de: Translations = {
360360
code: {
361361
log: 'Element im XML Editor angepasst: {{id}}',
362362
},
363+
userinfo: {
364+
loggedInAs: '???'
365+
},
363366
add: 'Hinzufügen',
364367
new: 'Neu',
365368
remove: 'Entfernen',

src/translations/en.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,9 @@ export const en = {
357357
code: {
358358
log: 'Changed element in XML editor: {{id}}',
359359
},
360+
userinfo: {
361+
loggedInAs: 'Logged in as {{name}}'
362+
},
360363
add: 'Add',
361364
new: 'New',
362365
remove: 'Remove',

test/integration/compas/foundation.test.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,34 @@ import {updateDocumentInOpenSCD} from "../../../src/compas/foundation.js";
44

55
import {OpenSCD} from "../../../src/open-scd.js";
66
import '../../../src/open-scd.js';
7+
import { newUserInfoEvent } from '../../../src/foundation.js';
78

89
describe('compas-foundation', () => {
9-
let doc: XMLDocument;
1010
let element: OpenSCD;
1111

1212
beforeEach(async () => {
1313
element = await fixture(
1414
html`<open-scd></open-scd>`
1515
);
16+
});
1617

17-
doc = await fetch('/base/test/testfiles/compas/test-scd.cid')
18+
it('when loaded the document should be on open-scd component', async () => {
19+
let doc = await fetch('/base/test/testfiles/compas/test-scd.cid')
1820
.then(response => response.text())
1921
.then(str => new DOMParser().parseFromString(str, 'application/xml'));
20-
});
2122

22-
it('when loaded the document should be on open-scd component', async () => {
2323
updateDocumentInOpenSCD(doc);
2424
await element.updateComplete;
2525

2626
expect(element.doc).to.be.not.undefined;
2727
expect(element.docId).to.be.equal('380b5e70-4753-4b59-b5b4-d51ceb26a30c');
2828
expect(element.docName).to.be.equal('ied_utrecht_station235-3.0.0.cid');
2929
});
30+
31+
it('when UserInfoEvent event is dispatched, the username is shown in OpenSCD', async () => {
32+
element.dispatchEvent(newUserInfoEvent("Henk"));
33+
await element.updateComplete;
34+
35+
expect(element.shadowRoot!.querySelector('span[id="userField"]')!.textContent).to.be.equal('Logged in as Henk')
36+
});
3037
});

0 commit comments

Comments
 (0)