2021-01-14 21:02:21 +00:00
|
|
|
import { gettext } from "django";
|
|
|
|
import { css, CSSResult, customElement, html, LitElement, property, TemplateResult } from "lit-element";
|
2021-03-16 20:32:39 +00:00
|
|
|
import { EventsApi, Notification } from "authentik-api";
|
2021-02-09 16:04:55 +00:00
|
|
|
import { AKResponse } from "../../api/Client";
|
2021-03-08 10:14:00 +00:00
|
|
|
import { DEFAULT_CONFIG } from "../../api/Config";
|
2021-03-17 16:11:39 +00:00
|
|
|
import PFBase from "@patternfly/patternfly/patternfly-base.css";
|
|
|
|
import PFNotificationDrawer from "@patternfly/patternfly/components/NotificationDrawer/notification-drawer.css";
|
|
|
|
import PFDropdown from "@patternfly/patternfly/components/Dropdown/dropdown.css";
|
2021-03-17 18:49:08 +00:00
|
|
|
import AKGlobal from "../../authentik.css";
|
|
|
|
import PFContent from "@patternfly/patternfly/components/Content/content.css";
|
2021-01-14 21:02:21 +00:00
|
|
|
|
|
|
|
@customElement("ak-notification-drawer")
|
|
|
|
export class NotificationDrawer extends LitElement {
|
|
|
|
|
|
|
|
@property({attribute: false})
|
2021-02-09 16:04:55 +00:00
|
|
|
notifications?: AKResponse<Notification>;
|
2021-01-14 21:02:21 +00:00
|
|
|
|
|
|
|
@property({type: Number})
|
|
|
|
unread = 0;
|
|
|
|
|
|
|
|
static get styles(): CSSResult[] {
|
2021-03-17 18:49:08 +00:00
|
|
|
return [PFBase, PFNotificationDrawer, PFContent, PFDropdown, AKGlobal].concat(
|
2021-01-14 21:02:21 +00:00
|
|
|
css`
|
|
|
|
.pf-c-notification-drawer__header {
|
2021-01-15 11:16:35 +00:00
|
|
|
height: 114px;
|
2021-01-14 21:02:21 +00:00
|
|
|
padding: var(--pf-c-page__main-section--PaddingTop) var(--pf-c-page__main-section--PaddingRight) var(--pf-c-page__main-section--PaddingBottom) var(--pf-c-page__main-section--PaddingLeft);
|
2021-01-15 11:16:35 +00:00
|
|
|
display: flex;
|
|
|
|
flex-direction: column;
|
2021-01-14 21:02:21 +00:00
|
|
|
}
|
2021-01-16 16:02:48 +00:00
|
|
|
.pf-c-notification-drawer__list-item-description {
|
2021-01-16 17:22:25 +00:00
|
|
|
white-space: pre-wrap;
|
2021-01-16 16:02:48 +00:00
|
|
|
}
|
2021-01-14 21:02:21 +00:00
|
|
|
`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
firstUpdated(): void {
|
2021-03-08 10:14:00 +00:00
|
|
|
new EventsApi(DEFAULT_CONFIG).eventsNotificationsList({
|
|
|
|
seen: "false",
|
|
|
|
ordering: "-created",
|
2021-01-16 18:09:03 +00:00
|
|
|
}).then(r => {
|
2021-01-14 21:02:21 +00:00
|
|
|
this.notifications = r;
|
2021-01-16 18:09:03 +00:00
|
|
|
this.unread = r.results.length;
|
2021-01-14 21:02:21 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
renderItem(item: Notification): TemplateResult {
|
|
|
|
let level = "";
|
|
|
|
switch (item.severity) {
|
|
|
|
case "notice":
|
|
|
|
level = "pf-m-info";
|
|
|
|
break;
|
|
|
|
case "warning":
|
|
|
|
level = "pf-m-warning";
|
|
|
|
break;
|
|
|
|
case "alert":
|
|
|
|
level = "pf-m-danger";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2021-03-17 18:49:08 +00:00
|
|
|
return html`<li class="pf-c-notification-drawer__list-item pf-m-read">
|
2021-01-14 21:02:21 +00:00
|
|
|
<div class="pf-c-notification-drawer__list-item-header">
|
2021-03-17 18:49:08 +00:00
|
|
|
<span class="pf-c-notification-drawer__list-item-header-icon ${level}">
|
2021-01-14 21:02:21 +00:00
|
|
|
<i class="fas fa-info-circle" aria-hidden="true"></i>
|
|
|
|
</span>
|
|
|
|
<h2 class="pf-c-notification-drawer__list-item-header-title">
|
|
|
|
${item.event?.action}
|
|
|
|
</h2>
|
|
|
|
</div>
|
2021-01-16 18:09:03 +00:00
|
|
|
<div class="pf-c-notification-drawer__list-item-action">
|
2021-03-17 18:49:08 +00:00
|
|
|
${item.event && html`
|
|
|
|
<a class="pf-c-dropdown__toggle pf-m-plain" href="#/events/log/${item.event?.pk}">
|
2021-03-17 18:51:52 +00:00
|
|
|
<i class="fas fas fa-share-square"></i>
|
2021-03-17 18:49:08 +00:00
|
|
|
</a>
|
|
|
|
`}
|
2021-01-16 18:09:03 +00:00
|
|
|
<button class="pf-c-dropdown__toggle pf-m-plain" type="button" @click=${() => {
|
2021-03-08 10:14:00 +00:00
|
|
|
new EventsApi(DEFAULT_CONFIG).eventsNotificationsPartialUpdate({
|
|
|
|
uuid: item.pk || "",
|
|
|
|
data: {
|
|
|
|
seen: true,
|
|
|
|
}
|
2021-03-08 17:00:33 +00:00
|
|
|
}).then(() => {
|
|
|
|
this.firstUpdated();
|
2021-01-16 18:09:03 +00:00
|
|
|
});
|
|
|
|
}}>
|
|
|
|
<i class="fas fa-times"></i>
|
|
|
|
</button>
|
|
|
|
</div>
|
2021-01-16 16:02:48 +00:00
|
|
|
<p class="pf-c-notification-drawer__list-item-description">${item.body}</p>
|
2021-03-08 10:14:00 +00:00
|
|
|
<small class="pf-c-notification-drawer__list-item-timestamp">${item.created?.toLocaleString()}</small>
|
2021-01-14 21:02:21 +00:00
|
|
|
</li>`;
|
|
|
|
}
|
|
|
|
|
|
|
|
render(): TemplateResult {
|
|
|
|
if (!this.notifications) {
|
|
|
|
return html``;
|
|
|
|
}
|
|
|
|
return html`<div class="pf-c-drawer__body pf-m-no-padding">
|
|
|
|
<div class="pf-c-notification-drawer">
|
2021-01-15 11:16:35 +00:00
|
|
|
<div class="pf-c-notification-drawer__header pf-c-content">
|
|
|
|
<h1>
|
2021-01-14 21:02:21 +00:00
|
|
|
${gettext("Notifications")}
|
|
|
|
</h1>
|
2021-01-15 11:16:35 +00:00
|
|
|
<p>
|
2021-01-14 21:02:21 +00:00
|
|
|
${gettext(`${this.unread} unread`)}
|
2021-01-15 11:16:35 +00:00
|
|
|
</p>
|
2021-01-14 21:02:21 +00:00
|
|
|
</div>
|
|
|
|
<div class="pf-c-notification-drawer__body">
|
|
|
|
<ul class="pf-c-notification-drawer__list">
|
|
|
|
${this.notifications.results.map(n => this.renderItem(n))}
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>`;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|