diff options
author | 2021-08-22 14:33:54 +0200 | |
---|---|---|
committer | 2021-08-29 15:35:49 +0200 | |
commit | 9f74c550927671f4ded301d0cf3e9d592716375c (patch) | |
tree | 6075bead5939bfb9c3b6137fc5ef865f088b57cb /hooks/usePopup.js | |
parent | 71cc09db93ec9b079a30593e14ca57c98fdc94ac (diff) | |
download | my_apps-9f74c550927671f4ded301d0cf3e9d592716375c.tar.gz my_apps-9f74c550927671f4ded301d0cf3e9d592716375c.tar.bz2 my_apps-9f74c550927671f4ded301d0cf3e9d592716375c.zip |
settings
Diffstat (limited to 'hooks/usePopup.js')
-rw-r--r-- | hooks/usePopup.js | 39 |
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 |