import * as uuid from 'uuid'; import { defineComponent, ref, onUnmounted } from 'vue'; import type { BCMSNotificationMessage, BCMSNotificationMessageType, } from '../types'; import BCMSIcon from './icon'; const component = defineComponent({ setup() { const notification = window.bcms.notification; const timeout = 8000; const notifUnreg = notification.register((type, content) => { const message: BCMSNotificationMessage = { id: uuid.v4(), type, content, }; if (messages.value.find((e) => e.content === message.content)) { return; } setTimeout(() => { messages.value = messages.value.filter((e) => e.id !== message.id); }, timeout); messages.value = [...messages.value, message]; }); const messages = ref([]); function messageTypeClass(type: BCMSNotificationMessageType) { switch (type) { case 'error': { return 'bg-error'; } case 'success': { return 'bg-success'; } case 'warning': { return 'bg-warning'; } } } onUnmounted(() => { notifUnreg(); }); function getTypeIcon(type: BCMSNotificationMessageType) { switch (type) { case 'error': { return ( ); } case 'success': { return ( ); } case 'warning': { return ( ); } } } return () => { return (
{messages.value.map((message) => { return (
{getTypeIcon(message.type)}

{message.content}

); })}
); }; }, }); export default component;