Skip to content

Commit

Permalink
dope
Browse files Browse the repository at this point in the history
  • Loading branch information
Zizzamia committed Aug 13, 2024
1 parent 880827f commit c38368d
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 72 deletions.
4 changes: 2 additions & 2 deletions src/transaction/components/Transaction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export function Transaction({
children,
contracts,
onError,
onState,
onStatus,
onSuccess,
}: TransactionReact) {
return (
Expand All @@ -20,7 +20,7 @@ export function Transaction({
chainId={chainId}
contracts={contracts}
onError={onError}
onState={onState}
onStatus={onStatus}
onSuccess={onSuccess}
>
<div className={cn(className, 'flex w-full flex-col gap-2')}>
Expand Down
30 changes: 15 additions & 15 deletions src/transaction/components/TransactionProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { useCallsStatus } from '../hooks/useCallsStatus';
import { useWriteContract } from '../hooks/useWriteContract';
import { useWriteContracts } from '../hooks/useWriteContracts';
import type {
LifeCycleState,
LifeCycleStatus,
TransactionContextType,
TransactionProviderReact,
} from '../types';
Expand All @@ -52,17 +52,17 @@ export function TransactionProvider({
children,
contracts,
onError,
onState,
onStatus,
onSuccess,
}: TransactionProviderReact) {
// Core Hooks
const account = useAccount();
const config = useConfig();
const [errorMessage, setErrorMessage] = useState('');
const [isToastVisible, setIsToastVisible] = useState(false);
const [lifeCycleState, setLifeCycleState] = useState<LifeCycleState>({
stateName: 'init',
stateData: null,
const [lifeCycleStatus, setLifeCycleStatus] = useState<LifeCycleStatus>({
statusName: 'init',
statusData: null,
}); // Component lifecycle
const [receiptArray, setReceiptArray] = useState<TransactionReceipt[]>([]);
const [transactionId, setTransactionId] = useState('');
Expand All @@ -75,7 +75,7 @@ export function TransactionProvider({
const { status: statusWriteContracts, writeContractsAsync } =
useWriteContracts({
setErrorMessage,
setLifeCycleState,
setLifeCycleStatus,
setTransactionId,
});
const {
Expand All @@ -84,12 +84,12 @@ export function TransactionProvider({
data: writeContractTransactionHash,
} = useWriteContract({
setErrorMessage,
setLifeCycleState,
setLifeCycleStatus,
setTransactionHashArray,
transactionHashArray,
});
const { transactionHash, status: callStatus } = useCallsStatus({
setLifeCycleState,
setLifeCycleStatus,
transactionId,
});
const { data: receipt } = useWaitForTransactionReceipt({
Expand All @@ -99,17 +99,17 @@ export function TransactionProvider({
// Component lifecycle emitters
useEffect(() => {
// Emit Error
if (lifeCycleState.stateName === 'error') {
onError?.(lifeCycleState.stateData);
if (lifeCycleStatus.statusName === 'error') {
onError?.(lifeCycleStatus.statusData);
}
// Emit State
onState?.(lifeCycleState);
onStatus?.(lifeCycleStatus);
}, [
onError,
onState,
lifeCycleState,
lifeCycleState.stateData, // Keep stateData, so that the effect runs when it changes
lifeCycleState.stateName, // Keep stateName, so that the effect runs when it changes
onStatus,
lifeCycleStatus,
lifeCycleStatus.statusData, // Keep statusData, so that the effect runs when it changes
lifeCycleStatus.statusName, // Keep statusName, so that the effect runs when it changes
]);

const getTransactionReceipts = useCallback(async () => {
Expand Down
10 changes: 5 additions & 5 deletions src/transaction/hooks/useCallsStatus.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,22 @@ describe('useCallsStatus', () => {
});

it('should handle errors and call onError callback', () => {
const mockSetLifeCycleState = vi.fn();
const mockSetLifeCycleStatus = vi.fn();
const mockError = new Error('Test error');
(useCallsStatusWagmi as ReturnType<typeof vi.fn>).mockImplementation(() => {
throw mockError;
});
const { result } = renderHook(() =>
useCallsStatus({
setLifeCycleState: mockSetLifeCycleState,
setLifeCycleStatus: mockSetLifeCycleStatus,
transactionId,
}),
);
expect(result.current.status).toBe('error');
expect(result.current.transactionHash).toBeUndefined();
expect(mockSetLifeCycleState).toHaveBeenCalledWith({
stateName: 'error',
stateData: {
expect(mockSetLifeCycleStatus).toHaveBeenCalledWith({
statusName: 'error',
statusData: {
code: 'UNCAUGHT_CALL_STATUS_ERROR',
error: JSON.stringify(mockError),
},
Expand Down
8 changes: 4 additions & 4 deletions src/transaction/hooks/useCallsStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { UseCallsStatusParams } from '../types';
const uncaughtErrorCode = 'UNCAUGHT_CALL_STATUS_ERROR';

export function useCallsStatus({
setLifeCycleState,
setLifeCycleStatus,
transactionId,
}: UseCallsStatusParams) {
try {
Expand All @@ -20,9 +20,9 @@ export function useCallsStatus({
const transactionHash = data?.receipts?.[0]?.transactionHash;
return { status: data?.status, transactionHash };
} catch (err) {
setLifeCycleState({
stateName: 'error',
stateData: { code: uncaughtErrorCode, error: JSON.stringify(err) },
setLifeCycleStatus({
statusName: 'error',
statusData: { code: uncaughtErrorCode, error: JSON.stringify(err) },
});
return { status: 'error', transactionHash: undefined };
}
Expand Down
22 changes: 11 additions & 11 deletions src/transaction/hooks/useWriteContract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type MockUseWriteContractReturn = {

describe('useWriteContract', () => {
const mockSetErrorMessage = vi.fn();
const mockSetLifeCycleState = vi.fn();
const mockSetLifeCycleStatus = vi.fn();
const mockSetTransactionHashArray = vi.fn();

beforeEach(() => {
Expand All @@ -40,7 +40,7 @@ describe('useWriteContract', () => {
const { result } = renderHook(() =>
useWriteContract({
setErrorMessage: mockSetErrorMessage,
setLifeCycleState: mockSetLifeCycleState,
setLifeCycleStatus: mockSetLifeCycleStatus,
setTransactionHashArray: mockSetTransactionHashArray,
}),
);
Expand All @@ -65,7 +65,7 @@ describe('useWriteContract', () => {
renderHook(() =>
useWriteContract({
setErrorMessage: mockSetErrorMessage,
setLifeCycleState: mockSetLifeCycleState,
setLifeCycleStatus: mockSetLifeCycleStatus,
setTransactionHashArray: mockSetTransactionHashArray,
}),
);
Expand All @@ -76,9 +76,9 @@ describe('useWriteContract', () => {
expect(mockSetErrorMessage).toHaveBeenCalledWith(
'Something went wrong. Please try again.',
);
expect(mockSetLifeCycleState).toHaveBeenCalledWith({
stateName: 'error',
stateData: {
expect(mockSetLifeCycleStatus).toHaveBeenCalledWith({
statusName: 'error',
statusData: {
code: 'WRITE_CONTRACT_ERROR',
error: 'Something went wrong. Please try again.',
},
Expand All @@ -101,7 +101,7 @@ describe('useWriteContract', () => {
renderHook(() =>
useWriteContract({
setErrorMessage: mockSetErrorMessage,
setLifeCycleState: mockSetLifeCycleState,
setLifeCycleStatus: mockSetLifeCycleStatus,
setTransactionHashArray: mockSetTransactionHashArray,
}),
);
Expand All @@ -120,7 +120,7 @@ describe('useWriteContract', () => {
const { result } = renderHook(() =>
useWriteContract({
setErrorMessage: mockSetErrorMessage,
setLifeCycleState: mockSetLifeCycleState,
setLifeCycleStatus: mockSetLifeCycleStatus,
setTransactionHashArray: mockSetTransactionHashArray,
}),
);
Expand All @@ -129,9 +129,9 @@ describe('useWriteContract', () => {
expect(mockSetErrorMessage).toHaveBeenCalledWith(
'Something went wrong. Please try again.',
);
expect(mockSetLifeCycleState).toHaveBeenCalledWith({
stateName: 'error',
stateData: {
expect(mockSetLifeCycleStatus).toHaveBeenCalledWith({
statusName: 'error',
statusData: {
code: 'UNCAUGHT_WRITE_CONTRACT_ERROR',
error: JSON.stringify(uncaughtError),
},
Expand Down
14 changes: 7 additions & 7 deletions src/transaction/hooks/useWriteContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import type { UseWriteContractParams } from '../types';
*/
export function useWriteContract({
setErrorMessage,
setLifeCycleState,
setLifeCycleStatus,
setTransactionHashArray,
transactionHashArray,
}: UseWriteContractParams) {
Expand All @@ -30,9 +30,9 @@ export function useWriteContract({
} else {
setErrorMessage(GENERIC_ERROR_MESSAGE);
}
setLifeCycleState({
stateName: 'error',
stateData: { code: WRITE_CONTRACT_ERROR_CODE, error: e.message },
setLifeCycleStatus({
statusName: 'error',
statusData: { code: WRITE_CONTRACT_ERROR_CODE, error: e.message },
});
},
onSuccess: (hash: Address) => {
Expand All @@ -44,9 +44,9 @@ export function useWriteContract({
});
return { status, writeContractAsync, data };
} catch (err) {
setLifeCycleState({
stateName: 'error',
stateData: {
setLifeCycleStatus({
statusName: 'error',
statusData: {
code: UNCAUGHT_WRITE_CONTRACT_ERROR_CODE,
error: JSON.stringify(err),
},
Expand Down
20 changes: 10 additions & 10 deletions src/transaction/hooks/useWriteContracts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ vi.mock('wagmi/experimental', () => ({

describe('useWriteContracts', () => {
const mockSetErrorMessage = vi.fn();
const mockSetLifeCycleState = vi.fn();
const mockSetLifeCycleStatus = vi.fn();
const mockSetTransactionId = vi.fn();

beforeEach(() => {
Expand All @@ -31,7 +31,7 @@ describe('useWriteContracts', () => {
renderHook(() =>
useWriteContracts({
setErrorMessage: mockSetErrorMessage,
setLifeCycleState: mockSetLifeCycleState,
setLifeCycleStatus: mockSetLifeCycleStatus,
setTransactionId: mockSetTransactionId,
}),
);
Expand All @@ -40,9 +40,9 @@ describe('useWriteContracts', () => {
expect(mockSetErrorMessage).toHaveBeenCalledWith(
'Something went wrong. Please try again.',
);
expect(mockSetLifeCycleState).toHaveBeenCalledWith({
stateName: 'error',
stateData: {
expect(mockSetLifeCycleStatus).toHaveBeenCalledWith({
statusName: 'error',
statusData: {
code: 'WRITE_CONTRACTS_ERROR',
error: 'Something went wrong. Please try again.',
},
Expand All @@ -64,7 +64,7 @@ describe('useWriteContracts', () => {
renderHook(() =>
useWriteContracts({
setErrorMessage: mockSetErrorMessage,
setLifeCycleState: mockSetLifeCycleState,
setLifeCycleStatus: mockSetLifeCycleStatus,
setTransactionId: mockSetTransactionId,
}),
);
Expand All @@ -83,7 +83,7 @@ describe('useWriteContracts', () => {
const { result } = renderHook(() =>
useWriteContracts({
setErrorMessage: mockSetErrorMessage,
setLifeCycleState: mockSetLifeCycleState,
setLifeCycleStatus: mockSetLifeCycleStatus,
setTransactionId: mockSetTransactionId,
}),
);
Expand All @@ -92,9 +92,9 @@ describe('useWriteContracts', () => {
expect(mockSetErrorMessage).toHaveBeenCalledWith(
'Something went wrong. Please try again.',
);
expect(mockSetLifeCycleState).toHaveBeenCalledWith({
stateName: 'error',
stateData: {
expect(mockSetLifeCycleStatus).toHaveBeenCalledWith({
statusName: 'error',
statusData: {
code: 'UNCAUGHT_WRITE_WRITE_CONTRACTS_ERROR',
error: JSON.stringify(uncaughtError),
},
Expand Down
14 changes: 7 additions & 7 deletions src/transaction/hooks/useWriteContracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import type { UseWriteContractsParams } from '../types';
*/
export function useWriteContracts({
setErrorMessage,
setLifeCycleState,
setLifeCycleStatus,
setTransactionId,
}: UseWriteContractsParams) {
try {
Expand All @@ -39,9 +39,9 @@ export function useWriteContracts({
} else {
setErrorMessage(GENERIC_ERROR_MESSAGE);
}
setLifeCycleState({
stateName: 'error',
stateData: { code: WRITE_CONTRACTS_ERROR_CODE, error: e.message },
setLifeCycleStatus({
statusName: 'error',
statusData: { code: WRITE_CONTRACTS_ERROR_CODE, error: e.message },
});
},
onSuccess: (id) => {
Expand All @@ -51,9 +51,9 @@ export function useWriteContracts({
});
return { status, writeContractsAsync };
} catch (err) {
setLifeCycleState({
stateName: 'error',
stateData: {
setLifeCycleStatus({
statusName: 'error',
statusData: {
code: UNCAUGHT_WRITE_CONTRACTS_ERROR_CODE,
error: JSON.stringify(err),
},
Expand Down
2 changes: 1 addition & 1 deletion src/transaction/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export { TransactionToastIcon } from './components/TransactionToastIcon';
export { TransactionToastAction } from './components/TransactionToastAction';
export { TransactionToastLabel } from './components/TransactionToastLabel';
export type {
LifeCycleState,
LifeCycleStatus,
TransactionButtonReact,
TransactionError,
TransactionReact,
Expand Down
Loading

0 comments on commit c38368d

Please sign in to comment.