Skip to content

wappla/notifications

Repository files navigation

@dashdot/notifications

Built with ❤︎ by Dashdot and contributors

Table of Contents

Motivation

This package contains a Notification dispatcher EventListener to dispatch and listen to notifications

Installing

Install package

npm i @dashdot/notifications

Add provider

import { getNotificationDispatcher } from '@dashdot/notifications/NotificationDispatcher'
import NotificationsProvider from '@dashdot/notifications/NotificationsProvider'

export default function AppProviders() {
    return (
        <NotificationsProvider value={getNotificationDispatcher()}>
            {children}
        </NotificationsProvider>
    )
}

Subscribe and dispatch to notifications

import { useEffect, useState, useContext } from 'react'
import { NotificationsContext } from '@dashdot/notifications/NotificationsProvider'
import {
    Notification,
    NotificationData,
    ENotificationType,
} from '@dashdote/notifications/NotificationDispatcher'

export default function useNotifications() {
    const notificationDispatcher = useContext(NotificationsContext)
    const [notifications, setNotifications] = useState<[Notification] | []>([])
    useEffect(
        () =>
            notificationDispatcher?.subscribe(
                (newNotifications: [Notification]) => {
                    setNotifications(newNotifications)
                },
            ),
        [notificationDispatcher],
    )

    const success = (data = {}, duration?: undefined | number) => {
        notificationDispatcher?.dispatch(
            {
                type: ENotificationType.SUCCESS,
                title: 'Success,
                ...data,
            },
            duration,
        )
    }

    const removeNotification = (notification: any) => {
        notificationDispatcher?.removeNotification(notification)
    }

    return {
        notifications,
        success,
        dispatch: (
            data: NotificationData,
            createdAt?: number,
            duration?: number,
        ) => notificationDispatcher?.dispatch(data, createdAt, duration),
        removeNotification,
    }
}

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

Authors

See also the list of contributors who participated in this project.

About us

Dashdot BV We shape, build and grow ambitious digital products.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published