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

Feature: Notification Placeholder component #8

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions src/NotificationPlaceholder/hooks/index.ts
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', {
Copy link
Contributor

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?

Copy link
Member Author

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.

Copy link
Contributor

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 😄

Copy link
Member Author

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 or useDispatch hooks. We can still dispatch or get the data outside the FB context using dispatch and select, 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.

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);
});
},
];
};
20 changes: 20 additions & 0 deletions src/NotificationPlaceholder/index.ts
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)
});
}
45 changes: 45 additions & 0 deletions src/NotificationPlaceholder/readme.md
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>
);

}
```
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export {default as ClassicEditor} from "./ClassicEditor";
export {default as OptionsPanel} from "./OptionsPanel";
export {default as SettingsSection} from "./SettingsSection";
export {default as BlockNotice} from "./BlockNotice";
export {default as NotificationPlaceholder} from "./NotificationPlaceholder";