File

src/app/toast.service.ts

Description

Provides methods to notify the user of errors and various message via toast notification.

Index

Methods

Constructor

constructor()

Constructor

Methods

toast
toast(text: )

Displays a message to the user via toast notification.

Parameters :
Name Optional Description
text no

Message to display

Returns : void
toastError
toastError(e: )

Displays an error to the user via toast notification.

Parameters :
Name Optional Description
e no

Error to display

Returns : void
import { Injectable } from '@angular/core';
import { toast } from 'angular2-materialize';

/**
 * Provides methods to notify the user of errors and various message
 * via toast notification.
 */
@Injectable({
    providedIn: 'root'
})
export class ToastService {

    /**
     * Constructor
     */
    constructor() { }

    /**
     * Displays an error to the user via toast notification.
     * @param {Error} e Error to display
     */
    toastError(e) {
        function buildHTML(message) {
            console.log(typeof message, message);
            if (typeof message === 'object') {
                message = JSON.stringify(message);
            }
            return `
                <span style="color:red !important">${JSON.stringify(message)}</span>
                <button onclick="document.querySelector('.toast').remove()" class="btn-flat toast-action">
                    Dismiss
                </button>
            `;
        }
        // If just text is sent in
        let text = e;

        // Classic Error
        if (e instanceof Error) {
            text = e.message;

            // If a web socket goes down unexpectedly
        } else if (e instanceof Event && e.target instanceof WebSocket && (e.target.readyState === 3 || e.target.readyState === 2)) {
            text = 'Websocket unexpectedly closed. Server may be down.';
        }
        toast(buildHTML(text));
        setTimeout(() => {
            document.querySelector('.toast').remove();
        }, 15000);
    }

    /**
     * Displays a message to the user via toast notification.
     * @param {string} text Message to display
     */
    toast(text) {
        function buildHTML(message) {
            return `
                <span>${JSON.stringify(message)}</span>
                <button onclick="document.querySelector('.toast').remove()" class="btn-flat toast-action">
                    Dismiss
                </button>
            `;
        }
        toast(buildHTML(text));
        setTimeout(() => {
            document.querySelector('.toast').remove();
        }, 5000);
    }
}

results matching ""

    No results matching ""