-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Proof of concept Lit notification element
- Loading branch information
Showing
5 changed files
with
144 additions
and
61 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import * as popupcards from "./popupcards"; | ||
import * as header from "./header"; | ||
import * as filter from "./filter"; | ||
import * as notifications from "./notifications"; | ||
|
||
export { popupcards, header, filter }; | ||
export { popupcards, header, filter, notifications }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { LitElement, css, html, nothing } from "lit"; | ||
import {repeat} from 'lit/directives/repeat.js'; | ||
import {when} from 'lit/directives/when.js'; | ||
import {classMap} from 'lit/directives/class-map.js'; | ||
import {unsafeHTML} from 'lit/directives/unsafe-html.js'; | ||
|
||
export class NotificationList extends LitElement { | ||
static properties = { | ||
url: {type: String}, | ||
csrfToken: {type: String, attribute: "csrf-token"}, | ||
notifications: {state: true}, | ||
finished: {state: true, type: Boolean}, | ||
}; | ||
|
||
// Use light DOM with inherited styles instead of shadow DOM | ||
createRenderRoot() { | ||
return this; | ||
} | ||
|
||
fetchNotifications() { | ||
if (!this.url || this.finished) { | ||
return; | ||
} | ||
fetch(this.url).then((response) => { | ||
response.json().then((data) => { | ||
if (data.results) { | ||
this.notifications = data.results; | ||
} | ||
}); | ||
this.finished = true; | ||
}); | ||
} | ||
|
||
render() { | ||
// Trigger async notification fetch | ||
this.fetchNotifications(); | ||
|
||
if (this.notifications && this.notifications.length > 0) { | ||
return html` | ||
<div class="ui vertical relaxed list"> | ||
${repeat( | ||
this.notifications, | ||
(notification) => notification.id, | ||
(notification, index) => { | ||
return this.renderNotification(notification); | ||
} | ||
)} | ||
</div> | ||
`; | ||
} else { | ||
return nothing; | ||
} | ||
} | ||
|
||
renderNotification(notification) { | ||
return html` | ||
<div class="ui ${notification.message.type} message"> | ||
${when(notification.dismissable, () => html` | ||
<i class="fas fa-xmark close inline icon" @click=${{handleEvent: () => this.dismissNotification(notification), once: true}}></i> | ||
`)} | ||
<div class="header"> | ||
<i class="fad ${notification.message.icon_classes} icon"></i> | ||
${unsafeHTML(notification.message.header)} | ||
</div> | ||
${unsafeHTML(notification.message.body)} | ||
</div> | ||
` | ||
} | ||
|
||
dismissNotification(notification) { | ||
console.debug("Dismissing notification:", notification.id); | ||
const options = { | ||
method: "PATCH", | ||
headers: { | ||
"Content-Type": "application/json", | ||
"X-CSRFToken": this.csrfToken, | ||
}, | ||
body: { | ||
state: "dismissed", | ||
}, | ||
}; | ||
fetch(notification._links._self, options).then((response) => { | ||
console.log(response); | ||
// TODO error catch response | ||
response.json().then((data) => { | ||
console.log(data); | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
customElements.define("rtd-notification-list", NotificationList); |