Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Notification Timeout #92

Merged
merged 8 commits into from
Dec 8, 2022
27 changes: 25 additions & 2 deletions frontend/components/context/Notifications/Notification.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,48 @@
import { useEffect, useRef } from "react";
import { faXmarkCircle } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import classnames from "classnames";

import { Notification as NotificationType } from "./NotificationProvider";

interface NotificationProps {
notification: NotificationType;
clearNotification: (text?: string) => void;
notification: Required<NotificationType>;
clearNotification: (text: string) => void;
}

const Notification = ({
notification,
clearNotification,
}: NotificationProps) => {
const timeout = useRef<number>();

const handleClearNotification = () => clearNotification(notification.text);

const setNotifTimeout = () => {
timeout.current = window.setTimeout(
handleClearNotification,
notification.timeoutMs
);
};

const cancelNotifTimeout = () => {
clearTimeout(timeout.current);
};

useEffect(() => {
setNotifTimeout();

return cancelNotifTimeout;
}, []);

return (
<div
className={classnames(
"w-full flex items-center justify-between px-4 py-3 rounded pointer-events-auto",
{
"bg-green-600": notification.type === "success",
"bg-red-500": notification.type === "error",
"bg-blue-500": notification.type === "info",
}
)}
role="alert"
Expand Down
33 changes: 19 additions & 14 deletions frontend/components/context/Notifications/NotificationProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ import { createContext, ReactNode, useContext, useState } from "react";

import Notifications from "./Notifications";

type NotificationType = "success" | "error";
type NotificationType = "success" | "error" | "info";

export type Notification = {
text: string;
type: NotificationType;
type?: NotificationType;
timeoutMs?: number;
};

type NotificationContextState = {
createNotification: ({ text, type }: Notification) => void;
createNotification: (newNotification: Notification) => void;
};

const NotificationContext = createContext<NotificationContextState>({
Expand All @@ -24,26 +25,30 @@ interface NotificationProviderProps {
}

const NotificationProvider = ({ children }: NotificationProviderProps) => {
const [notifications, setNotifications] = useState<Notification[]>([]);

const clearNotification = (text?: string) => {
if (text) {
return setNotifications((state) =>
state.filter((notif) => notif.text !== text)
);
}
const [notifications, setNotifications] = useState<Required<Notification>[]>(
[]
);

return setNotifications([]);
const clearNotification = (text: string) => {
return setNotifications((state) =>
state.filter((notif) => notif.text !== text)
);
};

const createNotification = ({ text, type = "success" }: Notification) => {
const createNotification = ({
text,
type = "success",
timeoutMs = 2000,
}: Notification) => {
const doesNotifExist = notifications.some((notif) => notif.text === text);

if (doesNotifExist) {
return;
}

return setNotifications((state) => [...state, { text, type }]);
const newNotification: Required<Notification> = { text, type, timeoutMs };

return setNotifications((state) => [...state, newNotification]);
};

return (
Expand Down
26 changes: 14 additions & 12 deletions frontend/components/context/Notifications/Notifications.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,27 @@ import Notification from "./Notification";
import { Notification as NotificationType } from "./NotificationProvider";

interface NoticationsProps {
notifications: NotificationType[];
clearNotification: (text?: string) => void;
notifications: Required<NotificationType>[];
clearNotification: (text: string) => void;
}

const Notifications = ({
notifications,
clearNotification,
}: NoticationsProps) => {
if (!notifications.length) {
return null;
}

return (
<div className="hidden fixed z-50 top-1 w-full inset-x-0 pointer-events-none md:flex justify-center">
<div className="flex flex-col gap-y-2 w-96">
{notifications.map((notif) => (
<Notification
key={notif.text}
notification={notif}
clearNotification={clearNotification}
/>
))}
</div>
<div className="hidden fixed z-50 md:flex md:flex-col-reverse bottom-1 gap-y-2 w-96 h-full right-1 pointer-events-none">
{notifications.map((notif) => (
<Notification
key={notif.text}
notification={notif}
clearNotification={clearNotification}
/>
))}
</div>
);
};
Expand Down