From 9f74c550927671f4ded301d0cf3e9d592716375c Mon Sep 17 00:00:00 2001 From: piotrruss Date: Sun, 22 Aug 2021 14:33:54 +0200 Subject: settings --- hooks/usePopup.js | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 hooks/usePopup.js (limited to 'hooks') 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 ( + + {children} + + ) +} + +const usePopup = () => useContext(PopupContext) + +export default usePopup -- cgit v1.2.3