Simplify toast code

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2024-06-14 11:43:51 +02:00
parent 75547291fe
commit 126f815a44
Signed by: luzifer
SSH key fingerprint: SHA256:/xtE5lCgiRDQr8SLxHMS92ZBlACmATUmF1crK16Ks4E
4 changed files with 46 additions and 10 deletions

View file

@ -27,9 +27,9 @@ import { Toast } from 'bootstrap'
export type ToastContent = { export type ToastContent = {
id: string id: string
autoHide: boolean | undefined autoHide?: boolean
color: string | undefined color?: string
delay: number | undefined delay?: number
text: string text: string
} }

View file

@ -32,7 +32,7 @@ export default defineComponent({
mounted() { mounted() {
this.bus.on(BusEventTypes.Toast, (toast: ToastContent) => this.toasts.push({ this.bus.on(BusEventTypes.Toast, (toast: ToastContent) => this.toasts.push({
...toast, ...toast,
id: crypto.randomUUID(), id: toast.id || crypto.randomUUID(),
})) }))
}, },

40
src/helpers/toasts.ts Normal file
View file

@ -0,0 +1,40 @@
import { ToastContent } from '../components/_toast.vue'
/**
* Create the content of an error-toast
*
* @param text The message to display to the user
* @returns The {ToastContent} for usage in `this.bus.emit(BusEventTypes.Toast, errorToast(...))`
*/
const errorToast = (text: string): ToastContent => ({
autoHide: false,
color: 'danger',
id: crypto.randomUUID(),
text,
})
/**
* Create the content of an info-toast
*
* @param text The message to display to the user
* @returns The {ToastContent} for usage in `this.bus.emit(BusEventTypes.Toast, infoToast(...))`
*/
const infoToast = (text: string): ToastContent => ({
color: 'info',
id: crypto.randomUUID(),
text,
})
/**
* Create the content of an success-toast
*
* @param text The message to display to the user
* @returns The {ToastContent} for usage in `this.bus.emit(BusEventTypes.Toast, successToast(...))`
*/
const successToast = (text: string): ToastContent => ({
color: 'success',
id: crypto.randomUUID(),
text,
})
export { errorToast, infoToast, successToast }

View file

@ -10,7 +10,7 @@ import mitt from 'mitt'
import BusEventTypes from './helpers/busevents' import BusEventTypes from './helpers/busevents'
import ConfigNotifyListener from './helpers/configNotify' import ConfigNotifyListener from './helpers/configNotify'
import { ToastContent } from './components/_toast.vue' import { errorToast } from './helpers/toasts'
import router from './router' import router from './router'
import App from './components/app.vue' import App from './components/app.vue'
@ -159,11 +159,7 @@ const app = createApp({
} }
this.bus.emit(BusEventTypes.LoginProcessing, false) this.bus.emit(BusEventTypes.LoginProcessing, false)
this.bus.emit(BusEventTypes.Toast, { this.bus.emit(BusEventTypes.Toast, errorToast(errorText))
autoHide: false,
color: 'danger',
text: errorText,
} as ToastContent)
throw new Error(`login failed, status=${resp.status}`) throw new Error(`login failed, status=${resp.status}`)
} }