Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor toast module #26677

Merged
merged 2 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions web_src/js/features/common-global.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,14 @@ async function fetchActionDoRequest(actionElem, url, opt) {
const data = await resp.json();
// the code was quite messy, sometimes the backend uses "err", sometimes it uses "error", and even "user_error"
// but at the moment, as a new approach, we only use "errorMessage" here, backend can use JSONError() to respond.
await showErrorToast(data.errorMessage || `server error: ${resp.status}`);
showErrorToast(data.errorMessage || `server error: ${resp.status}`);
} else {
await showErrorToast(`server error: ${resp.status}`);
showErrorToast(`server error: ${resp.status}`);
}
} catch (e) {
console.error('error when doRequest', e);
actionElem.classList.remove('is-loading', 'small-loading-icon');
await showErrorToast(i18n.network_error);
showErrorToast(i18n.network_error);
}
}

Expand Down
23 changes: 9 additions & 14 deletions web_src/js/modules/toast.js
Original file line number Diff line number Diff line change
@@ -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: {
Expand All @@ -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({
Expand All @@ -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);
}
6 changes: 3 additions & 3 deletions web_src/js/modules/toast.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ import {test, expect} from 'vitest';
import {showInfoToast, showErrorToast, showWarningToast} from './toast.js';

test('showInfoToast', async () => {
await showInfoToast('success 😀', {duration: -1});
showInfoToast('success 😀', {duration: -1});
expect(document.querySelector('.toastify')).toBeTruthy();
});

test('showWarningToast', async () => {
await showWarningToast('warning 😐', {duration: -1});
showWarningToast('warning 😐', {duration: -1});
expect(document.querySelector('.toastify')).toBeTruthy();
});

test('showErrorToast', async () => {
await showErrorToast('error 🙁', {duration: -1});
showErrorToast('error 🙁', {duration: -1});
expect(document.querySelector('.toastify')).toBeTruthy();
});