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

Commit

Permalink
[CLAIM] Button refactor and error fix (#2218)
Browse files Browse the repository at this point in the history
* export types

* move Footer code into new file

* if statement
  • Loading branch information
W3stside authored Jan 19, 2022
1 parent 5d43dc5 commit f35c12f
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 66 deletions.
2 changes: 1 addition & 1 deletion src/custom/pages/Claim/ClaimAddress.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import useENS from 'hooks/useENS'
import { useClaimDispatchers, useClaimState } from 'state/claim/hooks'
import { ClaimStatus } from 'state/claim/actions'

type ClaimAddressProps = Pick<ClaimCommonTypes, 'account'> & {
export type ClaimAddressProps = Pick<ClaimCommonTypes, 'account'> & {
toggleWalletModal: () => void
}

Expand Down
2 changes: 1 addition & 1 deletion src/custom/pages/Claim/ClaimsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { CustomLightSpinner } from 'theme'
import Circle from 'assets/images/blue-loader.svg'
import { Countdown } from 'pages/Claim/Countdown'

type ClaimsTableProps = {
export type ClaimsTableProps = {
handleSelectAll: (event: React.ChangeEvent<HTMLInputElement>) => void
handleSelect: (event: React.ChangeEvent<HTMLInputElement>, index: number) => void
userClaimData: EnhancedUserClaimData[]
Expand Down
111 changes: 111 additions & 0 deletions src/custom/pages/Claim/FooterNavButtons.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import { Trans } from '@lingui/macro'
import { isAddress } from '@ethersproject/address'
import { useClaimDispatchers, useClaimState } from 'state/claim/hooks'
import { ButtonPrimary, ButtonSecondary } from 'components/Button'
import { ClaimStatus } from 'state/claim/actions'
import { FooterNavButtons as FooterNavButtonsWrapper } from 'pages/Claim/styled'
import { useActiveWeb3React } from 'hooks/web3'
import { ClaimsTableProps } from './ClaimsTable'
import { ClaimAddressProps } from './ClaimAddress'
import { ReactNode } from 'react'

type FooterNavButtonsProps = Pick<ClaimsTableProps, 'hasClaims' | 'isAirdropOnly'> &
Pick<ClaimAddressProps, 'toggleWalletModal'> & {
isPaidClaimsOnly: boolean
resolvedAddress: string | null
handleSubmitClaim: () => void
handleCheckClaim: () => void
}

export default function FooterNavButtons({
hasClaims,
isAirdropOnly,
isPaidClaimsOnly,
resolvedAddress,
toggleWalletModal,
handleSubmitClaim,
handleCheckClaim,
}: FooterNavButtonsProps) {
const { account } = useActiveWeb3React()
const {
// account
activeClaimAccount,
// claiming
claimStatus,
// investment
investFlowStep,
isInvestFlowActive,
// table select change
selected,
} = useClaimState()

const {
// investing
setInvestFlowStep,
setIsInvestFlowActive,
} = useClaimDispatchers()

const isInputAddressValid = isAddress(resolvedAddress || '')

// User is connected and has some unclaimed claims
const isConnectedAndHasClaims = !!activeClaimAccount && !!hasClaims && claimStatus === ClaimStatus.DEFAULT
const noPaidClaimsSelected = !selected.length

let buttonContent: ReactNode = null
// Disconnected, show wallet connect
if (!account) {
buttonContent = (
<ButtonPrimary onClick={toggleWalletModal}>
<Trans>Connect a wallet</Trans>
</ButtonPrimary>
)
}

// User has no set active claim account and/or has claims, show claim account search
if (!activeClaimAccount || !hasClaims) {
buttonContent = (
<ButtonPrimary disabled={!isInputAddressValid} type="text" onClick={handleCheckClaim}>
<Trans>Check claimable vCOW</Trans>
</ButtonPrimary>
)
}

// USER is CONNECTED + HAS SOMETHING TO CLAIM
if (isConnectedAndHasClaims) {
if (!isInvestFlowActive) {
buttonContent = (
<ButtonPrimary onClick={handleSubmitClaim} disabled={isPaidClaimsOnly && noPaidClaimsSelected}>
<Trans>Claim vCOW</Trans>
</ButtonPrimary>
)
} else if (!isAirdropOnly) {
buttonContent = (
<>
{investFlowStep === 0 ? (
<ButtonPrimary onClick={() => setInvestFlowStep(1)}>
<Trans>Approve tokens</Trans>
</ButtonPrimary>
) : investFlowStep === 1 ? (
<ButtonPrimary onClick={() => setInvestFlowStep(2)}>
<Trans>Review</Trans>
</ButtonPrimary>
) : (
<ButtonPrimary onClick={handleSubmitClaim}>
<Trans>Claim and invest vCOW</Trans>
</ButtonPrimary>
)}

<ButtonSecondary
onClick={() =>
investFlowStep === 0 ? setIsInvestFlowActive(false) : setInvestFlowStep(investFlowStep - 1)
}
>
<Trans>Go back</Trans>
</ButtonSecondary>
</>
)
}
}

return <FooterNavButtonsWrapper>{buttonContent}</FooterNavButtonsWrapper>
}
91 changes: 27 additions & 64 deletions src/custom/pages/Claim/index.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { useEffect, useMemo } from 'react'
import { Trans } from '@lingui/macro'
import { useCallback, useEffect, useMemo } from 'react'
import { CurrencyAmount, MaxUint256 } from '@uniswap/sdk-core'
import { useActiveWeb3React } from 'hooks/web3'
import { useUserEnhancedClaimData, useUserUnclaimedAmount, useClaimCallback, ClaimInput } from 'state/claim/hooks'
import { ButtonPrimary, ButtonSecondary } from 'components/Button'
import { PageWrapper, FooterNavButtons } from 'pages/Claim/styled'
import { PageWrapper } from 'pages/Claim/styled'
import EligibleBanner from './EligibleBanner'
import { getFreeClaims, hasPaidClaim, getIndexes, getPaidClaims } from 'state/claim/hooks/utils'
import { getFreeClaims, hasPaidClaim, getIndexes, getPaidClaims, hasFreeClaim } from 'state/claim/hooks/utils'
import { useWalletModalToggle } from 'state/application/hooks'
import Confetti from 'components/Confetti'
import { isAddress } from 'web3-utils'

import useENS from 'hooks/useENS'

import ClaimNav from './ClaimNav'
Expand All @@ -31,6 +29,7 @@ import { GNO, USDC_BY_CHAIN } from 'constants/tokens'
import { isSupportedChain } from 'utils/supportedChainId'
import { useErrorModal } from 'hooks/useErrorMessageAndModal'
import { EnhancedUserClaimData } from './types'
import FooterNavButtons from './FooterNavButtons'

const GNO_CLAIM_APPROVE_MESSAGE = 'Approving GNO for investing in vCOW'
const USDC_CLAIM_APPROVE_MESSAGE = 'Approving USDC for investing in vCOW'
Expand All @@ -48,7 +47,6 @@ export default function Claim() {
// claiming
claimStatus,
// investment
isInvestFlowActive,
investFlowStep,
// table select change
selected,
Expand All @@ -66,7 +64,6 @@ export default function Claim() {
// setClaimedAmount, // TODO: uncomment when used
// investing
setIsInvestFlowActive,
setInvestFlowStep,
// claim row selection
setSelected,
setSelectedAll,
Expand All @@ -76,7 +73,6 @@ export default function Claim() {

// addresses
const { address: resolvedAddress, name: resolvedENS } = useENS(inputAddress)
const isInputAddressValid = useMemo(() => isAddress(resolvedAddress || ''), [resolvedAddress])

// toggle wallet when disconnected
const toggleWalletModal = useWalletModalToggle()
Expand All @@ -91,6 +87,7 @@ export default function Claim() {

const hasClaims = useMemo(() => userClaimData.length > 0, [userClaimData])
const isAirdropOnly = useMemo(() => !hasPaidClaim(userClaimData), [userClaimData])
const isPaidClaimsOnly = useMemo(() => hasPaidClaim(userClaimData) && !hasFreeClaim(userClaimData), [userClaimData])

// claim callback
const { claimCallback } = useClaimCallback(activeClaimAccount)
Expand Down Expand Up @@ -128,9 +125,8 @@ export default function Claim() {
setInputAddress('')
}

// TODO: useCallback
// handle submit claim
const handleSubmitClaim = () => {
const handleSubmitClaim = useCallback(() => {
// Reset error handling
handleCloseError()

Expand Down Expand Up @@ -177,7 +173,17 @@ export default function Claim() {
} else {
setIsInvestFlowActive(true)
}
}
}, [
activeClaimAccount,
investFlowStep,
selected,
userClaimData,
claimCallback,
handleCloseError,
handleSetError,
setClaimStatus,
setIsInvestFlowActive,
])

// on account/activeAccount/non-connected account (if claiming for someone else) change
useEffect(() => {
Expand Down Expand Up @@ -255,58 +261,15 @@ export default function Claim() {
}}
/>

<FooterNavButtons>
{/* General claim vCOW button (no invest) */}
{!!activeClaimAccount && !!hasClaims && !isInvestFlowActive && claimStatus === ClaimStatus.DEFAULT ? (
account ? (
<ButtonPrimary onClick={handleSubmitClaim}>
<Trans>Claim vCOW</Trans>
</ButtonPrimary>
) : (
<ButtonPrimary onClick={toggleWalletModal}>
<Trans>Connect a wallet</Trans>
</ButtonPrimary>
)
) : null}

{/* Check for claims button */}
{(!activeClaimAccount || !hasClaims) && (
<ButtonPrimary disabled={!isInputAddressValid} type="text" onClick={handleCheckClaim}>
<Trans>Check claimable vCOW</Trans>
</ButtonPrimary>
)}

{/* Invest flow button */}
{!!activeClaimAccount &&
!!hasClaims &&
claimStatus === ClaimStatus.DEFAULT &&
!isAirdropOnly &&
!!isInvestFlowActive && (
<>
{investFlowStep === 0 ? (
<ButtonPrimary onClick={() => setInvestFlowStep(1)}>
<Trans>Approve tokens</Trans>
</ButtonPrimary>
) : investFlowStep === 1 ? (
<ButtonPrimary onClick={() => setInvestFlowStep(2)}>
<Trans>Review</Trans>
</ButtonPrimary>
) : (
<ButtonPrimary onClick={handleSubmitClaim}>
<Trans>Claim and invest vCOW</Trans>
</ButtonPrimary>
)}

<ButtonSecondary
onClick={() =>
investFlowStep === 0 ? setIsInvestFlowActive(false) : setInvestFlowStep(investFlowStep - 1)
}
>
<Trans>Go back</Trans>
</ButtonSecondary>
</>
)}
</FooterNavButtons>
<FooterNavButtons
handleCheckClaim={handleCheckClaim}
handleSubmitClaim={handleSubmitClaim}
toggleWalletModal={toggleWalletModal}
isAirdropOnly={isAirdropOnly}
isPaidClaimsOnly={isPaidClaimsOnly}
hasClaims={hasClaims}
resolvedAddress={resolvedAddress}
/>
</PageWrapper>
)
}

0 comments on commit f35c12f

Please sign in to comment.