-
Notifications
You must be signed in to change notification settings - Fork 44
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 NotificationContext and useNotification hook #524
Conversation
interface NotificationFunc extends NotificationTypeFunc { | ||
remove: NotificationRemoveProps | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a little hacky. Maybe we should just have a separate hook for when we want to return all notifications and do something with them like removing one.
import { NotificationContext } from '~/app/context/NotificationContext'; | ||
|
||
const ToastNotifications: React.FC = () => { | ||
const [notifications] = useContext(NotificationContext); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current hook only gives access to notification actions. Should we expand it to also return the notifications list? This is why I'm using useContext
here.
7c51326
to
ec52f09
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Haven't tested it yet, but did a quick review, we can take a deeper look later today if you want.
0936ef9
to
61fd6aa
Compare
Ok @lucferbux addressed your comments thanks! |
61fd6aa
to
7b6e4c2
Compare
7b6e4c2
to
9227320
Compare
Ok with the newest push I have added a bunch of testing which includes some for checking the notification pops up correctly. |
@@ -0,0 +1,7 @@ | |||
export class Notification { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we should rename ToastNotification
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, we are not using Notification, we might even refactor this in the future if this is not part of the project but reusable code, we'll see
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At the moment, just rename it and we can call it a day
modelVersionId: 2, | ||
}, | ||
}, | ||
mockBFFResponse(mockModelVersion({})), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Notice how we have to put a bunch of mockBFFResponse
everywhere. We should figure out a way for all of our mockObject
calls to inherently know about the BFF response so we don't have to be so explicit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a great point, my first guess is to just modifify the mock objects and add a wrapper there, can you open a ticket in enhancemets to cover this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Everything looks great, just a couple of nits, really great work here!
@@ -0,0 +1,7 @@ | |||
export class Notification { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, we are not using Notification, we might even refactor this in the future if this is not part of the project but reusable code, we'll see
@@ -0,0 +1,7 @@ | |||
export class Notification { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At the moment, just rename it and we can call it a day
modelVersionId: 2, | ||
}, | ||
}, | ||
mockBFFResponse(mockModelVersion({})), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a great point, my first guess is to just modifify the mock objects and add a wrapper there, can you open a ticket in enhancemets to cover this?
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'] }; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you move this to the types.ts
file under app
?
|
||
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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why don't you move the notification count to the context itself?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why don't I......
9227320
to
56a746e
Compare
Signed-off-by: Griffin-Sullivan <[email protected]>
56a746e
to
93349aa
Compare
/lgtm |
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: ederign, lucferbux The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
Description
This PR adds a new global context for Notifications. I also added a
useNotification
hook to allow for easier dispatching by calling the specific method you want to usenotification.success
ornotification.error
. I also looped in the removal of notifications into this hook. You can call it like:There are a couple known issues at the moment:
How Has This Been Tested?
Merge criteria:
DCO
check)If you have UI changes