Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jamesloper/poon-permissions
The easiest way to manage permissions in your PWA
https://github.com/jamesloper/poon-permissions
Last synced: 27 days ago
JSON representation
The easiest way to manage permissions in your PWA
- Host: GitHub
- URL: https://github.com/jamesloper/poon-permissions
- Owner: jamesloper
- Created: 2024-07-24T16:11:07.000Z (5 months ago)
- Default Branch: main
- Last Pushed: 2024-07-28T13:02:33.000Z (5 months ago)
- Last Synced: 2024-07-28T14:26:09.379Z (5 months ago)
- Language: JavaScript
- Size: 8.79 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Simple permission management with `poon-permissions`!!!
The easiest way to manage permissions in your PWA.
Currently supports only Push Notifications, which is what needs the most help. As you know, browser API's are crazy,
which is why this helper library exists.## React Example
This is a React component that will return a button if the user has denied push notifications.
``` javascript
import React, { useEffect } from 'react';
import { Button } from 'poon-ui';
import { usePermission, pushNotifications, DENIED } from 'poon-permissions';
import { setupPush } from '../util/permissions';const PushNotificationsButton = () => {
const status = usePermission(pushNotifications);useEffect(() => {
setupPush();
}, []);if (status === DENIED) return (
);
return null;
};export default PushNotificationsButton;
```Here is basic code for `setupPush()` which is called when the button is clicked.
```javascript
import { showAlert, toast } from 'poon-ui';
import { callMethod } from './util';
import { pushNotifications, GRANTED } from 'poon-permissions';export const setupPush = async () => {
const status = await pushNotifications.askAsync();
if (status === GRANTED) callMethod('PushNotifications', {
'data': await pushNotifications.getConfigAsync(),
'onSuccess': () => toast('Push subscribed'),
});
};
```## Package Exports
| Name | Description |
|---------------------|----------------------------------------------|
| `usePermission` | Used in React, returns the status |
| `GRANTED` | Constant for Granted status |
| `PENDING` | Constant for Pending status (initial status) |
| `DENIED` | Constant for Denied status |
| `pushNotifications` | API for Push Notifications permission! |## Documentation for `pushNotifications`
| Method | Description |
|------------------|--------------------------------------------------------|
| `checkAsync` | Check the permission status (GRANTED, PENDING, DENIED) |
| `askAsync` | Present the system modal and return status |
| `getConfigAsync` | Get the configuration (make sure you askAsync first!) |
| `userDecline` | Call to make checkAsync always return DENIED status |
| `on` | Callback to fire when permission status changes |