Skip to content

Commit

Permalink
move render modal from provider to app
Browse files Browse the repository at this point in the history
  • Loading branch information
Sworzen1 committed Feb 5, 2024
1 parent 1aa9989 commit 1718fc2
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 22 deletions.
20 changes: 18 additions & 2 deletions govtool/frontend/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useCallback, useEffect } from "react";
import { Route, Routes, useNavigate } from "react-router-dom";

import { ScrollToTop } from "@atoms";
import { Modal, ScrollToTop } from "@atoms";
import { PATHS } from "@consts";
import { useCardano } from "@context";
import { useCardano, useModal } from "@context";
import {
DashboardCards,
DashboardGovernanceActions,
Expand All @@ -23,6 +23,7 @@ import {
DashboardGovernanceActionsCategory,
} from "@pages";
import {
callAll,
getItemFromLocalStorage,
WALLET_LS_KEY,
removeItemFromLocalStorage,
Expand All @@ -34,6 +35,7 @@ export default function App() {
const { enable, setDRep, setIsDrepLoading } = useCardano();
const navigate = useNavigate();
const { data } = useGetDRepInfo();
const { modal, openModal, modals } = useModal();

useWalletConnectionListener();

Expand Down Expand Up @@ -118,6 +120,20 @@ export default function App() {
<Route path="*" element={<ErrorPage />} />
<Route path={PATHS.error} element={<ErrorPage />} />
</Routes>
{modals[modal.type]?.component && (
<Modal
open={Boolean(modals[modal.type].component)}
handleClose={
!modals[modal.type].preventDismiss
? callAll(modals[modal.type]?.onClose, () =>
openModal({ type: "none", state: null })
)
: undefined
}
>
{modals[modal.type]?.component ?? <></>}
</Modal>
)}
</>
);
}
7 changes: 6 additions & 1 deletion govtool/frontend/src/components/organisms/DashboardCards.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,12 @@ export const DashboardCards = () => {
} finally {
setIsRetirementLoading(false);
}
}, [buildDRepRetirementCert, buildSignSubmitConwayCertTx]);
}, [
buildDRepRetirementCert,
buildSignSubmitConwayCertTx,
isPendingTransaction,
openModal,
]);

const delegationDescription = useMemo(() => {
const correctAdaRepresentation = (
Expand Down
22 changes: 5 additions & 17 deletions govtool/frontend/src/context/modal.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createContext, useContext, useMemo, useReducer } from "react";

import { Modal, type MuiModalChildren } from "@atoms";
import { type MuiModalChildren } from "@atoms";
import {
ChooseWalletModal,
ExternalLinkModal,
Expand Down Expand Up @@ -53,7 +53,8 @@ interface ModalState<T> {
}

interface ModalContext<T> {
modal: ContextModal;
modal: ModalState<T>;
modals: Record<ModalType, ContextModal>;
state: T | null;
openModal: (modal: Optional<ModalState<T>, "state">) => void;
closeModal: () => void;
Expand All @@ -74,7 +75,8 @@ function ModalProvider<T>(props: ProviderProps) {

const value = useMemo(
() => ({
modal: modals[modal.type],
modals,
modal,
state: modal.state,
openModal,
closeModal: callAll(modals[modal.type]?.onClose, () =>
Expand All @@ -86,20 +88,6 @@ function ModalProvider<T>(props: ProviderProps) {

return (
<ModalContext.Provider value={value} {...props}>
{modals[modal.type]?.component && (
<Modal
open={Boolean(modals[modal.type].component)}
handleClose={
!modals[modal.type].preventDismiss
? callAll(modals[modal.type]?.onClose, () =>
openModal({ type: "none", state: null })
)
: undefined
}
>
{modals[modal.type]?.component ?? <></>}
</Modal>
)}
{props.children}
</ModalContext.Provider>
);
Expand Down
6 changes: 4 additions & 2 deletions govtool/frontend/src/context/wallet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ import {
VOTE_TRANSACTION_KEY,
checkIsMaintenanceOn,
} from "@utils";
import { getDRepInfo, getEpochParams, getTransactionStatus } from "@services";
import { getEpochParams, getTransactionStatus } from "@services";
import {
setLimitedDelegationInterval,
setLimitedRegistrationInterval,
Expand Down Expand Up @@ -228,8 +228,10 @@ function CardanoProvider(props: Props) {
}
return false;
}, [
registerTransaction?.transactionHash,
closeModal,
delegateTransaction?.transactionHash,
openModal,
registerTransaction?.transactionHash,
voteTransaction?.transactionHash,
]);

Expand Down

0 comments on commit 1718fc2

Please sign in to comment.