aboutsummaryrefslogtreecommitdiffstats
path: root/hooks
diff options
context:
space:
mode:
authorGravatar piotrruss <mail@pruss.it> 2021-08-22 14:33:54 +0200
committerGravatar piotrruss <mail@pruss.it> 2021-08-29 15:35:49 +0200
commit9f74c550927671f4ded301d0cf3e9d592716375c (patch)
tree6075bead5939bfb9c3b6137fc5ef865f088b57cb /hooks
parent71cc09db93ec9b079a30593e14ca57c98fdc94ac (diff)
downloadmy_apps-9f74c550927671f4ded301d0cf3e9d592716375c.tar.gz
my_apps-9f74c550927671f4ded301d0cf3e9d592716375c.tar.bz2
my_apps-9f74c550927671f4ded301d0cf3e9d592716375c.zip
settings
Diffstat (limited to 'hooks')
-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