Skip to content

Commit

Permalink
refactor: replace setState calls with updateOpState
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben-Rey committed Jan 17, 2025
1 parent 8822b9b commit b7d276a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
9 changes: 6 additions & 3 deletions src/lib/massa-react/hooks/useHandleOperation.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { useState } from 'react';
import { CHAIN_ID, Operation } from '@massalabs/massa-web3';
import { ToasterMessage } from './types';
import { processOperation, OperationState } from '../utils/operationHandler';
import {
processOperation,
OperationState,
updateOpState,
} from '../utils/operationHandler';

export function useHandleOperation() {
const [state, setState] = useState<OperationState>({
Expand All @@ -24,8 +28,7 @@ export function useHandleOperation() {
throw new Error('Operation is already pending');
}

setState({
...state,
updateOpState(setState, {
isOpPending: true,
isPending: true,
isSuccess: false,
Expand Down
7 changes: 3 additions & 4 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 { processOperation } from '../utils/operationHandler';
import { processOperation, updateOpState } from '../utils/operationHandler';
import { Provider } from '@massalabs/massa-web3';
import { ToasterMessage } from './types';

Expand Down Expand Up @@ -33,8 +33,7 @@ export function useWriteSmartContract(account: Provider, isMainnet = false) {
throw new Error('Operation is already pending');
}

setState({
...state,
updateOpState(setState, {
isOpPending: true,
isPending: true,
isSuccess: false,
Expand All @@ -53,7 +52,7 @@ export function useWriteSmartContract(account: Provider, isMainnet = false) {

await processOperation(operation, messages, final, isMainnet, setState);
} catch (error) {
setState((prev) => ({ ...prev, isOpPending: false, isPending: false }));
updateOpState(setState, { isOpPending: false, isPending: false });
throw error;
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/lib/massa-react/utils/operationHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ function handleOperationTimeout(
opId: string,
setState: React.Dispatch<React.SetStateAction<OperationState>>,
): void {
setState((prev: OperationState) => ({ ...prev, isError: true }));
updateOpState(setState, { isError: true });
showToast('error', message, opId);
}

Expand All @@ -76,7 +76,7 @@ function handleOperationSuccess(
opId: string,
setState: React.Dispatch<React.SetStateAction<OperationState>>,
): void {
setState((prev) => ({ ...prev, isSuccess: true }));
updateOpState(setState, { isSuccess: true });
showToast('success', message, opId);
}

Expand All @@ -87,11 +87,11 @@ function handleOperationError(
setState: React.Dispatch<React.SetStateAction<OperationState>>,
): void {
console.error(error);
setState((prev) => ({ ...prev, isError: true }));
updateOpState(setState, { isError: true });
showToast('error', message, opId);
}

function updateOpState<State>(
export function updateOpState<State>(
setState: React.Dispatch<React.SetStateAction<State>>,
partialState: Partial<State>,
) {
Expand Down

0 comments on commit b7d276a

Please sign in to comment.