-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
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
[Doc] Add Real Time Notifications section in Realtime introduction #9304
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 |
---|---|---|
|
@@ -97,6 +97,112 @@ And the following components: | |
- [`<EditLive>`](./EditLive.md) | ||
- [`<ShowLive>`](./ShowLive.md) | ||
|
||
## Real Time Notifications | ||
|
||
Thanks to the Ra-realtime hooks, you can implement custom notifications based on events. For instance, consider a long server action called `recompute` for which you'd like to show the progression. | ||
|
||
<video controls autoplay playsinline muted loop> | ||
<source src="./img/useSubscribeCallback.webm" type="video/webm"/> | ||
<source src="./img/useSubscribeCallback.mp4" type="video/mp4"/> | ||
Your browser does not support the video tag. | ||
</video> | ||
|
||
First, leverage the ability to [add custom dataProvider methods](https://marmelab.com/react-admin/Actions.html#calling-custom-methods) to allow calling this custom end point from the UI: | ||
|
||
```ts | ||
export const dataProvider = { | ||
// ...standard dataProvider methods such as getList, etc. | ||
recompute: async (params) => { | ||
httpClient(`${apiUrl}/recompute`, { | ||
method: 'POST', | ||
body: JSON.stringify(params.data), // contains the project's id | ||
}).then(({ json }) => ({ data: json })); | ||
} | ||
} | ||
``` | ||
|
||
Then, make sure your API sends events with a topic named `recompute_PROJECT_ID` where `PROJECT_ID` is the project identifier: | ||
|
||
```json | ||
{ | ||
"type": "recompute_PROJECT_ID", | ||
"payload": { | ||
"progress": 10 | ||
}, | ||
} | ||
``` | ||
|
||
Finally, create a component to actually call this function and show a notification, leveraging the [useSubscribeCallback](./useSubscribeCallback.md) hook: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Nos sure which function you're talking about |
||
|
||
{% raw %} | ||
```jsx | ||
import { useState, useCallback } from 'react'; | ||
import { useDataProvider, useRecordContext } from 'react-admin'; | ||
import { Box, Button, Card, Alert, AlertTitle, LinearProgress, Typography } from '@mui/material'; | ||
import { useSubscribeCallback } from '@react-admin/ra-realtime'; | ||
|
||
export const RecomputeProjectStatsButton = () => { | ||
const dataProvider = useDataProvider(); | ||
const record = useRecordContext(); | ||
const [progress, setProgress] = useState(0); | ||
|
||
const callback = useCallback( | ||
(event, unsubscribe) => { | ||
setProgress(event.payload?.progress || 0); | ||
if (event.payload?.progress === 100) { | ||
unsubscribe(); | ||
} | ||
}, | ||
[setProgress] | ||
); | ||
const subscribe = useSubscribeCallback( | ||
`recompute_${record.id}`, | ||
callback | ||
); | ||
|
||
return ( | ||
<div> | ||
<Button | ||
onClick={() => { | ||
subscribe(); | ||
dataProvider.recomputeProjectStats({ id: record.id }); | ||
}} | ||
> | ||
Recompute | ||
</Button> | ||
{progress > 0 && ( | ||
<Card sx={{ m: 2, maxWidth: 400 }}> | ||
<Alert severity={progress === 100 ? 'success' : 'info'}> | ||
<AlertTitle> | ||
Recomputing stats{' '} | ||
{progress === 100 ? 'complete' : 'in progress'} | ||
</AlertTitle> | ||
<LinearProgressWithLabel value={progress} /> | ||
djhi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
</Alert> | ||
</Card> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
const LinearProgressWithLabel = props => { | ||
return ( | ||
<Box sx={{ display: 'flex', alignItems: 'center' }}> | ||
<Box sx={{ width: '100%', mr: 1 }}> | ||
<LinearProgress variant="determinate" {...props} /> | ||
</Box> | ||
<Box sx={{ minWidth: 35 }}> | ||
<Typography | ||
variant="body2" | ||
color="text.secondary" | ||
>{`${Math.round(props.value)}%`}</Typography> | ||
</Box> | ||
</Box> | ||
); | ||
}; | ||
``` | ||
{% endraw %} | ||
|
||
## Menu Badges | ||
|
||
Ra-realtime also provides **badge notifications in the Menu**, so that users can see that something new happened to a resource list while working on another one. | ||
|
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.
the sentence above mentions "topic", but the snippet uses "type". Is it normal?