diff --git a/nym-wallet/src/pages/unbond/index.tsx b/nym-wallet/src/pages/unbond/index.tsx index a8755fc516..33b6765e03 100644 --- a/nym-wallet/src/pages/unbond/index.tsx +++ b/nym-wallet/src/pages/unbond/index.tsx @@ -1,87 +1,191 @@ import React, { useContext, useEffect, useState } from 'react'; -import { Alert, Box, Button, CircularProgress } from '@mui/material'; -import { useSnackbar } from 'notistack'; -import { Fee, NymCard } from '../../components'; +import { Alert, Button } from '@mui/material'; +import { FeeDetails } from '@nymproject/types'; +import { useNavigate } from 'react-router-dom'; +import { LoadingModal } from 'src/components/Modals/LoadingModal'; +import { AppContext, urls } from 'src/context'; +import { unbondGateway, unbondMixNode, vestingUnbondGateway, vestingUnbondMixnode } from 'src/requests'; +import { EnumNodeType } from 'src/types'; +import { NymCard } from '../../components'; import { useCheckOwnership } from '../../hooks/useCheckOwnership'; -import { AppContext } from '../../context/main'; -import { unbond, vestingUnbond } from '../../requests'; import { PageLayout } from '../../layouts'; +import { ConfirmationModal } from './ConfirmationModal'; +import { UnbondGateway } from './UnbondGateway'; +import { UnbondMixnode } from './UnbondMixnode'; export const Unbond = () => { - const [isLoading, setIsLoading] = useState(false); + const { network } = useContext(AppContext); const { checkOwnership, ownership } = useCheckOwnership(); - const { userBalance, getBondDetails } = useContext(AppContext); - const { enqueueSnackbar } = useSnackbar(); + const [isLoading, setIsLoading] = useState(false); + const [confirmationDetails, setConfirmationDetails] = useState< + { success: boolean; txUrl?: string; message?: string } | undefined + >(); + const navigate = useNavigate(); useEffect(() => { const initialiseForm = async () => { await checkOwnership(); }; initialiseForm(); - }, [ownership.hasOwnership, checkOwnership]); + }, [checkOwnership]); + + const handleUnbondMixnode = async (isWithVestingTokens: boolean, fee: FeeDetails) => { + let tx; + setIsLoading(true); + try { + if (isWithVestingTokens) tx = await vestingUnbondMixnode(fee?.fee); + if (!isWithVestingTokens) tx = await unbondMixNode(fee?.fee); + setConfirmationDetails({ + success: true, + txUrl: `${urls(network).blockExplorer}/transaction/${tx?.transaction_hash}`, + }); + } catch (err) { + setConfirmationDetails({ + success: false, + message: err as string, + }); + } finally { + await checkOwnership(); + setIsLoading(false); + } + }; + + const handleUnbondGateway = async (isWithVestingTokens: boolean, fee: FeeDetails) => { + let tx; + setIsLoading(true); + try { + if (isWithVestingTokens) tx = await vestingUnbondGateway(fee?.fee); + if (!isWithVestingTokens) tx = await unbondGateway(fee?.fee); + setConfirmationDetails({ + success: true, + txUrl: `${urls(network).blockExplorer}/transaction/${tx?.transaction_hash}`, + }); + } catch (err) { + setConfirmationDetails({ + success: false, + message: err as string, + }); + } finally { + await checkOwnership(); + setIsLoading(false); + } + }; return ( - {ownership?.hasOwnership ? ( - <> - { - setIsLoading(true); - try { - if (ownership.vestingPledge) { - await vestingUnbond(ownership.nodeType!); - } else { - await unbond(ownership.nodeType!); - } - } catch (e) { - enqueueSnackbar(`Failed to unbond ${ownership.nodeType}}`, { variant: 'error' }); - } finally { - await getBondDetails(); - await checkOwnership(); - await userBalance.fetchBalance(); - setIsLoading(false); - } - }} - color="inherit" - > - Unbond - - } - sx={{ m: 2 }} - > - {`Looks like you already have a ${ownership.nodeType} bonded.`} - - - - - - - ) : ( - - You do not currently have a bonded node + {!ownership.hasOwnership && ( + navigate('/bond')}> + Bond + + } + > + You do not currently have a bonded mixnode or gateway )} - {isLoading && ( - - - + {ownership.hasOwnership && ownership?.nodeType === EnumNodeType.mixnode && ( + + setConfirmationDetails({ + success: false, + message: err as string, + }) + } + /> + )} + {ownership.hasOwnership && ownership?.nodeType === EnumNodeType.gateway && ( + + setConfirmationDetails({ + success: false, + message: err as string, + }) + } + /> + )} + {isLoading && } + {confirmationDetails && ( + setConfirmationDetails(undefined)} /> )} ); }; + +// {fee && { +// setIsLoading(true); +// try { +// if (ownership.vestingPledge) { +// await vestingUnbond(ownership.nodeType!); +// } else { +// await unbond(ownership.nodeType!); +// } +// } catch (e) { +// enqueueSnackbar(`Failed to unbond ${ownership.nodeType}}`, { variant: 'error' }); +// } finally { +// await getBondDetails(); +// await checkOwnership(); +// await userBalance.fetchBalance(); +// setIsLoading(false); +// } +// }}/>} +// +// {ownership?.hasOwnership ? ( +// <> +// { +// setIsLoading(true) +// if (ownership.vestingPledge) { +// await getFee(simulateVestingUnbondMixnode); +// } else { +// await getFee(unbond, {type: ownership.nodeType}) +// } +// }} +// color="inherit" +// > +// Unbond +// +// } +// sx={{ m: 2 }} +// > +// {`Looks like you already have a ${ownership.nodeType} bonded.`} +// + +// +// +// +// +// ) : ( +// +// You do not currently have a bonded node +// +// )} +// {isLoading && ( +// +// +// +// )} +//