import { Controller } from "@hotwired/stimulus" import Toastify from 'toastify-js' export default class extends Controller { static targets = ['flashItem'] OPTIONS = { selector: 'flashbar', escapeMarkup: false, close: true, position: 'center', offset: { y: 30 }, } onLoad(event) { this.flashItemTargets.forEach((item) => { this.popup({ detail: { type: item.dataset.type, message: item.textContent.trim() } }) }) } icon(drawing) { return ` ` } iconNotice() { return this.icon('M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z') } iconAlert() { return this.icon('M10 14l2-2m0 0l2-2m-2 2l-2-2m2 2l2 2m7-2a9 9 0 11-18 0 9 9 0 0118 0z') } className(toastType) { return `alert alert-${toastType} inset-ring-2 shadow-xl/50` } classNameNotice() { return this.className('success') } classNameAlert() { return this.className('error') } textNotice(message) { return `${this.iconNotice()}
${message}
` } textAlert(message) { return `${this.iconAlert()}
${message}
` } popup(event) { const { type, message } = event.detail switch (type) { case 'notice': this.notice(message); break case 'alert': this.alert(message); break default: console.error(`undefined popup message type: ${type}`) } } log(type, message) { switch (type) { case 'notice': console.info(`FLASH:${type} => ${message}`); break case 'alert': console.warn(`FLASH:${type} => ${message}`); break } } notice(message) { Toastify({ ...this.OPTIONS, text: this.textNotice(message), className: this.classNameNotice(), }).showToast(); this.log('notice', message) } alert(message) { Toastify({ ...this.OPTIONS, text: this.textAlert(message), className: this.classNameAlert(), }).showToast(); this.log('alert', message) } }