generated from plutack/nextjsauth-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoast-message.tsx
64 lines (56 loc) · 1.7 KB
/
toast-message.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import React from 'react';
import { useToast } from "@/components/ui/use-toast";
import { Toast, ToastDescription, ToastProvider, ToastTitle, ToastViewport } from "@/components/ui/toast";
interface ApiResponse {
success: boolean;
message: string;
}
interface CustomToastProps {
response: ApiResponse;
}
export const CustomToast: React.FC<CustomToastProps> = ({ response }) => {
const { toast } = useToast();
const showToast = React.useCallback(() => {
toast({
variant: response.success ? "default" : "destructive",
title: response.success ? "Success" : "Error",
description: response.message,
});
}, [response, toast]);
React.useEffect(() => {
showToast();
}, [showToast]);
return (
<ToastProvider>
<Toast>
<ToastTitle>{response.success ? "Success" : "Error"}</ToastTitle>
<ToastDescription>{response.message}</ToastDescription>
</Toast>
<ToastViewport />
</ToastProvider>
);
};
// Usage in a component
// import React, { useState } from 'react';
// import { CustomToast } from './CustomToast';
// export const ApiCallComponent: React.FC = () => {
// const [response, setResponse] = useState<ApiResponse | null>(null);
// const makeApiCall = async () => {
// try {
// const res = await fetch('your-api-endpoint');
// const data: ApiResponse = await res.json();
// setResponse(data);
// } catch (error) {
// setResponse({
// success: false,
// message: "An unexpected error occurred"
// });
// }
// };
// return (
// <div>
// <button onClick={makeApiCall}>Make API Call</button>
// {response && <CustomToast response={response} />}
// </div>
// );
// };