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

Update docs on using useMutation with mutate #93

Merged
merged 1 commit into from
Dec 21, 2024
Merged
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
64 changes: 48 additions & 16 deletions docs/care/development/data-fetching-in-care.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,31 +114,63 @@ Note: For mutations (creating, updating, or deleting data), use `useMutation` in

[→ TanStack Docs: Mutations](https://tanstack.com/query/latest/docs/react/guides/mutations)

For creating, updating or deleting data:
CARE provides a `mutate` utility function that works seamlessly with TanStack Query's `useMutation` hook for creating, updating, or deleting data:

```tsx
function CreatePrescription() {
const mutation = useMutation({
mutationFn: (data: PrescriptionData) =>
request(routes.prescriptions.create, { body: data }),
});
import { useMutation } from "@tanstack/react-query";
import mutate from "@/Utils/request/mutate";

async function handleSubmit(data: PrescriptionData) {
const result = await mutation.mutateAsync(data);
if (result.res?.ok) {
toast.success("Prescription created");
}
}
function CreatePrescription({ consultationId }: { consultationId: string }) {
const { mutate: createPrescription, isPending } = useMutation({
mutationFn: mutate(routes.prescriptions.create, {
pathParams: { consultationId },
}),
onSuccess: () => {
toast.success("Prescription created successfully");
},
});

return (
<PrescriptionForm
onSubmit={handleSubmit}
isSubmitting={mutation.isPending}
/>
<Button
onClick={() => createPrescription({
medicineId: "123",
dosage: "1x daily"
})}
disabled={isPending}
>
Create Prescription
</Button>
);
}
```

### Using Path Parameters with Mutations

For URLs that require path parameters, like `/api/v1/patients/123/update/`:

```tsx
function UpdatePatient({ patientId }: { patientId: string }) {
const { mutate: updatePatient } = useMutation({
mutationFn: mutate(routes.patients.update, {
pathParams: { id: patientId },
silent: true // Optional: suppress error notifications
})
});

const handleSubmit = (data: PatientData) => {
updatePatient(data);
};

return <PatientForm onSubmit={handleSubmit} />;
}
```

The `mutate` utility accepts configuration options similar to the `query` utility:
- `pathParams`: For URL parameters
- `queryParams`: For query string parameters
- `silent`: Optional boolean to suppress error notifications
- Additional request options as needed

## Further Reading

For advanced features like:
Expand Down