-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rui/loans modals lose close animation due to conditional render (#1460)
* wip * feat: continue * fix: code review * fix:merge --------- Co-authored-by: Thomas Jeatt <[email protected]>
- Loading branch information
1 parent
56f45b0
commit c26b788
Showing
5 changed files
with
133 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
src/pages/Loans/LoansOverview/components/CollateralModal/CollateralForm.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { LoanAsset } from '@interlay/interbtc-api'; | ||
import { useEffect, useRef } from 'react'; | ||
import { useTranslation } from 'react-i18next'; | ||
|
||
import { Flex } from '@/component-library'; | ||
import { AuthCTA, TransactionFeeDetails } from '@/components'; | ||
import { | ||
LOAN_TOGGLE_COLLATERAL_FEE_TOKEN_FIELD, | ||
toggleCollateralLoanSchema, | ||
ToggleCollateralLoansFormData, | ||
useForm | ||
} from '@/lib/form'; | ||
import { useGetAccountLendingStatistics } from '@/utils/hooks/api/loans/use-get-account-lending-statistics'; | ||
import { Transaction, useTransaction } from '@/utils/hooks/transaction'; | ||
import { isTransactionFormDisabled } from '@/utils/hooks/transaction/utils/form'; | ||
|
||
import { CollateralModalVariant } from './CollateralModal'; | ||
|
||
type CollateralFormProps = { | ||
asset: LoanAsset; | ||
variant: Extract<CollateralModalVariant, 'enable' | 'disable'>; | ||
isOpen?: boolean; | ||
onSigning: () => void; | ||
}; | ||
|
||
const CollateralForm = ({ asset, variant, isOpen, onSigning }: CollateralFormProps): JSX.Element => { | ||
const { t } = useTranslation(); | ||
|
||
const { refetch } = useGetAccountLendingStatistics(); | ||
|
||
const overlappingModalRef = useRef<HTMLDivElement>(null); | ||
|
||
const transaction = useTransaction({ | ||
onSigning, | ||
onSuccess: refetch | ||
}); | ||
|
||
const handleSubmit = () => { | ||
if (variant === 'enable') { | ||
return transaction.execute(Transaction.LOANS_ENABLE_COLLATERAL, asset.currency); | ||
} else { | ||
return transaction.execute(Transaction.LOANS_DISABLE_COLLATERAL, asset.currency); | ||
} | ||
}; | ||
|
||
const form = useForm<ToggleCollateralLoansFormData>({ | ||
initialValues: { | ||
[LOAN_TOGGLE_COLLATERAL_FEE_TOKEN_FIELD]: '' | ||
}, | ||
validationSchema: toggleCollateralLoanSchema(), | ||
onSubmit: handleSubmit, | ||
onComplete: async () => { | ||
if (variant === 'enable') { | ||
return transaction.fee.estimate(Transaction.LOANS_ENABLE_COLLATERAL, asset.currency); | ||
} else { | ||
return transaction.fee.estimate(Transaction.LOANS_DISABLE_COLLATERAL, asset.currency); | ||
} | ||
} | ||
}); | ||
|
||
// Doing this call on mount so that the form becomes dirty | ||
// TODO: find better approach | ||
useEffect(() => { | ||
if (!isOpen) return; | ||
|
||
form.setFieldValue(LOAN_TOGGLE_COLLATERAL_FEE_TOKEN_FIELD, transaction.fee.defaultCurrency.ticker, true); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [isOpen]); | ||
|
||
const isBtnDisabled = isTransactionFormDisabled(form, transaction.fee); | ||
|
||
return ( | ||
<form onSubmit={form.handleSubmit}> | ||
<Flex direction='column' gap='spacing4'> | ||
<TransactionFeeDetails | ||
fee={transaction.fee} | ||
selectProps={{ | ||
...form.getSelectFieldProps(LOAN_TOGGLE_COLLATERAL_FEE_TOKEN_FIELD), | ||
modalRef: overlappingModalRef | ||
}} | ||
/> | ||
<AuthCTA type='submit' size='large' disabled={isBtnDisabled} loading={transaction.isLoading}> | ||
{variant === 'enable' | ||
? t('use_ticker_as_collateral', { ticker: asset.currency.ticker }) | ||
: t('disable_ticker', { ticker: asset.currency.ticker })} | ||
</AuthCTA> | ||
</Flex> | ||
</form> | ||
); | ||
}; | ||
|
||
export { CollateralForm }; | ||
export type { CollateralFormProps }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
c26b788
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
interbtc-ui-kintsugi-testnet – ./
kintnet.interlay.io
interbtc-ui-kintsugi-testnet-git-master-interlay.vercel.app
interbtc-ui.vercel.app
interbtc-ui-kintsugi-testnet-interlay.vercel.app
c26b788
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
interbtc-ui-interlay-testnet – ./
interbtc-ui-interlay-testnet-interlay.vercel.app
testnet.interlay.io
interbtc-ui-interlay-testnet.vercel.app
interbtc-ui-interlay-testnet-git-master-interlay.vercel.app