-
Notifications
You must be signed in to change notification settings - Fork 22
/
FeedbacksProvider.tsx
197 lines (177 loc) · 4.85 KB
/
FeedbacksProvider.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import React, {
ReactNode,
createContext,
useCallback,
useContext,
useEffect,
useState,
} from "react";
import { Platform } from "react-native";
import { LoaderFullScreen } from "../components/loaders/LoaderFullScreen";
import {
NormalToast,
NormalToastProps,
} from "../components/toasts/NormalToast";
import { MiniToast, MiniToastProps } from "@/components/toasts/MiniToast";
interface INormalToast extends NormalToastProps {
mode: "normal";
duration?: number;
}
interface IMiniToast extends MiniToastProps {
mode: "mini";
duration?: number;
}
type Toast = IMiniToast | INormalToast;
export const initialToast: Toast = {
message: "",
title: "",
duration: 8000,
mode: "normal",
type: "success",
};
/**
* @deprecated Use initialToast instead
*/
export const initialToastError: Pick<
INormalToast,
"message" | "duration" | "onPress" | "title"
> = {
title: "",
message: "",
duration: 8000,
};
interface FeedbacksProviderValue {
/**
* @deprecated Use setToast instead
*/
setToastError: (
error: Pick<INormalToast, "message" | "duration" | "onPress" | "title">,
) => void;
/**
* @deprecated Use setToast instead
*/
setToastSuccess: (
info: Pick<INormalToast, "message" | "duration" | "onPress" | "title">,
) => void;
loadingFullScreen: boolean;
setLoadingFullScreen: (loading: boolean) => void;
wrapWithFeedback: (
cb: () => Promise<void>,
success?: { title: string; message?: string },
errorTransform?: (err: unknown) => { title: string; message?: string },
) => () => Promise<void>;
setToast: (data: Toast) => void;
}
const defaultValue: FeedbacksProviderValue = {
setToastError: () => {},
setToastSuccess: () => {},
loadingFullScreen: false,
setLoadingFullScreen: () => {},
wrapWithFeedback: () => async () => {},
setToast: () => {},
};
const FeedbacksContext = createContext(defaultValue);
export const FeedbacksContextProvider: React.FC<{ children: ReactNode }> = ({
children,
}) => {
const [loadingFullScreen, setLoadingFullScreen] = useState(false);
const [toast, setToast] = useState<Toast>(initialToast);
const { onPress: onToastPress, ...toastRestProps } = toast;
const clearToast = () => setToast(initialToast);
useEffect(() => {
const toastDuration = toast.duration || initialToast.duration;
const timeoutID = setTimeout(() => {
// If the toast is in "mini" mode and showAlways flag is enabled, do not clear the toast
const shouldClearToast = !(toast.mode === "mini" && toast.showAlways);
if (shouldClearToast) {
clearToast();
}
}, toastDuration);
return () => clearTimeout(timeoutID);
}, [toast]);
const wrapWithFeedback: FeedbacksProviderValue["wrapWithFeedback"] =
useCallback(
(
cb,
success = { title: "Success", message: "" },
errorTransform = (err) => {
console.error(err);
return {
title: "Error",
message: err instanceof Error ? err.message : `${err}`,
};
},
) => {
return async () => {
try {
await cb();
setToast({
mode: "normal",
type: "success",
message: "",
...success,
});
} catch (err) {
setToast({
mode: "normal",
type: "error",
message: "",
...errorTransform(err),
});
}
};
},
[],
);
const tapToClear = () => {
clearToast();
if (onToastPress) {
onToastPress();
}
};
return (
<FeedbacksContext.Provider
value={{
loadingFullScreen,
setLoadingFullScreen,
setToastError: ({ title = "", ...args }) =>
setToast({ mode: "normal", type: "error", title, ...args }),
setToastSuccess: ({ title = "", ...args }) =>
setToast({
mode: "normal",
type: "success",
title,
...args,
}),
wrapWithFeedback,
setToast,
}}
>
{/*==== Loader full screen*/}
<LoaderFullScreen visible={loadingFullScreen} />
{/*==== Toasts*/}
{toast && toast.mode === "normal" && toast.title ? (
<NormalToast
onPress={tapToClear}
title={toast.title}
message={toast.message}
type={toast.type}
topOffset={
toast.topOffset ? toast.topOffset : Platform.OS !== "web" ? 55 : 24
}
/>
) : null}
{/* Mini Toast */}
{toast && toast.mode === "mini" && toast.message && (
<MiniToast
onPress={tapToClear}
message={toast.message || ""}
{...toastRestProps}
/>
)}
{/*==== Page content*/}
{children}
</FeedbacksContext.Provider>
);
};
export const useFeedbacks = () => useContext(FeedbacksContext);