From 28db987e71272f2077045707e3a5a75e11f15825 Mon Sep 17 00:00:00 2001 From: Leonardo Zizzamia Date: Mon, 12 Aug 2024 15:45:52 -0700 Subject: [PATCH] dope --- .../components/TransactionProvider.tsx | 18 +++++++++++++++--- src/transaction/types.ts | 10 +++++++++- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/transaction/components/TransactionProvider.tsx b/src/transaction/components/TransactionProvider.tsx index d4f5b03312..38704be84f 100644 --- a/src/transaction/components/TransactionProvider.tsx +++ b/src/transaction/components/TransactionProvider.tsx @@ -26,7 +26,7 @@ import { useCallsStatus } from '../hooks/useCallsStatus'; import { useWriteContract } from '../hooks/useWriteContract'; import { useWriteContracts } from '../hooks/useWriteContracts'; import type { - LifeCycleStateName, + LifeCycleState, TransactionContextType, TransactionProviderReact, } from '../types'; @@ -60,9 +60,11 @@ export function TransactionProvider({ const config = useConfig(); const [errorMessage, setErrorMessage] = useState(''); const [isToastVisible, setIsToastVisible] = useState(false); + const [lifeCycleState, setLifeCycleState] = useState({ + stateName: 'init', + stateData: {}, + }); // Component lifecycle const [receiptArray, setReceiptArray] = useState([]); - const [stateName, setStateName] = useState('init'); // Components lifecycle state name - const [stateData, setStateData] = useState({}); // Components lifecycle state data const [transactionId, setTransactionId] = useState(''); const [transactionHashArray, setTransactionHashArray] = useState( [], @@ -94,6 +96,16 @@ export function TransactionProvider({ hash: writeContractTransactionHash || transactionHash, }); + // Component lifecycle emitters + useEffect(() => { + // Emit Error + if (lifeCycleState.stateName === 'error') { + onError?.(lifeCycleState.stateData); + } + // Emit State + onState?.(lifeCycleState.stateName, lifeCycleState.stateData); + }, [onState, lifeCycleState.stateData, lifeCycleState.stateName]); + const getTransactionReceipts = useCallback(async () => { const receipts = []; for (const hash of transactionHashArray) { diff --git a/src/transaction/types.ts b/src/transaction/types.ts index 2023f32913..e003c6ce6a 100644 --- a/src/transaction/types.ts +++ b/src/transaction/types.ts @@ -9,7 +9,15 @@ import type { /** * Note: exported as public Type */ -export type LifeCycleStateName = 'init'; +export type LifeCycleState = + | { + stateName: 'init'; + stateData: {}; + } + | { + stateName: 'error'; + stateData: TransactionError; + }; export type IsSpinnerDisplayedProps = { errorMessage?: string;