This application is a JavaScript widget that needs to be built before embedding it into a third-party project.
To build the widget, run the following commands:
npm i
npm run buildThe built files will be stored in the dist folder.
To use this widget in your project, copy the JavaScript and CSS files from the dist folder into your application. Then, include them in your HTML file as follows:
<script src="./smart-app-launch.subscriptions.umd.js"></script>After adding the script to your page, the window.SmartAppLaunchSubscriptions object should be available. This allows you to initialize the widget using the following script:
<script>
document.addEventListener('DOMContentLoaded', () => {
if (window.SmartAppLaunchSubscriptions) {
window.SmartAppLaunchSubscriptions.init('notifications-container', {
apiKey: 'http://localhost:9000'
});
} else {
console.error('SmartAppLaunchSubscriptions is not defined');
}
});
</script>The init function is used to set up the widget. It requires two parameters:
-
containerId(string, required) – Theidof an HTML element where the widget will be rendered.
Example: If your HTML page contains the following element:<div id="notifications-container"></div>
Then you should pass
'notifications-container'as the first parameter when callinginit. -
options(object, required) – An object containing configuration options for the widget.
Available options:apiKey(string, required) – URL of theSmart App Subscriptionsbackend service.width(number, optional) – Width of the widget in pixels.height(number, optional) – Height of the widget in pixels.
The setUser function is used to set the logged-in practitioner for the widget. This ensures that the widget subscribes to events related to the specified practitioner.
practitionerId(string ornull, required) – Theidof the practitioner whose events should be tracked.
Ifnullis passed, the widget will unsubscribe from all events.
const response = await fetch(`${AIDBOX_URL}/auth/token`, {
// authentication request
});
const loginData = await response.json();
const practitionerId = loginData.userinfo.data?.practitioner?.id;
if (window.SmartAppLaunchSubscriptions) {
window.SmartAppLaunchSubscriptions.setUser(practitionerId);
}To unsubscribe from all events, pass null:
window.SmartAppLaunchSubscriptions.setUser(null);