Skip to content
This repository was archived by the owner on Jun 24, 2022. It is now read-only.

[Claim] Reject tx modals #2193

Merged
merged 5 commits into from
Jan 18, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { useState } from 'react'
import { ErrorMessageProps, SwapCallbackError } from 'components/swap/styleds'
import useTransactionErrorModal from './useTransactionErrorModal'

/**
* @description hook for getting CowSwap error and handling them visually
* @description ErrorMessage component accepts an error message to override exported error state, and a close option
* @returns returns object: { error, setError, ErrorMessage } => error message, error message setter, and our ErrorMessage component
*/
export default function useErrorMessage() {
export function useErrorMessage() {
// Any async bc errors
const [internalError, setError] = useState<string | undefined>()

const handleCloseError = () => setError(undefined)

return {
error: internalError,
setError,
handleSetError: setError,
ErrorMessage: ({
error = internalError,
showClose = false,
Expand All @@ -23,3 +23,30 @@ export default function useErrorMessage() {
error ? <SwapCallbackError showClose={showClose} handleClose={handleCloseError} error={error} {...rest} /> : null,
}
}

export function useErrorModal() {
// Any async bc errors
const [internalError, setError] = useState<string | undefined>()
const { openModal, closeModal, TransactionErrorModal } = useTransactionErrorModal()

const handleCloseError = () => {
closeModal()
setError(undefined)
}
const handleSetError = (error: string | undefined) => {
// close any open modals
// closeModal()
// set the error and IF error, open modal
setError(error)
error && openModal()
}

return {
error: internalError,
handleCloseError,
handleSetError,
ErrorModal: ({ message = internalError }: { message?: string }) => (
<TransactionErrorModal onDismiss={handleCloseError} message={message} />
),
}
}
24 changes: 24 additions & 0 deletions src/custom/hooks/useTransactionErrorModal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { useCallback } from 'react'
import { ApplicationModal } from 'state/application/reducer'
import { TransactionErrorContent } from 'components/TransactionConfirmationModal'
import { useOpenModal, useCloseModals, useModalOpen } from 'state/application/hooks'
import { GpModal } from '../components/Modal'

export default function useTransactionErrorModal() {
const openModal = useOpenModal(ApplicationModal.TRANSACTION_ERROR)
const closeModal = useCloseModals()
const showTransactionErrorModal = useModalOpen(ApplicationModal.TRANSACTION_ERROR)

return {
openModal,
closeModal,
TransactionErrorModal: useCallback(
({ message, onDismiss }: { message?: string; onDismiss: () => void }) => (
<GpModal isOpen={showTransactionErrorModal} onDismiss={closeModal}>
<TransactionErrorContent onDismiss={onDismiss} message={message} />
</GpModal>
),
[closeModal, showTransactionErrorModal]
),
}
}
26 changes: 10 additions & 16 deletions src/custom/pages/Claim/InvestmentFlow/InvestOption.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { useActiveWeb3React } from 'hooks/web3'
import { ButtonConfirmed } from 'components/Button'
import { ButtonSize } from 'theme'
import Loader from 'components/Loader'
import useErrorMessage from 'hooks/useErrorMessage'
import { useErrorModal } from 'hooks/useErrorMessageAndModal'

const RangeSteps = styled.div`
display: flex;
Expand Down Expand Up @@ -45,7 +45,7 @@ export default function InvestOption({ approveData, updateInvestAmount, claim }:

const balance = useCurrencyBalance(account || undefined, token)

const { setError, ErrorMessage } = useErrorMessage()
const { handleSetError, ErrorModal } = useErrorModal()

const handlePercentChange = (event: React.ChangeEvent<HTMLInputElement>) => {
console.log(event.target.value)
Expand All @@ -57,7 +57,7 @@ export default function InvestOption({ approveData, updateInvestAmount, claim }:

const onMaxClick = useCallback(() => {
// TODO: remove
setError('Transaction rejected. User denied signature.')
handleSetError('Transaction rejected. User denied signature.')
if (!maxCost || !balance) {
return
}
Expand All @@ -67,15 +67,15 @@ export default function InvestOption({ approveData, updateInvestAmount, claim }:
const investAmount = formatUnits(amount.quotient.toString(), balance.currency.decimals)

updateInvestAmount(claim.index, investAmount)
}, [balance, claim.index, maxCost, setError, updateInvestAmount])
}, [balance, claim.index, maxCost, handleSetError, updateInvestAmount])

// Cache approveData methods
const approveCallback = approveData?.approveCallback
const approveState = approveData?.approveState
// Save "local" approving state (pre-BC) for rendering spinners etc
const [approving, setApproving] = useState(false)
const handleApprove = useCallback(async () => {
setError(undefined)
handleSetError(undefined)
if (!approveCallback) return

try {
Expand All @@ -84,11 +84,11 @@ export default function InvestOption({ approveData, updateInvestAmount, claim }:
await approveCallback({ transactionSummary: `Approve ${token?.symbol || 'token'} for investing in vCOW` })
} catch (error) {
console.error('[InvestOption]: Issue approving.', error)
setError(error?.message)
handleSetError(error?.message)
} finally {
setApproving(false)
}
}, [approveCallback, setError, token?.symbol])
}, [approveCallback, handleSetError, token?.symbol])

const vCowAmount = useMemo(() => {
if (!token || !price) {
Expand Down Expand Up @@ -187,15 +187,9 @@ export default function InvestOption({ approveData, updateInvestAmount, claim }:
</div>
</span>
</InvestSummary>
{/* Error messages */}
<ErrorMessage
$css="
margin: 0 auto;
padding: 0 1.25rem;
z-index: 0;
"
showClose
/>
{/* Error modal */}
<ErrorModal />
{/* Investment inputs */}
<InvestInput>
<div>
<span>
Expand Down
19 changes: 6 additions & 13 deletions src/custom/pages/Claim/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import useTransactionConfirmationModal from 'hooks/useTransactionConfirmationMod

import { GNO, USDC_BY_CHAIN } from 'constants/tokens'
import { isSupportedChain } from 'utils/supportedChainId'
import useErrorMessage from 'hooks/useErrorMessage'
import { useErrorModal } from 'hooks/useErrorMessageAndModal'

const GNO_CLAIM_APPROVE_MESSAGE = 'Approving GNO for investing in vCOW'
const USDC_CLAIM_APPROVE_MESSAGE = 'Approving USDC for investing in vCOW'
Expand Down Expand Up @@ -123,12 +123,12 @@ export default function Claim() {
setInputAddress('')
}

const { setError, ErrorMessage } = useErrorMessage()
const { handleSetError, ErrorModal } = useErrorModal()

// handle submit claim
const handleSubmitClaim = () => {
// Reset error handling
setError(undefined)
handleSetError(undefined)

// just to be sure
if (!activeClaimAccount) return
Expand All @@ -152,7 +152,7 @@ export default function Claim() {
.catch((error) => {
setClaimStatus(ClaimStatus.DEFAULT)
console.log(error)
setError(error?.message)
handleSetError(error?.message)
})
} else {
const inputData = [...getIndexes(freeClaims), ...selected].map((idx: number) => {
Expand Down Expand Up @@ -218,6 +218,8 @@ export default function Claim() {
<PageWrapper>
{/* Approve confirmation modal */}
<TransactionConfirmationModal />
{/* Error modal */}
<ErrorModal />
{/* If claim is confirmed > trigger confetti effect */}
<Confetti start={claimStatus === ClaimStatus.CONFIRMED} />

Expand Down Expand Up @@ -308,15 +310,6 @@ export default function Claim() {
</>
)}
</FooterNavButtons>
{/* Error messages */}
<ErrorMessage
$css="
margin: -2rem auto 0 auto;
padding: 2rem 1.25rem 0rem 1rem;
width: 95%;
z-index: 0;
"
/>
</PageWrapper>
)
}
2 changes: 1 addition & 1 deletion src/custom/pages/Swap/SwapMod.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ import { ApplicationModal } from 'state/application/reducer'
import TransactionConfirmationModal, { OperationType } from 'components/TransactionConfirmationModal'
import AffiliateStatusCheck from 'components/AffiliateStatusCheck'
import usePriceImpact from 'hooks/usePriceImpact'
import useErrorMessage from 'hooks/useErrorMessage'
import { useErrorMessage } from 'hooks/useErrorMessageAndModal'

// MOD - exported in ./styleds to avoid circ dep
// export const StyledInfo = styled(Info)`
Expand Down
1 change: 1 addition & 0 deletions src/state/application/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export enum ApplicationModal {
PRIVACY_POLICY,
// ----------------- MOD: CowSwap specific modals --------------------
TRANSACTION_CONFIRMATION,
TRANSACTION_ERROR,
ARBITRUM_OPTIONS,
// ------------------------------------------------------------------------------
}
Expand Down