Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | 34x 34x 34x 34x 34x 34x 34x 34x | const serviceImplementation = { _hide: () => console.debug('hide() NOT IMPLEMENTED'), _show: showArguments => { console.debug('show() NOT IMPLEMENTED'); return null; }, }; type ToastType = 'success' | 'error' | 'info' | 'warning' | 'loading'; class UINotificationService { static REGISTRATION = { name: 'uiNotificationService', altName: 'UINotificationService', create: (): UINotificationService => { return new UINotificationService(); }, }; /** * * * @param {*} { * hide: hideImplementation, * show: showImplementation, * } */ public setServiceImplementation({ hide: hideImplementation, show: showImplementation }): void { if (hideImplementation) { serviceImplementation._hide = hideImplementation; } if (showImplementation) { serviceImplementation._show = showImplementation; } } /** * Hides/dismisses the notification, if currently shown * * @param {number} id - id of the notification to hide/dismiss * @returns undefined */ public hide(id: string) { return serviceImplementation._hide(id); } /** * Create and show a new UI notification; returns the * ID of the created notification. Can also handle promises for loading states. * * @param {object} notification - The notification object * @param {string} notification.title - The title of the notification * @param {string | function} notification.message - The message content of the notification or a function that returns a message * @param {number} [notification.duration=5000] - The duration to show the notification (in milliseconds) * @param {'top-left' | 'top-right' | 'bottom-left' | 'bottom-right' | 'top-center' | 'bottom-center'} [notification.position='bottom-right'] - The position of the notification * @param {ToastType} [notification.type='info'] - The type of the notification * @param {boolean} [notification.autoClose=true] - Whether the notification should auto-close * @param {Promise} [notification.promise] - A promise to track for loading, success, and error states * @param {object} [notification.promiseMessages] - Custom messages for promise states * @param {string} [notification.promiseMessages.loading] - Message to show while promise is pending * @param {string | function} [notification.promiseMessages.success] - Message to show when promise resolves * @param {string | function} [notification.promiseMessages.error] - Message to show when promise rejects * @returns {string} id - The ID of the created notification */ show({ title, message, duration = 5000, position = 'bottom-right', type = 'info', autoClose = true, promise, promiseMessages, }: { title: string; message: string | ((data?: any) => string); duration?: number; position?: | 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right' | 'top-center' | 'bottom-center'; type?: ToastType; autoClose?: boolean; promise?: Promise<any>; promiseMessages?: { loading?: string; success?: string | ((data: any) => string); error?: string | ((error: any) => string); }; }): string { Iif (promise && promiseMessages) { const loadingId = serviceImplementation._show({ title, message: promiseMessages.loading || 'Loading...', type: 'loading', autoClose: false, position, }); promise.then( data => { const successMessage = typeof promiseMessages.success === 'function' ? promiseMessages.success(data) : promiseMessages.success || 'Success'; serviceImplementation._show({ title, message: successMessage, type: 'success', duration, position, autoClose, }); this.hide(loadingId); }, error => { const errorMessage = typeof promiseMessages.error === 'function' ? promiseMessages.error(error) : promiseMessages.error || 'Error'; serviceImplementation._show({ title, message: errorMessage, type: 'error', duration, position, autoClose, }); this.hide(loadingId); } ); return loadingId; } return serviceImplementation._show({ title, message, duration, position, type, autoClose, }); } } export default UINotificationService; |