-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathErrorModal.tsx
118 lines (108 loc) · 3.18 KB
/
ErrorModal.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import { IoMdClose } from "react-icons/io";
import { useError } from "@/app/context/Error/ErrorContext";
import { ErrorState, ShowErrorParams } from "@/app/types/errors";
import { GeneralModal } from "./GeneralModal";
interface ErrorModalProps {
open: boolean;
onClose: () => void;
onRetry?: () => void;
errorMessage: string;
errorState?: ErrorState;
errorTime: Date;
noCancel?: boolean;
}
export const ErrorModal: React.FC<ErrorModalProps> = ({
open,
onClose,
onRetry,
errorMessage,
errorState,
noCancel,
// errorTime, // This prop is not used in the component
}) => {
const { error, retryErrorAction } = useError();
const handleRetry = () => {
const retryErrorParam: ShowErrorParams = {
error: {
message: error.message,
errorState: error.errorState,
errorTime: new Date(),
},
retryAction: retryErrorAction,
};
onClose();
setTimeout(() => {
if (retryErrorParam.retryAction) {
retryErrorParam.retryAction();
}
}, 300);
};
const getErrorTitle = () => {
switch (errorState) {
case ErrorState.SERVER_ERROR:
return "Server Error";
case ErrorState.WALLET:
return "Network Error";
case ErrorState.WITHDRAW:
return "Withdraw Error";
case ErrorState.STAKING:
return "Stake Error";
case ErrorState.UNBONDING:
return "Unbonding Error";
default:
return "Unknown Error";
}
};
const getErrorMessage = () => {
switch (errorState) {
case ErrorState.SERVER_ERROR:
return `Error fetching data due to: ${errorMessage}`;
case ErrorState.UNBONDING:
return `Your request to unbond failed due to: ${errorMessage}`;
case ErrorState.WITHDRAW:
return `Failed to withdraw due to: ${errorMessage}`;
case ErrorState.STAKING:
return `Failed to stake due to: ${errorMessage}`;
case ErrorState.WALLET:
return `Failed to switch network due to: ${errorMessage}`;
default:
return errorMessage;
}
};
return (
<GeneralModal open={open} onClose={onClose}>
<div className="mb- flex items-center justify-end">
<button
className="btn btn-circle btn-ghost btn-sm"
onClick={() => onClose()}
>
<IoMdClose size={24} />
</button>
</div>
<div className="flex flex-col justify-center gap-4">
<h3 className="text-center font-bold">{getErrorTitle()}</h3>
<div className="flex flex-col gap-3">
<p className="text-center">{getErrorMessage()}</p>
</div>
<div className="mt-4 flex justify-around gap-4">
{!noCancel && ( // Only show the cancel button if noCancel is false or undefined
<button
className="btn btn-outline flex-1 rounded-lg px-2"
onClick={() => onClose()}
>
Cancel
</button>
)}
{onRetry && (
<button
className="btn-primary btn flex-1 rounded-lg px-2 text-white"
onClick={handleRetry}
>
Try Again
</button>
)}
</div>
</div>
</GeneralModal>
);
};