aboutsummaryrefslogtreecommitdiffstats
path: root/hooks/usePopup.js
diff options
context:
space:
mode:
Diffstat (limited to 'hooks/usePopup.js')
-rw-r--r--hooks/usePopup.js39
1 files changed, 39 insertions, 0 deletions
diff --git a/hooks/usePopup.js b/hooks/usePopup.js
new file mode 100644
index 0000000..615d403
--- /dev/null
+++ b/hooks/usePopup.js
@@ -0,0 +1,39 @@
+import React, { createContext, useState, useContext } from 'react'
+import Queue from 'helpers/queue'
+
+const PopupContext = createContext()
+
+export const PopupProvider = ({children}) => {
+ const [popupData, setPopupData] = useState()
+
+ const setPopup = p => {
+ Queue.enqueue(
+ () =>
+ new Promise(r => {
+ if (p.time) {
+ setPopupData(p)
+ setTimeout(() => {
+ r(setPopupData());
+ }, p.time);
+ } else {
+ setPopupData({
+ ...p,
+ ...(p.yes && {yes: {label: p.yes.label, action: () => p.yes.action().then(() => r(setPopupData()))}}),
+ ...(p.no && {no: {label: p.no.label, action: () => p.no.action().then(() => {r(setPopupData())})}}),
+ })
+ }
+ })
+ );
+
+ }
+
+ return (
+ <PopupContext.Provider value={{popupData, setPopup}}>
+ {children}
+ </PopupContext.Provider>
+ )
+}
+
+const usePopup = () => useContext(PopupContext)
+
+export default usePopup