From 86ebc1ad8247700614f094d71d6d7681c57bf755 Mon Sep 17 00:00:00 2001 From: Lucas Magnus Date: Wed, 13 Dec 2023 00:47:59 -0300 Subject: [PATCH] fixes ui contracts --- .../app/core/pages/contracts-create/index.tsx | 1 + .../app/core/pages/contracts-detail/index.tsx | 37 ++- .../molecules/select-asset/index.tsx | 18 +- .../molecules/select-vault/index.tsx | 4 +- .../templates/contracts-create/index.tsx | 288 +++++++++++------- .../components/contract-info/index.tsx | 3 - .../components/deposit-details/index.tsx | 8 +- .../components/deposit/index.tsx | 162 +++++++--- .../components/withdraw-details/index.tsx | 5 - .../components/withdraw/index.tsx | 2 +- .../templates/contracts-detail/index.tsx | 20 +- 11 files changed, 352 insertions(+), 196 deletions(-) diff --git a/frontend/src/app/core/pages/contracts-create/index.tsx b/frontend/src/app/core/pages/contracts-create/index.tsx index 8062110b..60f11ee8 100644 --- a/frontend/src/app/core/pages/contracts-create/index.tsx +++ b/frontend/src/app/core/pages/contracts-create/index.tsx @@ -31,6 +31,7 @@ export const ContractsCreate: React.FC = () => { const { vaults, loadingVaults, getVaults } = useVaults() const { sign, submit, getSponsorPK } = useTransactions() const { getLatestSequenceLedger } = useHorizon() + const toast = useToast() const navigate = useNavigate() diff --git a/frontend/src/app/core/pages/contracts-detail/index.tsx b/frontend/src/app/core/pages/contracts-detail/index.tsx index 898a5d17..83f769bd 100644 --- a/frontend/src/app/core/pages/contracts-detail/index.tsx +++ b/frontend/src/app/core/pages/contracts-detail/index.tsx @@ -1,7 +1,7 @@ import { Flex, useToast } from '@chakra-ui/react' import React, { useCallback, useEffect, useState } from 'react' import { FieldValues, UseFormSetValue } from 'react-hook-form' -import { useParams } from 'react-router-dom' +import { useNavigate, useParams } from 'react-router-dom' import { useAuth } from 'hooks/useAuth' import { useContracts } from 'hooks/useContracts' @@ -38,6 +38,7 @@ export const ContractsDetail: React.FC = () => { const [currentInVault, setCurrentInVault] = useState() const { id } = useParams() + const navigate = useNavigate() useEffect(() => { getUserPermissions() @@ -66,10 +67,10 @@ export const ContractsDetail: React.FC = () => { const userYield = Number((await getYield(wallet, contract.address)) || 0) / 10000000 - const estimatedPrematureWithdraw = await getEstimatedPrematureWithdraw( - wallet, - contract.address - ) + const estimatedPrematureWithdraw = + Number( + (await getEstimatedPrematureWithdraw(wallet, contract.address)) || 0 + ) / 10000000 if (!timeLeft) { timeLeft = @@ -236,21 +237,43 @@ export const ContractsDetail: React.FC = () => { return history ? history[0]?.deposit_amount : undefined } + const hasAssetInVault = (): boolean => { + return ( + profile?.vault?.accountData?.balances.find( + balance => + balance.asset_code === contract?.asset.code && + balance.asset_issuer === contract?.asset.issuer.key.publicKey + ) !== undefined + ) + } + + const accessWallet = (): void => { + navigate(`${PathRoute.VAULT_DETAIL}/${profile?.vault_id}`) + } + + const accessProfile = (): void => { + navigate(`${PathRoute.PROFILE}`) + } + return ( diff --git a/frontend/src/components/molecules/select-asset/index.tsx b/frontend/src/components/molecules/select-asset/index.tsx index 80aec060..221f8bd8 100644 --- a/frontend/src/components/molecules/select-asset/index.tsx +++ b/frontend/src/components/molecules/select-asset/index.tsx @@ -1,5 +1,11 @@ -import { useColorMode } from '@chakra-ui/react' -import React, { Dispatch, SetStateAction, useEffect, useState } from 'react' +import { Flex, Text, useColorMode } from '@chakra-ui/react' +import React, { + Dispatch, + ReactNode, + SetStateAction, + useEffect, + useState, +} from 'react' import Select from 'react-select' export interface IOption { @@ -38,6 +44,13 @@ export const SelectAsset: React.FC = ({ setOptions(ops || []) }, [assets]) + const formatLabel = (data: IOption): ReactNode => ( + + {data.label} + {data.value.name} + + ) + return ( { - setValue('min_deposit', toNumber(event.target.value)) - }} - /> + Vault + - Term - - { - setValue('term', toNumber(event.target.value)) - }} - /> - - + Asset + - - - Yield Rate - - - - - - - - Penalty rate - + + + + Minimum Deposit { - setValue('penalty_rate', toNumber(event.target.value)) + setValue('min_deposit', toNumber(event.target.value)) }} /> - - + - - - - - - - Compound - - {compoundType === 'Compound interest' && } - - - - Simple Interest: Interest payment based on a percentage of the deposited amount over the term period. - Compound interest: Interest earned not just based on the deposited amount, but also on the interest already earned so far at each compound interval. + + Term + + { + setValue('term', toNumber(event.target.value)) + }} + /> + + + + + + + + Yield Rate + + + + + + + + Penalty rate + + { + setValue( + 'penalty_rate', + toNumber(event.target.value) + ) + }} + /> + + + + - - - - - - } + + Compound + + {compoundType === 'Compound interest' && ( + + )} + + + + + Simple Interest: Interest payment based on a + percentage of the deposited amount over the term period. + + + Compound interest: Interest earned not just based + on the deposited amount, but also on the interest already + earned so far at each compound interval. + + + + + + + + + + )} diff --git a/frontend/src/components/templates/contracts-detail/components/contract-info/index.tsx b/frontend/src/components/templates/contracts-detail/components/contract-info/index.tsx index 38578130..10d9ab09 100644 --- a/frontend/src/components/templates/contracts-detail/components/contract-info/index.tsx +++ b/frontend/src/components/templates/contracts-detail/components/contract-info/index.tsx @@ -20,9 +20,6 @@ export const ContractInfo: React.FC = ({ contract }) => { return ( - - Contract details - = ({ return ( - - Deposit info - @@ -103,7 +99,7 @@ export const DepositDetails: React.FC = ({ w="fit-content" mb="0.15rem" > - Current yield + Accrued Yield {`${(contractData.yield / 100).toFixed( 2 @@ -144,7 +140,7 @@ export const DepositDetails: React.FC = ({ w="fit-content" mb="0.15rem" > - Composed in + Compounds in ): Promise + accessWallet(): void + accessProfile(): void contract: Hooks.UseContractsTypes.IContract | undefined loading: boolean currentBalance: string + hasAssetInVault: boolean + hasWallet: boolean } export const Deposit: React.FC = ({ onSubmit, + accessWallet, + accessProfile, loading, contract, currentBalance, + hasAssetInVault, + hasWallet, }) => { const { formState: { errors }, @@ -62,64 +70,120 @@ export const Deposit: React.FC = ({ {`Deposit ${contract?.asset.code}`} - - Current in my wallet - - {contract?.asset?.image ? ( - - ) : ( - getCurrencyIcon(contract?.asset.code || '', '1rem') - )} - {`${toCrypto(Number(currentBalance))} ${contract?.asset.code}`} + {!hasWallet ? ( + + + You don't have a wallet yet + + - - -
{ - data.amount = amount - onSubmit(data, setValue) - })} - > - - { - setAmount(toNumber(target.currentTarget.value)) - limitExceeded(target.currentTarget.value) - }} - /> - Required - + ) : hasAssetInVault ? ( + <> + + Current in my wallet + + {contract?.asset?.image ? ( + + ) : ( + getCurrencyIcon(contract?.asset.code || '', '1rem') + )} + {`${toCrypto(Number(currentBalance))} ${contract?.asset.code}`} + + + + { + data.amount = amount + onSubmit(data, setValue) + })} + > + + { + setAmount(toNumber(target.currentTarget.value)) + limitExceeded(target.currentTarget.value) + }} + /> + Required + + + +
+ + ) : ( + + + You don't have this asset in your account + - -
+
+ )}
) diff --git a/frontend/src/components/templates/contracts-detail/components/withdraw-details/index.tsx b/frontend/src/components/templates/contracts-detail/components/withdraw-details/index.tsx index b48fcd13..71556529 100644 --- a/frontend/src/components/templates/contracts-detail/components/withdraw-details/index.tsx +++ b/frontend/src/components/templates/contracts-detail/components/withdraw-details/index.tsx @@ -14,18 +14,13 @@ export const WithdrawDetails: React.FC = ({ contractData, history, }) => { - return ( - - Deposit info - diff --git a/frontend/src/components/templates/contracts-detail/components/withdraw/index.tsx b/frontend/src/components/templates/contracts-detail/components/withdraw/index.tsx index 3943b021..9adb5e3b 100644 --- a/frontend/src/components/templates/contracts-detail/components/withdraw/index.tsx +++ b/frontend/src/components/templates/contracts-detail/components/withdraw/index.tsx @@ -220,7 +220,7 @@ export const Withdraw: React.FC = ({ mt="1rem" > - {isDone ? 'Withdraw' : 'Withdraw premature'} + {isDone ? 'Withdraw' : 'Withdraw with penalty'} diff --git a/frontend/src/components/templates/contracts-detail/index.tsx b/frontend/src/components/templates/contracts-detail/index.tsx index 36fe18b0..7c895f96 100644 --- a/frontend/src/components/templates/contracts-detail/index.tsx +++ b/frontend/src/components/templates/contracts-detail/index.tsx @@ -3,6 +3,7 @@ import React from 'react' import { FieldValues, UseFormSetValue } from 'react-hook-form' import { havePermission } from 'utils' +import { MAX_PAGE_WIDTH } from 'utils/constants/sizes' import { ContractInfo } from './components/contract-info' import { Deposit } from './components/deposit' @@ -18,6 +19,8 @@ interface IContractsDetailTemplate { data: FieldValues, setValue: UseFormSetValue ): Promise + accessWallet(): void + accessProfile(): void contractData: Hooks.UseContractsTypes.IContractData | undefined contract: Hooks.UseContractsTypes.IContract | undefined loading: boolean @@ -29,11 +32,15 @@ interface IContractsDetailTemplate { deposited: number | undefined userPermissions: Hooks.UseAuthTypes.IUserPermission[] | undefined currentInVault: string | undefined + hasAssetInVault: boolean + hasWallet: boolean } export const ContractsDetailTemplate: React.FC = ({ onSubmitWithdraw, onSubmitDeposit, + accessWallet, + accessProfile, contract, contractData, isWithdrawing, @@ -44,10 +51,12 @@ export const ContractsDetailTemplate: React.FC = ({ deposited, userPermissions, currentInVault, + hasAssetInVault, + hasWallet, }) => { return ( - + {loading || !contract ? ( ) : ( @@ -69,8 +78,6 @@ export const ContractsDetailTemplate: React.FC = ({ maxW="full" gap="2rem" > - - {contractData?.position && havePermission( Permissions.INVEST_CERTIFICATE, @@ -90,6 +97,7 @@ export const ContractsDetailTemplate: React.FC = ({ history={history} /> )} + {contractData?.position ? ( = ({ isDone={!contractData.timeLeft || contractData.timeLeft === 0} withdrawValue={ contractData.timeLeft && contractData.timeLeft > 0 - ? Number(contractData.position) + ? Number(contractData.estimatedPrematureWithdraw) : Number(contractData.position) } contractData={contractData} @@ -109,9 +117,13 @@ export const ContractsDetailTemplate: React.FC = ({ ) : ( )}