From 9499aa1097afad876f91548e345d285d9b68325e Mon Sep 17 00:00:00 2001 From: asharonbaltazar Date: Tue, 6 Dec 2022 12:49:44 -0500 Subject: [PATCH] feat: move NotificationProvider into Notifications folder --- .../context/NotificationProvider.tsx | 113 ------------------ .../Notifications/NotificationProvider.tsx | 64 ++++++++++ 2 files changed, 64 insertions(+), 113 deletions(-) delete mode 100644 frontend/components/context/NotificationProvider.tsx create mode 100644 frontend/components/context/Notifications/NotificationProvider.tsx diff --git a/frontend/components/context/NotificationProvider.tsx b/frontend/components/context/NotificationProvider.tsx deleted file mode 100644 index 98e24f5abc..0000000000 --- a/frontend/components/context/NotificationProvider.tsx +++ /dev/null @@ -1,113 +0,0 @@ -import { createContext, ReactNode, useContext, useState } from "react"; -import { faXmarkCircle } from "@fortawesome/free-solid-svg-icons"; -import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; -import classnames from "classnames"; - -type NotificationType = "success" | "error"; -type Notification = { - text: string; - type: NotificationType; -}; - -type NotificationContextState = { - createNotification: (text: string, type?: NotificationType) => void; -}; -const NotificationContext = createContext({ - createNotification: () => console.log("createNotification not set!"), -}); - -export const useNotificationContext = () => useContext(NotificationContext); - -interface NotificationProviderProps { - children: ReactNode; -} - -interface NoticationsProps { - notifications: Notification[]; - clearNotification: (text?: string) => void; -} - -const Notifications = ({ - notifications, - clearNotification, -}: NoticationsProps) => { - return ( -
-
- {notifications.map((notif) => ( -
-

{notif.text}

- -
- ))} -
-
- ); -}; - -const NotificationProvider = ({ children }: NotificationProviderProps) => { - const [notifications, setNotifications] = useState([ - { - text: "Your secrets weren't saved, please fix the conflicts first.", - type: "error", - }, - { - text: "Testing", - type: "success", - }, - ]); - - const clearNotification = (text?: string) => { - if (text) { - return setNotifications((state) => - state.filter((notif) => notif.text !== text) - ); - } - - return setNotifications([]); - }; - - const createNotification = ( - text: string, - type: NotificationType = "success" - ) => { - const doesNotifExist = notifications.some((notif) => notif.text === text); - - if (doesNotifExist) { - return; - } - - return setNotifications((state) => [...state, { text, type }]); - }; - - return ( - - - {children} - - ); -}; - -export default NotificationProvider; diff --git a/frontend/components/context/Notifications/NotificationProvider.tsx b/frontend/components/context/Notifications/NotificationProvider.tsx new file mode 100644 index 0000000000..723319615c --- /dev/null +++ b/frontend/components/context/Notifications/NotificationProvider.tsx @@ -0,0 +1,64 @@ +import { createContext, ReactNode, useContext, useState } from "react"; + +import Notifications from "./Notifications"; + +type NotificationType = "success" | "error"; + +export type Notification = { + text: string; + type: NotificationType; +}; + +type NotificationContextState = { + createNotification: ({ text, type }: Notification) => void; +}; + +const NotificationContext = createContext({ + createNotification: () => console.log("createNotification not set!"), +}); + +export const useNotificationContext = () => useContext(NotificationContext); + +interface NotificationProviderProps { + children: ReactNode; +} + +const NotificationProvider = ({ children }: NotificationProviderProps) => { + const [notifications, setNotifications] = useState([]); + + const clearNotification = (text?: string) => { + if (text) { + return setNotifications((state) => + state.filter((notif) => notif.text !== text) + ); + } + + return setNotifications([]); + }; + + const createNotification = ({ text, type = "success" }: Notification) => { + const doesNotifExist = notifications.some((notif) => notif.text === text); + + if (doesNotifExist) { + return; + } + + return setNotifications((state) => [...state, { text, type }]); + }; + + return ( + + + {children} + + ); +}; + +export default NotificationProvider;