Skip to content

Commit

Permalink
add warning toast
Browse files Browse the repository at this point in the history
  • Loading branch information
silverwind committed Jun 22, 2023
1 parent f153397 commit 1b8e9d8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
8 changes: 6 additions & 2 deletions templates/devtest/gitea-ui.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -232,14 +232,18 @@
<h1>Toast</h1>
<div>
<button class="ui button" id="info-toast">Show Info Toast</button>
<button class="ui button" id="warning-toast">Show Warning Toast</button>
<button class="ui button" id="error-toast">Show Error Toast</button>
</div>
<script>
document.getElementById('info-toast').addEventListener('click', () => {
window.giteaShowInfo('success :)');
window.giteaShowInfo('success 😀');
});
document.getElementById('warning-toast').addEventListener('click', () => {
window.giteaShowWarning('warning 😐');
});
document.getElementById('error-toast').addEventListener('click', () => {
window.giteaShowError('error :(');
window.giteaShowError('error 🙁');
});
</script>
</div>
Expand Down
10 changes: 10 additions & 0 deletions web_src/js/modules/toast.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ const levels = {
background: 'var(--color-green)',
duration: 2000,
},
warning: {
icon: 'gitea-exclamation',
background: 'var(--color-orange)',
duration: -1, // needs to be clicked away
},
error: {
icon: 'gitea-exclamation',
background: 'var(--color-red)',
Expand Down Expand Up @@ -47,12 +52,17 @@ export async function showInfo(message, opts) {
return await showToast(message, 'info', opts);
}

export async function showWarning(message, opts) {
return await showToast(message, 'warning', opts);
}

export async function showError(message, opts) {
return await showToast(message, 'error', opts);
}

// export for devtest page in development
if (process.env.NODE_ENV === 'development') {
window.giteaShowInfo = showInfo;
window.giteaShowWarning = showWarning;
window.giteaShowError = showError;
}
11 changes: 8 additions & 3 deletions web_src/js/modules/toast.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import {test, expect} from 'vitest';
import {showInfo, showError} from './toast.js';
import {showInfo, showError, showWarning} from './toast.js';

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

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

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

0 comments on commit 1b8e9d8

Please sign in to comment.