aboutsummaryrefslogtreecommitdiffstats
path: root/web_src/js/modules/toast.js
diff options
context:
space:
mode:
authorwxiaoguang <wxiaoguang@gmail.com>2023-08-23 15:25:13 +0800
committerGitHub <noreply@github.com>2023-08-23 07:25:13 +0000
commita428591f6b86d4ece21292712a9a5491266303eb (patch)
tree1e54cd5c04c0aa1929baad915becf308917d070c /web_src/js/modules/toast.js
parente4b2bdfbc0a28dcb4272de923b547a19131abd22 (diff)
downloadgitea-a428591f6b86d4ece21292712a9a5491266303eb.tar.gz
gitea-a428591f6b86d4ece21292712a9a5491266303eb.zip
Refactor toast module (#26677)
1. Do not use "async" 2. Call `hideToast` instead of `removeElement` for manual closing
Diffstat (limited to 'web_src/js/modules/toast.js')
-rw-r--r--web_src/js/modules/toast.js23
1 files changed, 9 insertions, 14 deletions
diff --git a/web_src/js/modules/toast.js b/web_src/js/modules/toast.js
index b5899052d4..fa075aed48 100644
--- a/web_src/js/modules/toast.js
+++ b/web_src/js/modules/toast.js
@@ -1,6 +1,6 @@
import {htmlEscape} from 'escape-goat';
import {svg} from '../svg.js';
-import Toastify from 'toastify-js';
+import Toastify from 'toastify-js'; // don't use "async import", because when network error occurs, the "async import" also fails and nothing is shown
const levels = {
info: {
@@ -21,9 +21,7 @@ const levels = {
};
// See https://github.com/apvarun/toastify-js#api for options
-async function showToast(message, level, {gravity, position, duration, ...other} = {}) {
- if (!message) return;
-
+function showToast(message, level, {gravity, position, duration, ...other} = {}) {
const {icon, background, duration: levelDuration} = levels[level ?? 'info'];
const toast = Toastify({
@@ -41,20 +39,17 @@ async function showToast(message, level, {gravity, position, duration, ...other}
});
toast.showToast();
-
- toast.toastElement.querySelector('.toast-close').addEventListener('click', () => {
- toast.removeElement(toast.toastElement);
- });
+ toast.toastElement.querySelector('.toast-close').addEventListener('click', () => toast.hideToast());
}
-export async function showInfoToast(message, opts) {
- return await showToast(message, 'info', opts);
+export function showInfoToast(message, opts) {
+ return showToast(message, 'info', opts);
}
-export async function showWarningToast(message, opts) {
- return await showToast(message, 'warning', opts);
+export function showWarningToast(message, opts) {
+ return showToast(message, 'warning', opts);
}
-export async function showErrorToast(message, opts) {
- return await showToast(message, 'error', opts);
+export function showErrorToast(message, opts) {
+ return showToast(message, 'error', opts);
}