-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add NotificationContext and useNotification hook
Signed-off-by: Griffin-Sullivan <[email protected]>
- Loading branch information
1 parent
9dfbd8e
commit 7b6e4c2
Showing
12 changed files
with
306 additions
and
20 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
clients/ui/frontend/src/app/context/NotificationContext.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { AlertVariant } from '@patternfly/react-core'; | ||
import React, { createContext } from 'react'; | ||
|
||
export type Notification = { | ||
id?: number; | ||
status: AlertVariant; | ||
title: string; | ||
message?: React.ReactNode; | ||
hidden?: boolean; | ||
read?: boolean; | ||
timestamp: Date; | ||
}; | ||
|
||
export enum NotificationActionTypes { | ||
ADD_NOTIFICATION = 'add_notification', | ||
DELETE_NOTIFICATION = 'delete_notification', | ||
} | ||
|
||
type NotificationAction = | ||
| { | ||
type: NotificationActionTypes.ADD_NOTIFICATION; | ||
payload: Notification; | ||
} | ||
| { | ||
type: NotificationActionTypes.DELETE_NOTIFICATION; | ||
payload: { id: Notification['id'] }; | ||
}; | ||
|
||
type NotificationContextProps = { | ||
notifications: Notification[]; | ||
dispatch: React.Dispatch<NotificationAction>; | ||
}; | ||
|
||
export const NotificationContext = createContext<NotificationContextProps>({ | ||
notifications: [], | ||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
dispatch: () => {}, | ||
}); | ||
|
||
const notificationReducer: React.Reducer<Notification[], NotificationAction> = ( | ||
notifications, | ||
action, | ||
) => { | ||
switch (action.type) { | ||
case NotificationActionTypes.ADD_NOTIFICATION: { | ||
return [ | ||
...notifications, | ||
{ | ||
status: action.payload.status, | ||
title: action.payload.title, | ||
timestamp: action.payload.timestamp, | ||
message: action.payload.message, | ||
id: action.payload.id, | ||
}, | ||
]; | ||
} | ||
case NotificationActionTypes.DELETE_NOTIFICATION: { | ||
return notifications.filter((t) => t.id !== action.payload.id); | ||
} | ||
default: { | ||
return notifications; | ||
} | ||
} | ||
}; | ||
|
||
type NotificationContextProviderProps = { | ||
children: React.ReactNode; | ||
}; | ||
|
||
export const NotificationContextProvider: React.FC<NotificationContextProviderProps> = ({ | ||
children, | ||
}) => { | ||
const [notifications, dispatch] = React.useReducer(notificationReducer, []); | ||
|
||
return ( | ||
<NotificationContext.Provider | ||
value={React.useMemo( | ||
() => ({ | ||
notifications, | ||
dispatch, | ||
}), | ||
[notifications, dispatch], | ||
)} | ||
> | ||
{children} | ||
</NotificationContext.Provider> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import React, { useContext } from 'react'; | ||
import { AlertVariant } from '@patternfly/react-core'; | ||
import { NotificationActionTypes, NotificationContext } from '~/app/context/NotificationContext'; | ||
|
||
enum NotificationTypes { | ||
SUCCESS = 'success', | ||
ERROR = 'error', | ||
INFO = 'info', | ||
WARNING = 'warning', | ||
} | ||
|
||
type NotificationProps = (title: string, message?: React.ReactNode) => void; | ||
|
||
type NotificationRemoveProps = (id: number | undefined) => void; | ||
|
||
type NotificationTypeFunc = { | ||
[key in NotificationTypes]: NotificationProps; | ||
}; | ||
|
||
interface NotificationFunc extends NotificationTypeFunc { | ||
remove: NotificationRemoveProps; | ||
} | ||
|
||
export const useNotification = (): NotificationFunc => { | ||
const { dispatch } = useContext(NotificationContext); | ||
// need to move this count somewhere else since it will reset on every new useNotification instance (like switching pages) | ||
let notificationCount = 0; | ||
|
||
const success: NotificationProps = React.useCallback( | ||
(title, message?) => { | ||
dispatch({ | ||
type: NotificationActionTypes.ADD_NOTIFICATION, | ||
payload: { | ||
status: AlertVariant.success, | ||
title, | ||
timestamp: new Date(), | ||
message, | ||
id: ++notificationCount, | ||
}, | ||
}); | ||
}, | ||
[dispatch, notificationCount], | ||
); | ||
|
||
const warning: NotificationProps = React.useCallback( | ||
(title, message?) => { | ||
dispatch({ | ||
type: NotificationActionTypes.ADD_NOTIFICATION, | ||
payload: { | ||
status: AlertVariant.warning, | ||
title, | ||
timestamp: new Date(), | ||
message, | ||
id: ++notificationCount, | ||
}, | ||
}); | ||
}, | ||
[dispatch, notificationCount], | ||
); | ||
|
||
const error: NotificationProps = React.useCallback( | ||
(title, message?) => { | ||
dispatch({ | ||
type: NotificationActionTypes.ADD_NOTIFICATION, | ||
payload: { | ||
status: AlertVariant.danger, | ||
title, | ||
timestamp: new Date(), | ||
message, | ||
id: ++notificationCount, | ||
}, | ||
}); | ||
}, | ||
[dispatch, notificationCount], | ||
); | ||
|
||
const info: NotificationProps = React.useCallback( | ||
(title, message?) => { | ||
dispatch({ | ||
type: NotificationActionTypes.ADD_NOTIFICATION, | ||
payload: { | ||
status: AlertVariant.info, | ||
title, | ||
timestamp: new Date(), | ||
message, | ||
id: ++notificationCount, | ||
}, | ||
}); | ||
}, | ||
[dispatch, notificationCount], | ||
); | ||
|
||
const remove: NotificationRemoveProps = React.useCallback( | ||
(id) => { | ||
dispatch({ | ||
type: NotificationActionTypes.DELETE_NOTIFICATION, | ||
payload: { id }, | ||
}); | ||
}, | ||
[dispatch], | ||
); | ||
|
||
const notification = React.useMemo( | ||
() => ({ success, error, info, warning, remove }), | ||
[success, error, info, warning, remove], | ||
); | ||
|
||
return notification; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.