eastern-cyan
eastern-cyan5mo ago

Confirmation on mutate update

How can i add Confirmation on mutate update for useUpdate
2 Replies
kapa.ai
kapa.ai5mo ago
Hi @Joe Gaffney. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- The useUpdate hook in Refine does not directly support a confirmation mechanism. However, you can implement a confirmation step before calling the mutate function from useUpdate. Here's a basic example:
import { useUpdate } from "@refinedev/core";

const { mutate } = useUpdate();

const handleUpdate = (data) => {
if (window.confirm("Are you sure you want to update this data?")) {
mutate(data);
}
};
import { useUpdate } from "@refinedev/core";

const { mutate } = useUpdate();

const handleUpdate = (data) => {
if (window.confirm("Are you sure you want to update this data?")) {
mutate(data);
}
};
In this example, handleUpdate is a function that shows a confirmation dialog. If the user confirms, it calls mutate with the provided data. Remember that the useUpdate hook also provides successNotification and errorNotification props that allow you to customize the notifications shown when the update operation is successful or fails. You need to have NotificationProvider for these props to work. Here's an example of how you can use these props:
const { mutate } = useUpdate();

mutate({
successNotification: (data, values, resource) => {
return {
message: `${data.title} Successfully fetched.`,
description: "Success with no errors",
type: "success",
};
},
errorNotification: (data, values, resource) => {
return {
message: `Something went wrong when getting ${data.id}`,
description: "Error",
type: "error",
};
},
});
const { mutate } = useUpdate();

mutate({
successNotification: (data, values, resource) => {
return {
message: `${data.title} Successfully fetched.`,
description: "Success with no errors",
type: "success",
};
},
errorNotification: (data, values, resource) => {
return {
message: `Something went wrong when getting ${data.id}`,
description: "Error",
type: "error",
};
},
});
For more information, you can check the useUpdate documentation (https://refine.dev/docs/data/hooks/use-update/). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai5mo ago
<@422289470487003153> kapa.ai is still learning and improving, please let us know how it did by reacting below