-
Notifications
You must be signed in to change notification settings - Fork 0
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
Feature: Notification Placeholder component #8
Open
alaca
wants to merge
10
commits into
master
Choose a base branch
from
feature/notification-placeholder
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
2826994
initial commit
54e1618
reorganize
e7cfa4e
change function name to match type
b40464c
change request method to post
fe0a130
refactor: use useState instead of useReducer
f5f39e4
refactor: remove data check
c8b363f
refactor: use localStorage instead of sessionStorage; add support for…
61ab05f
refactor: make type optional
2a9bf65
feature: update readme
df07f65
refactor: remove notification fetch
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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,50 @@ | ||
import {useState} from 'react'; | ||
|
||
declare const window: { | ||
givewpNotifications: { | ||
apiNonce: string; | ||
notifications: { | ||
user: string[]; | ||
system: string[]; | ||
}; | ||
}; | ||
} & Window; | ||
|
||
export type NotificationType = 'user' | 'system'; | ||
|
||
/** | ||
* Dismiss a notification | ||
*/ | ||
const dismissNotification = async (id: string, type: NotificationType) => { | ||
const response = await fetch('/wp-json/give-api/v2/dismiss-notification', { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'X-WP-Nonce': window.givewpNotifications.apiNonce, | ||
}, | ||
body: JSON.stringify({ | ||
notification: id, | ||
type | ||
}), | ||
}); | ||
|
||
return response.ok ? await response.json() : []; | ||
}; | ||
|
||
/** | ||
* Hook | ||
*/ | ||
export const useNotifications = (type: NotificationType): [Array<string>, Function] => { | ||
const [notifications, setNotifications] = useState( | ||
window.givewpNotifications.notifications[type] | ||
); | ||
|
||
return [ | ||
notifications, | ||
(id: string) => { | ||
dismissNotification(id, type).then((notifications) => { | ||
setNotifications(notifications); | ||
}); | ||
}, | ||
]; | ||
}; |
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,20 @@ | ||
import {MouseEventHandler} from 'react'; | ||
import {useNotifications, NotificationType} from './hooks'; | ||
|
||
interface NotificationProps { | ||
id: string; | ||
render: (dismiss: MouseEventHandler) => JSX.Element, | ||
type?: NotificationType; | ||
} | ||
|
||
export default ({id, render, type = 'user'}: NotificationProps) => { | ||
const [notifications, dismissNotification] = useNotifications(type); | ||
|
||
if (notifications.includes(id)) { | ||
return null; | ||
} | ||
|
||
return render(() => { | ||
dismissNotification(id) | ||
}); | ||
} |
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,45 @@ | ||
## Notification types | ||
There are two notification types, `user` and `system`. | ||
User type notifications when dismissed, won't be shown again only to the user that dismissed the notification, while system notifications when dismissed, won't be shown again to any user. | ||
You will probably use `user` type notifications 99% of the time, but sometimes in some special cases like upgrade action that can be done only once for all users, you might want to use `system` type notifications. | ||
|
||
## Usage | ||
|
||
```jsx | ||
import {NotificationPlaceholder} from '@givewp/form-builder-library'; | ||
|
||
function App() { | ||
return ( | ||
<div> | ||
<h1> | ||
My app title | ||
</h1> | ||
<NotificationPlaceholder | ||
id="notificationUniqueId" | ||
render={dismiss => ( | ||
<div> | ||
<p>User notification - if dismissed it won't be shown again to this user only.</p> | ||
<button onClick={dismiss}>Dismiss</button> | ||
</div> | ||
)} | ||
/> | ||
|
||
<NotificationPlaceholder | ||
id="sysNotificationUniqueId" | ||
type="system" | ||
render={dismiss => ( | ||
<div> | ||
<p>System notification - if dismissed it won't be shown again to any user.</p> | ||
<button onClick={dismiss}>Dismiss</button> | ||
</div> | ||
)} | ||
/> | ||
|
||
<div> | ||
My app content | ||
</div> | ||
</div> | ||
); | ||
|
||
} | ||
``` |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I'm wondering if we should elevate some of these specific endpoints to props so they can be more agnostic and changed during implementation. What do you think?
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 whole idea behind this is that we have a centralized place for handling dismissable notifications. Having endpoints as props means you will have to set the endpoint each time you use this component, which is unnecessary because we already have registered endpoints that won't change. If you want to set a different endpoint, then you will also have to implement the backend logic for that endpoint. For me, I just want to import the component and use it, and not worry about where the requests will go.
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.
I totally understand that, in terms of ease of use that would be great!
What i'm really wrestling with is the GiveWP dependency that this component will be bound to (and this package in general). This was something we talked about when first making this package and being careful about using the window to access things like the currency (in the currency component) so we decided to make those props so the components are more agnostic.
For example, if an add-on is attempting to use this component they would have to require a specific version of GiveWP that this component depends on. It's funny because that was the reason we created this library in the first place, so add-ons are not depending on the window to access components from GiveWP.
That's also where the discussion around a GiveWP data store came up so we're not relying on the window for data as well.
So, the question i'll raise here is: how do we create a library of re-usable components that are agnostic enough to be used throughout our add-ons without necessarily being bound to specific GiveWP versions?
I'd love to chat about this with you in person 😄
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.
@jon Good question. I remember we talked about creating a Give custom store and I think that would be a good approach. So instead of mounting everything to a window object, we can create a custom store and have everything there. The only potential drawback with this is that we won't be able to fully interact with the store outside the FB context - which makes sense.
Example: This PR - notifications.
I want to add a dismissible notification component in the Donation Form list table app. If I try to use the Give custom store outside the FB context, which in this case I do, I won't be able to use
useSelect
oruseDispatch
hooks. We can still dispatch or get the data outside the FB context usingdispatch
andselect
, but since we are outside the FB context, the component from which we call these functions won't re-render. This is an edge case, but still something to have in mind.I can put together a simple store (proof of concept) next Friday, and we can start from there.