Skip to content

Commit

Permalink
wait for state change before runing action
Browse files Browse the repository at this point in the history
  • Loading branch information
bryzettler committed Jan 20, 2024
1 parent db9e9a9 commit 35c1e68
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions src/features/governance/PositionCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ import {
secsToDays,
} from '@utils/dateTools'
import BN from 'bn.js'
import React, { useCallback, useMemo, useState } from 'react'
import React, { useCallback, useMemo, useRef, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { FadeIn, FadeOut } from 'react-native-reanimated'
import { useAsync } from 'react-async-hook'
import { useSolana } from '../../solana/SolanaProvider'
import { useWalletSign } from '../../solana/WalletSignProvider'
import { WalletStandardMessageTypes } from '../../solana/walletSignBottomSheetTypes'
Expand All @@ -70,6 +71,9 @@ export const PositionCard = ({
const { showOKAlert } = useAlert()
const { walletSignBottomSheetRef } = useWalletSign()
const [actionsOpen, setActionsOpen] = useState(false)
const actionRef = useRef<
null | 'undelegate' | 'relinquish' | 'flipLockupKind' | 'close'
>(null)
const [isTransferModalOpen, setIsTransferModalOpen] = useState(false)
const [isExtendModalOpen, setIsExtendModalOpen] = useState(false)
const [isSplitModalOpen, setIsSplitModalOpen] = useState(false)
Expand Down Expand Up @@ -253,6 +257,21 @@ export const PositionCard = ({
relinquishPositionVotes,
} = useRelinquishPositionVotes()

// used for actions that run right after clicking the action button
useAsync(async () => {
const ref = actionRef.current
actionRef.current = null

if (ref !== null) {
await {
relinquish: handleRelinquishVotes,
flipLockupKind: handleFlipPositionLockupKind,
undelegate: handleUndelegateTokens,
close: handleClosePosition,
}[ref]?.()
}
}, [actionRef.current])

const transactionError = useMemo(() => {
if (extendingError) {
return extendingError.message || t('gov.errors.extendLockup')
Expand Down Expand Up @@ -429,7 +448,7 @@ export const PositionCard = ({
title={t('gov.positions.undelegate')}
onPress={async () => {
setActionsOpen(false)
await handleUndelegateTokens()
actionRef.current = 'undelegate'
}}
selected={false}
hasPressedState={false}
Expand All @@ -448,7 +467,7 @@ export const PositionCard = ({
message: t('gov.positions.partakingInVote'),
})
} else {
await handleClosePosition()
actionRef.current = 'close'
}
}}
selected={false}
Expand Down Expand Up @@ -494,8 +513,8 @@ export const PositionCard = ({
key="extend"
title={t('gov.positions.extend')}
onPress={() => {
setIsExtendModalOpen(true)
setActionsOpen(false)
setIsExtendModalOpen(true)
}}
selected={false}
hasPressedState={false}
Expand All @@ -517,7 +536,7 @@ export const PositionCard = ({
message: t('gov.positions.partakingInVote'),
})
} else {
await handleFlipPositionLockupKind()
actionRef.current = 'flipLockupKind'
}
}}
selected={false}
Expand All @@ -528,8 +547,8 @@ export const PositionCard = ({
key="delegate"
title={t('gov.positions.delegate')}
onPress={() => {
setIsDelegateModalOpen(true)
setActionsOpen(false)
setIsDelegateModalOpen(true)
}}
selected={false}
hasPressedState={false}
Expand All @@ -539,9 +558,9 @@ export const PositionCard = ({
<ListItem
key="relinquish"
title={t('gov.positions.relinquish')}
onPress={async () => {
onPress={() => {
setActionsOpen(false)
await handleRelinquishVotes()
actionRef.current = 'relinquish'
}}
selected={false}
hasPressedState={false}
Expand Down

0 comments on commit 35c1e68

Please sign in to comment.