Skip to content

Commit

Permalink
fix: Refetch UTXOs on unsuccessful staking (#64)
Browse files Browse the repository at this point in the history
* no cancel

* proper noCancel pass

* partly reset
  • Loading branch information
gbarkhatov authored Aug 12, 2024
1 parent 4739984 commit 0632a17
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 21 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ name: ci
on:
pull_request:
branches:
- '**'
- "**"

jobs:
lint_test:
uses: babylonlabs-io/.github/.github/workflows/reusable_node_lint_test.yml@v0.3.0
with:
run-build: true
run-unit-tests: true
run-unit-tests: true
8 changes: 4 additions & 4 deletions .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ name: docker_publish
on:
push:
branches:
- 'main'
- 'dev'
- "main"
- "dev"
tags:
- '*'
- "*"

jobs:
lint_test:
uses: babylonlabs-io/.github/.github/workflows/reusable_node_lint_test.yml@v0.3.0
with:
run-build: true
run-unit-tests: true

docker_build:
needs: [lint_test]
runs-on: ubuntu-22.04
Expand Down
18 changes: 11 additions & 7 deletions src/app/components/Modals/ErrorModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ interface ErrorModalProps {
errorMessage: string;
errorState?: ErrorState;
errorTime: Date;
noCancel?: boolean;
}

export const ErrorModal: React.FC<ErrorModalProps> = ({
Expand All @@ -20,7 +21,8 @@ export const ErrorModal: React.FC<ErrorModalProps> = ({
onRetry,
errorMessage,
errorState,
errorTime,
noCancel,
// errorTime, // This prop is not used in the component
}) => {
const { error, retryErrorAction } = useError();

Expand Down Expand Up @@ -93,12 +95,14 @@ export const ErrorModal: React.FC<ErrorModalProps> = ({
<p className="text-center">{getErrorMessage()}</p>
</div>
<div className="mt-4 flex justify-around gap-4">
<button
className="btn btn-outline flex-1 rounded-lg px-2"
onClick={() => onClose()}
>
Cancel
</button>
{!noCancel && ( // Only show the cancel button if noCancel is false or undefined
<button
className="btn btn-outline flex-1 rounded-lg px-2"
onClick={() => onClose()}
>
Cancel
</button>
)}
{onRetry && (
<button
className="btn-primary btn flex-1 rounded-lg px-2 text-white"
Expand Down
11 changes: 10 additions & 1 deletion src/app/components/Staking/Staking.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,16 @@ export const Staking: React.FC<StakingProps> = ({
errorState: ErrorState.STAKING,
errorTime: new Date(),
},
retryAction: handleSign,
noCancel: true,
retryAction: () => {
// in case of error, we need to partly reset the state
setStakingAmountSat(0);
setSelectedFeeRate(0);
setPreviewModalOpen(false);
setResetFormInputs(!resetFormInputs);
// and refetch the UTXOs
queryClient.invalidateQueries({ queryKey: [UTXO_KEY, address] });
},
});
}
};
Expand Down
18 changes: 13 additions & 5 deletions src/app/context/Error/ErrorContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ interface ErrorContextType {
retryErrorAction?: () => void;
showError: (showErrorParams: ShowErrorParams) => void;
hideError: () => void;
noCancel?: boolean;
}

export const ErrorProvider: React.FC<ErrorProviderProps> = ({ children }) => {
const [isErrorOpen, setIsErrorOpen] = useState(false);
const [isNoCancel, setIsNoCancel] = useState(false);
const [error, setError] = useState<ErrorType>({
message: "",
errorTime: new Date(),
Expand All @@ -33,11 +35,15 @@ export const ErrorProvider: React.FC<ErrorProviderProps> = ({ children }) => {
(() => void) | undefined
>();

const showError = useCallback(({ error, retryAction }: ShowErrorParams) => {
setError(error);
setIsErrorOpen(true);
setRetryErrorAction(() => retryAction);
}, []);
const showError = useCallback(
({ error, retryAction, noCancel }: ShowErrorParams) => {
setError(error);
setIsErrorOpen(true);
setIsNoCancel(noCancel ?? false);
setRetryErrorAction(() => retryAction);
},
[],
);

const hideError = useCallback(() => {
setIsErrorOpen(false);
Expand All @@ -48,6 +54,7 @@ export const ErrorProvider: React.FC<ErrorProviderProps> = ({ children }) => {
errorState: undefined,
});
setRetryErrorAction(undefined);
setIsNoCancel(false);
}, 300);
}, []);

Expand All @@ -57,6 +64,7 @@ export const ErrorProvider: React.FC<ErrorProviderProps> = ({ children }) => {
showError,
hideError,
retryErrorAction,
noCancel: isNoCancel,
};

return (
Expand Down
11 changes: 9 additions & 2 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,14 @@ const Home: React.FC<HomeProps> = () => {
const [publicKeyNoCoord, setPublicKeyNoCoord] = useState("");

const [address, setAddress] = useState("");
const { error, isErrorOpen, showError, hideError, retryErrorAction } =
useError();
const {
error,
isErrorOpen,
showError,
hideError,
retryErrorAction,
noCancel,
} = useError();
const { isTermsOpen, closeTerms } = useTerms();

const {
Expand Down Expand Up @@ -470,6 +476,7 @@ const Home: React.FC<HomeProps> = () => {
errorTime={error.errorTime}
onClose={hideError}
onRetry={retryErrorAction}
noCancel={noCancel}
/>
<TermsModal open={isTermsOpen} onClose={closeTerms} />
</main>
Expand Down
1 change: 1 addition & 0 deletions src/app/types/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ export interface ErrorHandlerParam {
export interface ShowErrorParams {
error: ErrorType;
retryAction?: () => void;
noCancel?: boolean;
}

0 comments on commit 0632a17

Please sign in to comment.