Skip to content

Commit

Permalink
feat(extended-uportal-header): get service name by fname
Browse files Browse the repository at this point in the history
  • Loading branch information
Quentin-Guillemin committed Oct 17, 2024
1 parent dee6ca3 commit e1a263f
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 2 deletions.
8 changes: 7 additions & 1 deletion @uportal/extended-uportal-header/samples/api/api.json
Original file line number Diff line number Diff line change
Expand Up @@ -18247,7 +18247,8 @@
"show-favorites-in-slider": true,
"return-home-title": "retour à l'accueil",
"return-home-target": "_self",
"icon-type": "nine-square"
"icon-type": "nine-square",
"portlet-info-api-url": "/portlet"
}
},
"session": {
Expand All @@ -18259,5 +18260,10 @@
"userName": "Fxxxxxx",
"version": "5.12.0"
}
},
"portlet": {
"portlet": {
"title": "Demo Prolongation ENT"
}
}
}
3 changes: 2 additions & 1 deletion @uportal/extended-uportal-header/samples/api/routes.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"/uPortal/api/v4-3/dlm/portletRegistry.json": "/portletRegistry",
"/uPortal/api/v5-1/userinfo": "/userinfo",
"/commun/portal_template_api.tpl.json": "/portal_template",
"/uPortal/api/session.json": "/session"
"/uPortal/api/session.json": "/session",
"/portlet/demo.json": "/portlet"
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
</head>
<body>
<extended-uportal-header
fname="demo"
debug
>
<div slot="not-loaded">NOT LOADED</div>
Expand Down
25 changes: 25 additions & 0 deletions @uportal/extended-uportal-header/src/extended-uportal-header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import defaultAvatar from '@images/default-avatar.svg';
/** Icons */
import { icon } from '@fortawesome/fontawesome-svg-core';
import { faRightToBracket } from '@fortawesome/free-solid-svg-icons/faRightToBracket';
import portletService from '@services/portletService';

interface properties {
messages: unknown;
Expand Down Expand Up @@ -90,6 +91,8 @@ interface properties {
returnHomeTitle: string | null;
height: string;
sessionRenewDisable: boolean;
portletInfoApiUrl: string;
fname: string;
debug: boolean;
}

Expand Down Expand Up @@ -231,6 +234,10 @@ export class ExtendedUportalHeader extends LitElement {
sessionRenewDisable = false;
@property({ type: Object, attribute: 'dont-override' })
dontOverride: Array<keyof overridableProperties> | null = null;
@property({ type: String, attribute: 'portlet-info-api-url' })
portletInfoApiUrl = '';
@property({ type: String })
fname = '';
@property({ type: Boolean })
debug = false;

Expand Down Expand Up @@ -315,6 +322,7 @@ export class ExtendedUportalHeader extends LitElement {

private async _firstLoad() {
await this._getTemplate();
this._loadPortletInformations();
this._debounceLoad();
}

Expand Down Expand Up @@ -431,6 +439,8 @@ export class ExtendedUportalHeader extends LitElement {
returnHomeTitle: null,
height: 'auto',
sessionRenewDisable: false,
portletInfoApiUrl: '',
fname: '',
debug: false,
};

Expand Down Expand Up @@ -490,6 +500,20 @@ export class ExtendedUportalHeader extends LitElement {
}
}

private async _loadPortletInformations() {
if (
this.portletInfoApiUrl === '' ||
this.fname === '' ||
this.serviceName !== ''
)
return;
const data = await portletService.get(
this._makeUrl(this.portletInfoApiUrl),
this.fname
);
if (data) this.serviceName = data.title;
}

private _handleUserAction() {
if (this._loaded) {
this._debounceRenewToken();
Expand All @@ -498,6 +522,7 @@ export class ExtendedUportalHeader extends LitElement {
}

private _makeUrl(path: string, domain = ''): string {
if (path.startsWith('http')) return path;
const protocol = this.debug ? 'http' : 'https';
return `${protocol}://${domain == '' ? this.domain : domain}${path}`;
}
Expand Down
36 changes: 36 additions & 0 deletions @uportal/extended-uportal-header/src/services/portletService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
export interface portlet {
title: string;
}
export default class portletService {
static async get(
portletApiUrl: string,
fname: string
): Promise<portlet | null> {
portletApiUrl = portletApiUrl.endsWith('/')
? portletApiUrl
: `${portletApiUrl}/`;

try {
const options = {
method: 'GET',
};

const response = await fetch(`${portletApiUrl}${fname}.json`, options);

if (!response.ok) {
throw new Error(response.statusText);
}

const portlet = await response.json();

return {
title: portlet?.portlet?.title ?? '',
};
} catch (err) {
// eslint-disable-next-line
console.error(err, portletApiUrl);
return null;
}
return null;
}
}

0 comments on commit e1a263f

Please sign in to comment.