Skip to content

Commit

Permalink
refactor ux
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyenhoaidanh committed Sep 28, 2023
1 parent bfbf43c commit 2911f48
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 77 deletions.
19 changes: 6 additions & 13 deletions src/components/swapv2/LimitOrder/EditOrderModal.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Trans } from '@lingui/macro'
import { ethers } from 'ethers'
import { useCallback, useMemo, useState } from 'react'
import { useMemo, useState } from 'react'
import { X } from 'react-feather'
import { Flex, Text } from 'rebass'
import { useGetTotalActiveMakingAmountQuery } from 'services/limitOrder'
Expand Down Expand Up @@ -75,17 +75,6 @@ export default function EditOrderModal({
const [cancelStatus, setCancelStatus] = useState<CancelStatus>(CancelStatus.WAITING)
const [expiredTime, setExpiredTime] = useState(0)

const handleError = useCallback(
(error: any) => {
setFlowState(state => ({
...state,
attemptingTxn: false,
errorMessage: getErrorMessage(error),
}))
},
[setFlowState],
)

const signOrder = useSignOrder(setFlowState)
const { orderEditing } = useLimitState()
const onSubmitEditOrder = async (cancelType: CancelOrderType) => {
Expand All @@ -101,7 +90,11 @@ export default function EditOrderModal({
else onDismiss()
} catch (error) {
order && removeOrderNeedCreated(order.id)
handleError(error)
setFlowState(state => ({
...state,
attemptingTxn: false,
errorMessage: getErrorMessage(error),
}))
}
}

Expand Down
1 change: 1 addition & 0 deletions src/components/swapv2/LimitOrder/ListOrder/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ export default function ListLimitOrder() {
<CancelOrderModal
isOpen={isOpenCancel}
flowState={flowState}
setFlowState={setFlowState}
onDismiss={hideConfirmCancel}
onSubmit={onCancelOrder}
order={currentOrder}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { TransactionFlowState } from 'types/TransactionFlowState'
import { getContract } from 'utils/getContract'
import { sendEVMTransaction } from 'utils/sendTransaction'

import { formatAmountOrder, formatSignature, getErrorMessage, getPayloadTracking } from '../helpers'
import { formatAmountOrder, formatSignature, getPayloadTracking } from '../helpers'
import { CancelOrderType, LimitOrder } from '../type'

export const useGetEncodeLimitOrder = () => {
Expand Down Expand Up @@ -161,19 +161,11 @@ const useRequestCancelOrder = ({
}

const onCancelOrder = async (orders: LimitOrder[], cancelType: CancelOrderType) => {
try {
const resp = await (cancelType === CancelOrderType.HARD_CANCEL
? requestHardCancelOrder(orders?.[0])
: requestGasLessCancelOrder(orders))
setFlowState(state => ({ ...state, attemptingTxn: false, showConfirm: false }))
return resp
} catch (error) {
setFlowState(state => ({
...state,
attemptingTxn: false,
errorMessage: getErrorMessage(error),
}))
}
const resp = await (cancelType === CancelOrderType.HARD_CANCEL
? requestHardCancelOrder(orders?.[0])
: requestGasLessCancelOrder(orders))
setFlowState(state => ({ ...state, attemptingTxn: false, showConfirm: false }))
return resp
}

const onUpdateOrder = async (orders: LimitOrder[], cancelType: CancelOrderType) => {
Expand Down
80 changes: 31 additions & 49 deletions src/components/swapv2/LimitOrder/Modals/CancelOrderModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import useAllActiveOrders, { useIsSupportSoftCancelOrder } from 'components/swap
import { useCurrencyV2 } from 'hooks/Tokens'
import { TransactionFlowState } from 'types/TransactionFlowState'

import { BaseTradeInfo, useBaseTradeInfoLimitOrder } from '../../../../hooks/useBaseTradeInfo'
import { calcPercentFilledOrder, formatAmountOrder } from '../helpers'
import { useBaseTradeInfoLimitOrder } from '../../../../hooks/useBaseTradeInfo'
import { calcPercentFilledOrder, formatAmountOrder, getErrorMessage } from '../helpers'
import { CancelOrderFunction, CancelOrderResponse, CancelOrderType, LimitOrder, LimitOrderStatus } from '../type'
import { Container, Header, Label, ListInfo, Note, Rate, Value } from './styled'

Expand All @@ -23,23 +23,27 @@ export enum CancelStatus {
}

const styleLogo = { width: 20, height: 20 }
function ContentCancel({
function CancelOrderModal({
isCancelAll,
order,
marketPrice,
onSubmit,
onDismiss,
flowState,
isOpen,
setFlowState,
}: {
isCancelAll: boolean
order: LimitOrder | undefined
marketPrice: BaseTradeInfo | undefined
onSubmit: CancelOrderFunction
onDismiss: () => void
flowState: TransactionFlowState
isOpen: boolean
setFlowState: React.Dispatch<React.SetStateAction<TransactionFlowState>>
}) {
const currencyIn = useCurrencyV2(order?.makerAsset) || undefined
const currencyOut = useCurrencyV2(order?.takerAsset) || undefined
const { tradeInfo: marketPrice } = useBaseTradeInfoLimitOrder(currencyIn, currencyOut)

const {
takerAssetLogoURL,
makerAssetSymbol,
Expand Down Expand Up @@ -68,20 +72,28 @@ function ContentCancel({
const supportGasLessCancel = isCancelAll ? supportCancelGaslessAllOrders : isOrderSupportGaslessCancel(order)

const requestCancel = async (type: CancelOrderType) => {
const gasLessCancel = type === CancelOrderType.GAS_LESS_CANCEL
const signal = controller.current.signal
const data: CancelOrderResponse = await onSubmit(
isCancelAll ? (gasLessCancel ? ordersSoftCancel : orders) : order ? [order] : [],
type,
)
if (signal.aborted) {
onDismiss()
return
try {
const gasLessCancel = type === CancelOrderType.GAS_LESS_CANCEL
const signal = controller.current.signal
const data: CancelOrderResponse = await onSubmit(
isCancelAll ? (gasLessCancel ? ordersSoftCancel : orders) : order ? [order] : [],
type,
)
if (signal.aborted) {
onDismiss()
return
}
setCancelStatus(gasLessCancel ? CancelStatus.COUNTDOWN : CancelStatus.WAITING)
const expired = data?.orders?.[0]?.operatorSignatureExpiredAt
if (expired) setExpiredTime(expired)
else onDismiss()
} catch (error) {
setFlowState(state => ({
...state,
attemptingTxn: false,
errorMessage: getErrorMessage(error),
}))
}
setCancelStatus(gasLessCancel ? CancelStatus.COUNTDOWN : CancelStatus.WAITING)
const expired = data?.orders?.[0]?.operatorSignatureExpiredAt
if (expired) setExpiredTime(expired)
else onDismiss()
}

const onClickGaslessCancel = () => !isCountDown && requestCancel(CancelOrderType.GAS_LESS_CANCEL)
Expand Down Expand Up @@ -207,34 +219,4 @@ function ContentCancel({
)
}

export default function CancelOrderModal({
onSubmit,
onDismiss,
flowState,
order,
isOpen,
isCancelAll,
}: {
onSubmit: CancelOrderFunction
onDismiss: () => void
flowState: TransactionFlowState
order?: LimitOrder
isOpen: boolean
isCancelAll: boolean
}) {
const currencyIn = useCurrencyV2(order?.makerAsset) || undefined
const currencyOut = useCurrencyV2(order?.takerAsset) || undefined
const { tradeInfo } = useBaseTradeInfoLimitOrder(currencyIn, currencyOut)

return (
<ContentCancel
isOpen={isOpen}
onSubmit={onSubmit}
onDismiss={onDismiss}
marketPrice={tradeInfo}
isCancelAll={isCancelAll}
order={order}
flowState={flowState}
/>
)
}
export default CancelOrderModal
2 changes: 1 addition & 1 deletion src/components/swapv2/LimitOrder/Modals/styled.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ const ListWrapper = styled.div`
flex-direction: column;
gap: 12px;
border-radius: 12px;
background-color: ${({ theme }) => rgba(theme.buttonBlack, 0.2)};
background-color: ${({ theme }) => rgba(theme.buttonBlack, 0.3)};
padding: 16px;
`
export function ListInfo({
Expand Down

0 comments on commit 2911f48

Please sign in to comment.