diff --git a/container/src/LuigiContainer.svelte b/container/src/LuigiContainer.svelte index daaa608b9f..23db2af73c 100644 --- a/container/src/LuigiContainer.svelte +++ b/container/src/LuigiContainer.svelte @@ -145,7 +145,7 @@ } }; - thisComponent.closeAlert = (id: string, dismissKey: string) => { + thisComponent.closeAlert = (id: string, dismissKey?: string) => { ContainerAPI.closeAlert(id, dismissKey, iframeHandle); }; diff --git a/container/src/api/container-api.ts b/container/src/api/container-api.ts index 8e38801cb6..5e48a5cc5a 100644 --- a/container/src/api/container-api.ts +++ b/container/src/api/container-api.ts @@ -95,11 +95,12 @@ export class ContainerAPIFunctions { /** * Send a message to the microfrontend notifying the alert has been closed * @param id the id of the alert being closed - * @param dismissKey the dismiss key being sent if any - * @param iframeHandle the handle of the iframe to send the message to + * @param dismissKey the dismiss key being sent if any (optional) + * @param iframeHandle the handle of the iframe to send the message to (optional) */ - closeAlert(id: string, dismissKey: string, iframeHandle: IframeHandle) { - containerService.sendCustomMessageToIframe(iframeHandle, { id, dismissKey }, LuigiInternalMessageID.ALERT_CLOSED); + closeAlert(id: string, dismissKey?: string, iframeHandle?: IframeHandle) { + const message = dismissKey ? { id, dismissKey } : { id }; + containerService.sendCustomMessageToIframe(iframeHandle, message, LuigiInternalMessageID.ALERT_CLOSED); } } diff --git a/container/src/services/container.service.ts b/container/src/services/container.service.ts index 17d90b1869..84852d1ed4 100644 --- a/container/src/services/container.service.ts +++ b/container/src/services/container.service.ts @@ -24,7 +24,7 @@ export class ContainerService { sendCustomMessageToIframe(iframeHandle: IframeHandle, msg: object, msgName?: string) { const messageName = msgName || 'custom'; - if (iframeHandle.iframe.contentWindow) { + if (iframeHandle?.iframe?.contentWindow) { const iframeUrl = new URL(iframeHandle.iframe.src); if (messageName === 'custom') { iframeHandle.iframe.contentWindow.postMessage({ msg: messageName, data: msg }, iframeUrl.origin); diff --git a/container/test/api/container-api.spec.ts b/container/test/api/container-api.spec.ts index e317f9582d..681c14b474 100644 --- a/container/test/api/container-api.spec.ts +++ b/container/test/api/container-api.spec.ts @@ -218,6 +218,20 @@ describe('Container Service', () => { expect(spy).toHaveBeenCalledWith(iframeHandle, { id, dismissKey }, LuigiInternalMessageID.ALERT_CLOSED); }); + + it('internal method properly called if no dismisskey', () => { + // mock and spy + const id = 'some-id'; + const iframeHandle = { + data: 'test' + } as unknown as IframeHandle; + containerService.sendCustomMessageToIframe = jest.fn(); + const spy = jest.spyOn(containerService, 'sendCustomMessageToIframe'); + + containerAPI.closeAlert(id, undefined, iframeHandle); + + expect(spy).toHaveBeenCalledWith(iframeHandle, { id }, LuigiInternalMessageID.ALERT_CLOSED); + }); }); describe('sendCustomMessage', () => { diff --git a/container/typings/LuigiContainer.svelte.d.ts b/container/typings/LuigiContainer.svelte.d.ts index 34fa36e488..16073f7019 100644 --- a/container/typings/LuigiContainer.svelte.d.ts +++ b/container/typings/LuigiContainer.svelte.d.ts @@ -246,12 +246,12 @@ export default class LuigiContainer extends HTMLElement { /** * A function that notifies the microfrontend that the opened alert has been closed. * @param id the id of the opened alert - * @param dismissKey the key specifying which dismiss link was clicked on the alert message + * @param dismissKey the key specifying which dismiss link was clicked on the alert message (optional) * @example * containerElement.closeAlert('my-alert-id', 'my-dismiss-key') * @since 1.0.0 */ - closeAlert(id: string, dismissKey: string): void; + closeAlert(id: string, dismissKey?: string): void; /** * Manually triggers the micro frontend rendering process when using defer-init attribute. diff --git a/docs/luigi-container-api.md b/docs/luigi-container-api.md index 71a2dda932..d83c3b4b71 100644 --- a/docs/luigi-container-api.md +++ b/docs/luigi-container-api.md @@ -536,7 +536,7 @@ A function that notifies the microfrontend that the opened alert has been closed #### Parameters * `id` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** the id of the opened alert -* `dismissKey` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** the key specifying which dismiss link was clicked on the alert message +* `dismissKey` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)?** the key specifying which dismiss link was clicked on the alert message (optional) #### Examples diff --git a/docs/luigi-element-api.md b/docs/luigi-element-api.md index 229d5734a7..57db546396 100644 --- a/docs/luigi-element-api.md +++ b/docs/luigi-element-api.md @@ -62,7 +62,7 @@ Base class for Luigi web component micro frontends. #### afterInit -Override to execute logic after initialization of the web component, i.e. +Override to execute logic after initialization of the web component, i.e. after internal rendering and all context data set. ##### Parameters @@ -101,7 +101,7 @@ Query selector operating on shadow root. ### html -Html string processing according to luigi functionality. +Html string processing according to luigi functionality. Also useful in combination with LitElement VS Code plugins. #### Parameters