fix: Added cancelMutation to toast

This commit is contained in:
Tania Gutierrez 2024-03-19 11:38:42 -04:00
parent 37ad1d1dc9
commit 34e9009e96
Signed by: riobuenoDevelops
GPG Key ID: 53133EB28EB7E801
5 changed files with 57 additions and 36 deletions

View File

@ -41,7 +41,7 @@ const toastVariants = cva(
const Toast = React.forwardRef< const Toast = React.forwardRef<
React.ElementRef<typeof ToastPrimitives.Root>, React.ElementRef<typeof ToastPrimitives.Root>,
React.ComponentPropsWithoutRef<typeof ToastPrimitives.Root> & React.ComponentPropsWithoutRef<typeof ToastPrimitives.Root> &
VariantProps<typeof toastVariants> VariantProps<typeof toastVariants> & { cancelMutation?: () => void }
>(({ className, variant, ...props }, ref) => { >(({ className, variant, ...props }, ref) => {
return ( return (
<ToastPrimitives.Root <ToastPrimitives.Root

View File

@ -1,5 +1,6 @@
import { import {
Toast, Toast,
ToastAction,
ToastClose, ToastClose,
ToastDescription, ToastDescription,
ToastProvider, ToastProvider,
@ -13,7 +14,8 @@ export function Toaster() {
return ( return (
<ToastProvider> <ToastProvider>
{toasts.map(function ({ id, title, description, action, ...props }) { {toasts.map(function ({ id, title, description, action, cancelMutation, ...props }) {
const undoButton = cancelMutation ? <ToastAction altText="Undo" onClick={cancelMutation}>Undo</ToastAction> : undefined
return ( return (
<Toast key={id} {...props}> <Toast key={id} {...props}>
<div className="grid gap-1"> <div className="grid gap-1">
@ -23,6 +25,7 @@ export function Toaster() {
)} )}
</div> </div>
{action} {action}
{undoButton}
<ToastClose /> <ToastClose />
</Toast> </Toast>
) )

View File

@ -1,26 +0,0 @@
import { NotificationProvider } from "@refinedev/core";
import { ToastAction } from "~/components/ui/toast";
import { toast } from "~/components/ui/use-toast";
export const notificationProvider = (): NotificationProvider => {
return {
open: ({
key,
message,
type,
description,
undoableTimeout,
cancelMutation,
}) => {
toast({
variant: type,
key,
title: message,
description,
duration: undoableTimeout,
})
},
close: () => {}
}
};

View File

@ -0,0 +1,40 @@
import type {
NotificationProvider,
OpenNotificationParams,
} from "@refinedev/core";
import type { ToastActionElement } from "~/components/ui/toast";
import { toast } from "~/components/ui/use-toast";
interface Provider extends Omit<NotificationProvider, "open"> {
open: (
params: Omit<OpenNotificationParams, "type"> & {
action?: ToastActionElement;
type: "default" | "destructive";
},
) => void;
}
export const notificationProvider = () => {
return {
open: ({
key,
message,
description,
undoableTimeout,
cancelMutation,
action,
type,
}) => {
toast({
variant: type,
key,
title: message,
description,
duration: undoableTimeout,
action,
cancelMutation,
});
},
close: () => {},
} satisfies Provider;
};

View File

@ -9,7 +9,8 @@ import { Field } from "~/components/forms";
import { getFormProps, useForm } from "@conform-to/react"; import { getFormProps, useForm } from "@conform-to/react";
import { z } from "zod"; import { z } from "zod";
import { getZodConstraint, parseWithZod } from "@conform-to/zod"; import { getZodConstraint, parseWithZod } from "@conform-to/zod";
import { useToast } from "~/components/ui/use-toast"; import { ToastAction } from "~/components/ui/toast";
import { useNotification } from "@refinedev/core";
export const meta: MetaFunction = () => { export const meta: MetaFunction = () => {
return [{ title: "Sign Up" }]; return [{ title: "Sign Up" }];
@ -19,7 +20,7 @@ const RecoverPasswordSchema = z.object({
email: z.string().email(), email: z.string().email(),
}); });
export default function RecoverPassword() { export default function RecoverPassword() {
const { toast } = useToast(); const { open } = useNotification();
const [form, fields] = useForm({ const [form, fields] = useForm({
id: "sign-up", id: "sign-up",
constraint: getZodConstraint(RecoverPasswordSchema), constraint: getZodConstraint(RecoverPasswordSchema),
@ -28,13 +29,16 @@ export default function RecoverPassword() {
}, },
onSubmit(e) { onSubmit(e) {
e.preventDefault(); e.preventDefault();
open?.({
toast({ type: "success",
title: "Password reset email sent", message: "Password reset email sent",
description: "Check your email for a link to reset your password. If it doesnt appear within a few minutes, check your spam folder.", description: "Check your email for a link to reset your password. If it doesnt appear within a few minutes, check your spam folder.",
variant: "success", action: <ToastAction altText="Cancel">Cancel</ToastAction>,
key: "reset-password-email-sent", cancelMutation: () => {
}); console.log("cancel mutation");
},
})
} }
}); });