mirror of
https://github.com/Luzifer/twitch-bot.git
synced 2024-12-20 11:51:17 +00:00
Simplify toast code
Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
338284b89b
commit
8ca3642919
4 changed files with 46 additions and 10 deletions
|
@ -27,9 +27,9 @@ import { Toast } from 'bootstrap'
|
|||
|
||||
export type ToastContent = {
|
||||
id: string
|
||||
autoHide: boolean | undefined
|
||||
color: string | undefined
|
||||
delay: number | undefined
|
||||
autoHide?: boolean
|
||||
color?: string
|
||||
delay?: number
|
||||
text: string
|
||||
}
|
||||
|
||||
|
|
|
@ -32,7 +32,7 @@ export default defineComponent({
|
|||
mounted() {
|
||||
this.bus.on(BusEventTypes.Toast, (toast: ToastContent) => this.toasts.push({
|
||||
...toast,
|
||||
id: crypto.randomUUID(),
|
||||
id: toast.id || crypto.randomUUID(),
|
||||
}))
|
||||
},
|
||||
|
||||
|
|
40
src/helpers/toasts.ts
Normal file
40
src/helpers/toasts.ts
Normal 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 }
|
|
@ -10,7 +10,7 @@ import mitt from 'mitt'
|
|||
|
||||
import BusEventTypes from './helpers/busevents'
|
||||
import ConfigNotifyListener from './helpers/configNotify'
|
||||
import { ToastContent } from './components/_toast.vue'
|
||||
import { errorToast } from './helpers/toasts'
|
||||
|
||||
import router from './router'
|
||||
import App from './components/app.vue'
|
||||
|
@ -159,11 +159,7 @@ const app = createApp({
|
|||
}
|
||||
|
||||
this.bus.emit(BusEventTypes.LoginProcessing, false)
|
||||
this.bus.emit(BusEventTypes.Toast, {
|
||||
autoHide: false,
|
||||
color: 'danger',
|
||||
text: errorText,
|
||||
} as ToastContent)
|
||||
this.bus.emit(BusEventTypes.Toast, errorToast(errorText))
|
||||
throw new Error(`login failed, status=${resp.status}`)
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue