-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Type- and javascript for notifications
- Loading branch information
Showing
11 changed files
with
82 additions
and
34 deletions.
There are no files selected for viewing
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,56 @@ | ||
/** | ||
* Model for a notification in the navbar. | ||
*/ | ||
export class Notification { | ||
readonly element: Element; | ||
read: boolean; | ||
|
||
constructor(readonly id: number, readonly url: string, readonly _read: boolean) { | ||
this.element = document.querySelector(`.notification[data-id="${id}"]`); | ||
this.read = _read; | ||
|
||
this.element.querySelector(".notification-link").addEventListener("click", event => { | ||
const goto = (event.target as HTMLLinkElement).href; | ||
fetch(url, { | ||
method: "PATCH", | ||
headers: { | ||
"x-csrf-token": (document.querySelector("meta[name=\"csrf-token\"]") as HTMLMetaElement).content, | ||
"x-requested-with": "XMLHttpRequest", | ||
"content-type": "application/json" | ||
}, | ||
body: "{ \"notification\": { \"read\": true } }" | ||
}).then(() => { | ||
window.location.href = goto; | ||
}); | ||
return false; | ||
}); | ||
|
||
this.element.querySelector(".read-indicator").addEventListener("click", event => { | ||
const indicator = event.target as Element; | ||
fetch(url, { | ||
method: "PATCH", | ||
headers: { | ||
"x-csrf-token": (document.querySelector("meta[name=\"csrf-token\"]") as HTMLMetaElement).content, | ||
"x-requested-with": "XMLHttpRequest", | ||
"content-type": "application/json" | ||
}, | ||
body: `{ "notification": { "read": ${!this.read} } }` | ||
}).then(resp => { | ||
return Promise.all([resp.ok, resp.json()]); | ||
}).then(([ok, body]) => { | ||
if (!ok) { | ||
return Promise.reject(body); | ||
} | ||
this.read = body.read; | ||
if (!this.read) { | ||
indicator.classList.remove("mdi-circle-medium"); | ||
indicator.classList.add("mdi-check"); | ||
} else { | ||
indicator.classList.remove("mdi-check"); | ||
indicator.classList.add("mdi-circle-medium"); | ||
} | ||
}); | ||
event.stopPropagation(); | ||
}); | ||
} | ||
} |
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
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 |
---|---|---|
|
@@ -16,4 +16,8 @@ def update? | |
def destroy? | ||
record.user == user | ||
end | ||
|
||
def permitted_attributes | ||
%i[read] | ||
end | ||
end |
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,2 +1,3 @@ | ||
json.extract! notification, :id, :created_at, :updated_at | ||
json.extract! notification, :id, :message, :read, :created_at, :updated_at | ||
json.url notification_url(notification, format: :json) | ||
json.notifiable_url notifiable_url(notification) |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
$(".notification[data-id=\"<%= @notification.id %>\"]").remove(); |
This file was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
json.partial! 'notifications/notification', notification: @notification |
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