feat: updating useNotification hook

- automatically generates id and timestamp
- components just have to provide a message
This commit is contained in:
Mose Müller
2023-08-10 11:04:43 +02:00
parent 6e4909ece5
commit 04e0e9e8b2
9 changed files with 23 additions and 34 deletions

View File

@ -1,16 +1,20 @@
import { useState } from 'react';
type NotificationMsg = {
id: number;
time: string;
text: string;
};
import { Notification } from '../components/NotificationsComponent';
export const useNotification = () => {
const [notifications, setNotifications] = useState([]);
const [notifications, setNotifications] = useState<Notification[]>([]);
const notify = (text: string) => {
// Getting the current time in the required format
const timeString = new Date().toISOString().substring(11, 19);
// Adding an id to the notification to provide a way of removing it
const id = Math.random();
const notify = (message: NotificationMsg) => {
// Custom logic for notifications
setNotifications((prevNotifications) => [message, ...prevNotifications]);
setNotifications((prevNotifications) => [
{ id, text, time: timeString },
...prevNotifications
]);
};
const removeNotificationById = (id: number) => {