Skip to content

Commit

Permalink
refactor: rename executeOperation to processOperation and update code…
Browse files Browse the repository at this point in the history
… according to review
  • Loading branch information
Ben-Rey committed Jan 17, 2025
1 parent 5cdbe2c commit 8822b9b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
15 changes: 6 additions & 9 deletions src/lib/massa-react/hooks/useHandleOperation.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { useState } from 'react';
import { CHAIN_ID, Operation } from '@massalabs/massa-web3';
import { ToasterMessage } from './types';
import {
executeOperation as executeOperationUtil,
OperationState,
} from '../utils/operationHandler';
import { processOperation, OperationState } from '../utils/operationHandler';

export function useHandleOperation() {
const [state, setState] = useState<OperationState>({
Expand All @@ -15,13 +12,13 @@ export function useHandleOperation() {
opId: undefined,
});

async function executeOperation(
async function handleOperation(
operation: Operation,
messages: ToasterMessage,
final = false,
): Promise<void> {
const networkInfo = await operation.provider.networkInfos();
const isMainnet = networkInfo.chainId === CHAIN_ID.Mainnet;
const { chainId } = await operation.provider.networkInfos();
const isMainnet = chainId === CHAIN_ID.Mainnet;

if (state.isOpPending) {
throw new Error('Operation is already pending');
Expand All @@ -36,7 +33,7 @@ export function useHandleOperation() {
opId: undefined,
});

await executeOperationUtil(operation, messages, final, isMainnet, setState);
await processOperation(operation, messages, final, isMainnet, setState);
}

return {
Expand All @@ -45,6 +42,6 @@ export function useHandleOperation() {
isPending: state.isPending,
isSuccess: state.isSuccess,
isError: state.isError,
executeOperation,
handleOperation,
};
}
4 changes: 2 additions & 2 deletions src/lib/massa-react/hooks/useWriteSmartContract.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useState } from 'react';
import { executeOperation } from '../utils/operationHandler';
import { processOperation } from '../utils/operationHandler';
import { Provider } from '@massalabs/massa-web3';
import { ToasterMessage } from './types';

Expand Down Expand Up @@ -51,7 +51,7 @@ export function useWriteSmartContract(account: Provider, isMainnet = false) {
fee,
});

await executeOperation(operation, messages, final, isMainnet, setState);
await processOperation(operation, messages, final, isMainnet, setState);
} catch (error) {
setState((prev) => ({ ...prev, isOpPending: false, isPending: false }));
throw error;
Expand Down
23 changes: 13 additions & 10 deletions src/lib/massa-react/utils/operationHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,15 @@ export type OperationState = {
opId?: string;
};

export async function executeOperation(
export async function processOperation(
operation: Operation,
messages: ToasterMessage,
final: boolean,
isMainnet: boolean | undefined,
setState: React.Dispatch<React.SetStateAction<OperationState>>,
): Promise<void> {
try {
setState((prev) => ({
...prev,
opId: operation.id,
}));
updateOpState(setState, { opId: operation.id });

const loadingToastId = showToast(
'loading',
Expand Down Expand Up @@ -53,11 +50,7 @@ export async function executeOperation(
}
throw error;
} finally {
setState((prev) => ({
...prev,
isOpPending: false,
isPending: false,
}));
updateOpState(setState, { isOpPending: false, isPending: false });
}
}

Expand Down Expand Up @@ -97,3 +90,13 @@ function handleOperationError(
setState((prev) => ({ ...prev, isError: true }));
showToast('error', message, opId);
}

function updateOpState<State>(
setState: React.Dispatch<React.SetStateAction<State>>,
partialState: Partial<State>,
) {
setState((prevState) => ({
...prevState,
...partialState,
}));
}

0 comments on commit 8822b9b

Please sign in to comment.