From: Adam Dullage Date: Mon, 5 Feb 2024 20:50:05 +0000 (+0000) Subject: Centralised toast messaging. X-Git-Url: http://git.99rst.org/?a=commitdiff_plain;h=fb5beb822d3460c3448d695d647779509cbbaeee;p=flatnotes.git Centralised toast messaging. --- diff --git a/client/components/App.js b/client/components/App.js index 8ea084d..01a8889 100644 --- a/client/components/App.js +++ b/client/components/App.js @@ -134,12 +134,16 @@ export default { this.navigate(constants.basePaths.login); }, - noteDeletedToast: function () { - this.$bvToast.toast("Note deleted ✓", { - variant: "success", + showToast: function (variant, message, title = null) { + const options = { + variant: variant, noCloseButton: true, toaster: "b-toaster-bottom-right", - }); + }; + if (title != null) { + options.title = title; + } + this.$bvToast.toast(message, options); }, focusSearchInput: function () { @@ -158,14 +162,10 @@ export default { }, unhandledServerErrorToast: function () { - this.$bvToast.toast( + this.showToast( + "danger", "Unknown error communicating with the server. Please try again.", - { - title: "Unknown Error", - variant: "danger", - noCloseButton: true, - toaster: "b-toaster-bottom-right", - } + "Unknown Error" ); }, @@ -186,7 +186,8 @@ export default { this.constants = constants; EventBus.$on("navigate", this.navigate); - EventBus.$on("unhandledServerError", this.unhandledServerErrorToast); + EventBus.$on("showToast", this.showToast); + EventBus.$on("unhandledServerErrorToast", this.unhandledServerErrorToast); EventBus.$on("updateNoteTitle", this.updateNoteTitle); Mousetrap.bind("/", function () { diff --git a/client/components/App.vue b/client/components/App.vue index 78f0b71..3e46c52 100644 --- a/client/components/App.vue +++ b/client/components/App.vue @@ -61,7 +61,7 @@ class="flex-grow-1" :titleToLoad="noteTitle" :auth-type="authType" - @note-deleted="noteDeletedToast" + @note-deleted="showToast('success', 'Note deleted ✓')" > diff --git a/client/components/Login.vue b/client/components/Login.vue index 4a42c03..5080b5e 100644 --- a/client/components/Login.vue +++ b/client/components/Login.vue @@ -148,13 +148,9 @@ export default { typeof error.response !== "undefined" && error.response.status == 401 ) { - parent.$bvToast.toast("Incorrect login credentials ✘", { - variant: "danger", - noCloseButton: true, - toaster: "b-toaster-bottom-right", - }); + EventBus.$emit("showToast", "danger", "Incorrect login credentials ✘") } else { - EventBus.$emit("unhandledServerError"); + EventBus.$emit("unhandledServerErrorToast"); } }) .finally(function () { diff --git a/client/components/NoteViewerEditor.vue b/client/components/NoteViewerEditor.vue index e62467b..d1a8550 100644 --- a/client/components/NoteViewerEditor.vue +++ b/client/components/NoteViewerEditor.vue @@ -243,13 +243,10 @@ export default { methods: { badFilenameToast: function (invalidItem) { - this.$bvToast.toast( - `Invalid ${invalidItem}. Due to filename restrictions, the following characters are not allowed: <>:"/\\|?*`, - { - variant: "danger", - noCloseButton: true, - toaster: "b-toaster-bottom-right", - } + EventBus.$emit( + "showToast", + "danger", + `Invalid ${invalidItem}. Due to filename restrictions, the following characters are not allowed: <>:"/\\|?*` ); }, @@ -276,7 +273,7 @@ export default { parent.noteLoadFailedMessage = "Note not found"; parent.noteLoadFailed = true; } else { - EventBus.$emit("unhandledServerError"); + EventBus.$emit("unhandledServerErrorToast"); parent.noteLoadFailed = true; } }); @@ -392,26 +389,20 @@ export default { }, existingTitleToast: function () { - this.$bvToast.toast( + EventBus.$emit( + "showToast", + "danger", "A note with this title already exists. Please try again with a new title.", - { - title: "Duplicate ✘", - variant: "danger", - noCloseButton: true, - toaster: "b-toaster-bottom-right", - } + "Duplicate ✘" ); }, entityTooLargeToast: function (entityName) { - this.$bvToast.toast( + EventBus.$emit( + "showToast", + "danger", `This ${entityName.toLowerCase()} is too large. Please try again with a smaller ${entityName.toLowerCase()} or adjust your server configuration.`, - { - title: `${entityName} Too Large ✘`, - variant: "danger", - noCloseButton: true, - toaster: "b-toaster-bottom-right", - } + `${entityName} Too Large ✘` ); }, @@ -426,11 +417,11 @@ export default { this.titleInput = this.titleInput.trim(); } if (!this.titleInput) { - this.$bvToast.toast("Cannot save note without a title ✘", { - variant: "danger", - noCloseButton: true, - toaster: "b-toaster-bottom-right", - }); + EventBus.$emit( + "showToast", + "danger", + "Cannot save note without a title ✘" + ); return; } @@ -455,7 +446,7 @@ export default { } else if (error.response?.status == 413) { this.entityTooLargeToast("Note"); } else { - EventBus.$emit("unhandledServerError"); + EventBus.$emit("unhandledServerErrorToast"); } }); } @@ -480,7 +471,7 @@ export default { ) { parent.existingTitleToast(); } else { - EventBus.$emit("unhandledServerError"); + EventBus.$emit("unhandledServerErrorToast"); } }); } @@ -507,11 +498,7 @@ export default { }, noteSavedToast: function () { - this.$bvToast.toast("Note saved ✓", { - variant: "success", - noCloseButton: true, - toaster: "b-toaster-bottom-right", - }); + EventBus.$emit("showToast", "success", "Note saved ✓"); }, cancelNote: function () { @@ -575,7 +562,7 @@ export default { }) .catch(function (error) { if (!error.handled) { - EventBus.$emit("unhandledServerError"); + EventBus.$emit("unhandledServerErrorToast"); } }); } @@ -613,11 +600,7 @@ export default { return false; } - this.$bvToast.toast("Uploading attachment...", { - variant: "success", - noCloseButton: true, - toaster: "b-toaster-bottom-right", - }); + EventBus.$emit("showToast", "success", "Uploading attachment..."); const formData = new FormData(); formData.append("file", file); @@ -628,31 +611,24 @@ export default { }, }) .then(function () { - parent.$bvToast.toast("Attachment uploaded ✓", { - variant: "success", - noCloseButton: true, - toaster: "b-toaster-bottom-right", - }); + EventBus.$emit("showToast", "success", "Attachment uploaded ✓"); return true; }) .catch(function (error) { if (error.response?.status == 409) { - parent.$bvToast.toast( - "An attachment with this filename already exists ✘", - { - variant: "danger", - noCloseButton: true, - toaster: "b-toaster-bottom-right", - } + EventBus.$emit( + "showToast", + "danger", + "An attachment with this filename already exists ✘" ); } else if (error.response?.status == 413) { this.entityTooLargeToast("Attachment"); } else { - parent.$bvToast.toast("Failed to upload attachment ✘", { - variant: "danger", - noCloseButton: true, - toaster: "b-toaster-bottom-right", - }); + EventBus.$emit( + "showToast", + "danger", + "Failed to upload attachment ✘" + ); } return false; }); diff --git a/client/components/RecentlyModified.vue b/client/components/RecentlyModified.vue index 789d298..af872b0 100644 --- a/client/components/RecentlyModified.vue +++ b/client/components/RecentlyModified.vue @@ -92,7 +92,7 @@ export default { .catch(function (error) { parent.loadingFailed = true; if (!error.handled) { - EventBus.$emit("unhandledServerError"); + EventBus.$emit("unhandledServerErrorToast"); } }); }, diff --git a/client/components/SearchInput.vue b/client/components/SearchInput.vue index fb54f91..dea5212 100644 --- a/client/components/SearchInput.vue +++ b/client/components/SearchInput.vue @@ -93,11 +93,7 @@ export default { }=${encodeURIComponent(this.searchTermInput)}` ); } else { - this.$bvToast.toast("Please enter a search term ✘", { - variant: "danger", - noCloseButton: true, - toaster: "b-toaster-bottom-right", - }); + EventBus.$emit("showToast", "danger", "Please enter a search term ✘"); } }, diff --git a/client/components/SearchResults.vue b/client/components/SearchResults.vue index e39083b..f8792cf 100644 --- a/client/components/SearchResults.vue +++ b/client/components/SearchResults.vue @@ -232,7 +232,7 @@ export default { .catch(function (error) { if (!error.handled) { parent.searchFailed = true; - EventBus.$emit("unhandledServerError"); + EventBus.$emit("unhandledServerErrorToast"); } }); },