Skip to content

Instantly share code, notes, and snippets.

@kitze
Created April 15, 2021 19:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kitze/16df977304d52b23cce1f910ef2b750f to your computer and use it in GitHub Desktop.
Save kitze/16df977304d52b23cce1f910ef2b750f to your computer and use it in GitHub Desktop.
chakra-alert-manager
import { useDisclosure } from "@chakra-ui/react";
import ConfirmDialog from "app/core/chakra-prompts/ConfirmDialog";
import FormPrompt from "app/core/chakra-prompts/FormPrompt";
import React, { useContext, useState } from "react";
const ChakraPromptsContext = React.createContext<any>(null);
export const useChakraPrompts = () => useContext(ChakraPromptsContext);
const Manager = ({ children }) => {
const [config, setConfig] = useState<any>({});
const promptDisclosure = useDisclosure();
const confirmDisclosure = useDisclosure();
const closeAllPopups = () => {
promptDisclosure.onClose();
confirmDisclosure.onClose();
};
const submitPrompt = (results) => {
config.res?.(results);
closeAllPopups();
};
const close = () => {
config.res?.();
closeAllPopups();
};
const prompt = (config) =>
new Promise((res) => {
setConfig({ ...config, res });
promptDisclosure.onOpen();
});
const confirm = (config) =>
new Promise((res) => {
setConfig({ ...config, res });
confirmDisclosure.onOpen();
});
return (
<ChakraPromptsContext.Provider value={{ prompt, confirm }}>
{children}
<FormPrompt
close={close}
submit={submitPrompt}
disclosure={promptDisclosure}
config={config}
/>
<ConfirmDialog
submit={submitPrompt}
close={close}
config={config}
disclosure={confirmDisclosure}
/>
</ChakraPromptsContext.Provider>
);
};
export default Manager;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment