Skip to content

Commit

Permalink
(docs) Update data fetching guide (openmrs#619)
Browse files Browse the repository at this point in the history
  • Loading branch information
denniskigen committed Feb 28, 2023
1 parent 975fc6f commit 6fd9c5f
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions docs/main/data.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,13 @@ interface VisitData {
export function useVisits() {
const url = `/ws/rest/v1/visit?includeInactive=false`;

const { data, error } = useSWR<{ data: VisitData }, Error>(url, openmrsFetch);
const { data, error, isLoading, mutate } = useSWR<{ data: VisitData }, Error>(url, openmrsFetch);

return {
visits: data,
isLoading: !data && !error,
isError: error,
isLoading,
error,
mutate,
};
}
```
Expand All @@ -74,7 +75,7 @@ const Visits = () => {
if (isLoading) {
return <DataTableSkeleton role="progressbar" />
}
if (isError) {
if (error) {
// render error state
}
if (visits?.length) {
Expand All @@ -88,6 +89,23 @@ const Visits = () => {
export default Visits;
```
The `mutate` function returned by `useSWR` can be used to update the cache and trigger a re-render of the component. This is useful when we want to update the UI after a successful mutation.
```typescript
const res = await saveVisitNote(payload);

try {
if (res.status === 201) {
mutate();
closeWorkspace();

// show success toast
}
} catch (error) {
// handle error
}
```
## Posting data to the server
Here's an example that demonstrates posting session data to the server.
Expand Down

0 comments on commit 6fd9c5f

Please sign in to comment.