diff --git a/frontend/src/index.tsx b/frontend/src/index.tsx index af3f59a00..45ba861c3 100644 --- a/frontend/src/index.tsx +++ b/frontend/src/index.tsx @@ -6,6 +6,7 @@ import ReactDOM from 'react-dom/client'; import { RecoilRoot } from 'recoil'; import App from './App'; +import ModalProvider from './providers/ModalProvider/ModalProvider'; import ToastProvider from './providers/ToastProvider/ToastProvider'; import GlobalStyle from './styles/GlobalStyle'; import { Theme } from './styles/Theme'; @@ -34,9 +35,11 @@ enableMocking().then(() => { - - - + + + + + diff --git a/frontend/src/providers/ModalProvider/ModalProvider.tsx b/frontend/src/providers/ModalProvider/ModalProvider.tsx new file mode 100644 index 000000000..c295295c2 --- /dev/null +++ b/frontend/src/providers/ModalProvider/ModalProvider.tsx @@ -0,0 +1,60 @@ +import { createContext, PropsWithChildren, useMemo, useState } from 'react'; + +interface ModalContent { + title: string; + message: string; +} + +interface ModalState extends ModalContent { + isOpen: boolean; + onClose: () => void; +} + +interface Modal extends ModalContent { + Component: React.FC | null; + isOpen: boolean; +} + +interface ModalDispatchContextProps { + show: (Component: React.FC | null, { title, message }: ModalContent) => void; + close: () => void; +} + +export const ModalDispatchContext = createContext(null); + +const ModalProvider = ({ children }: PropsWithChildren) => { + const [modal, setModal] = useState({ + Component: null, + isOpen: false, + title: '', + message: '', + }); + + const show = (Component: React.FC | null, { title, message }: ModalContent) => { + setModal({ + Component, + title, + message, + isOpen: true, + }); + }; + + const close = () => { + setModal((prev) => ({ + ...prev, + Component: null, + isOpen: false, + })); + }; + + const dispatch = useMemo(() => ({ show, close }), []); + + return ( + + {children} + {modal.isOpen && modal.Component && } + + ); +}; + +export default ModalProvider; diff --git a/frontend/src/utils/test-utils.tsx b/frontend/src/utils/test-utils.tsx index 394b660a1..d8345bef3 100644 --- a/frontend/src/utils/test-utils.tsx +++ b/frontend/src/utils/test-utils.tsx @@ -10,6 +10,7 @@ import type { MutableSnapshot } from 'recoil'; import AsyncErrorBoundary from '@/components/common/ErrorBoundary/AsyncErrorBoundary'; import RootErrorBoundary from '@/components/common/ErrorBoundary/RootErrorBoundary'; import Spinner from '@/components/common/Spinner/Spinner'; +import ModalProvider from '@/providers/ModalProvider/ModalProvider'; import ToastProvider from '@/providers/ToastProvider/ToastProvider'; import GlobalStyle from '@/styles/GlobalStyle'; import { Theme } from '@/styles/Theme'; @@ -38,7 +39,9 @@ const wrapper = ({ - {children} + + {children} +