From 53e04130d9a54ee66c86ced96cd729b86e6feee6 Mon Sep 17 00:00:00 2001 From: WRadoslaw Date: Tue, 3 Oct 2023 15:10:45 +0200 Subject: [PATCH] Add rhf and transaction into modal --- .../_crt/SellTokenModal/SellTokenModal.tsx | 141 +++++++++++++----- 1 file changed, 106 insertions(+), 35 deletions(-) diff --git a/packages/atlas/src/components/_crt/SellTokenModal/SellTokenModal.tsx b/packages/atlas/src/components/_crt/SellTokenModal/SellTokenModal.tsx index d41091a4fe..1a7f8cd9a6 100644 --- a/packages/atlas/src/components/_crt/SellTokenModal/SellTokenModal.tsx +++ b/packages/atlas/src/components/_crt/SellTokenModal/SellTokenModal.tsx @@ -1,7 +1,7 @@ -import { useMemo, useState } from 'react' +import { useMemo } from 'react' +import { Controller, useForm } from 'react-hook-form' import { FlexBox } from '@/components/FlexBox/FlexBox' -import { Information } from '@/components/Information' import { JoyTokenIcon } from '@/components/JoyTokenIcon' import { NumberFormat } from '@/components/NumberFormat' import { Text } from '@/components/Text' @@ -10,7 +10,12 @@ import { FormField } from '@/components/_inputs/FormField' import { TokenInput } from '@/components/_inputs/TokenInput' import { DetailsContent } from '@/components/_nft/NftTile' import { DialogModal } from '@/components/_overlays/DialogModal' +import { atlasConfig } from '@/config' import { useMediaMatch } from '@/hooks/useMediaMatch' +import { useFee, useJoystream, useTokenPrice } from '@/providers/joystream' +import { useSnackbar } from '@/providers/snackbars' +import { useTransaction } from '@/providers/transactions/transactions.hooks' +import { useUser } from '@/providers/user/user.hooks' export type SellTokenModalProps = { tokenId: string @@ -25,40 +30,96 @@ const getTokenDetails = (_: string) => ({ }) export const SellTokenModal = ({ tokenId, onClose }: SellTokenModalProps) => { - const [tokens, setTokens] = useState(null) + const { control, watch, handleSubmit } = useForm<{ tokens: number }>() + const { convertTokensToUSD } = useTokenPrice() const smMatch = useMediaMatch('sm') + const { memberId } = useUser() const { pricePerUnit, tokensOnSale, userBalance, title } = getTokenDetails(tokenId) - + const tokens = watch('tokens') + const { joystream, proxyCallback } = useJoystream() + const handleTransaction = useTransaction() + const { displaySnackbar } = useSnackbar() + const { fullFee } = useFee('sellTokenOnMarketTx') const details = useMemo( () => [ { - title: 'You will get', + title: 'You will receive', content: ( 1_000_000 ? 'short' : 'full'} as="p" + variant="t200-strong" + withToken + /> + ), + }, + { + title: 'Purchase', + content: ( + ), - tooltipText: 'Lorem ipsum', }, { - title: 'Fee', - content: , - tooltipText: 'Lorem ipsum', + title: 'Platform fee', // todo: introduce platform fee + content: , + }, + { + title: 'Transaction fee', + content: , }, { - title: 'You will spend', - content: , - tooltipText: 'Lorem ipsum', + title: 'Total', + content: ( + + + + + + + + + ), }, ], - [title, tokens, tokensOnSale] + [fullFee, pricePerUnit, title, tokens, tokensOnSale] ) + + const onSubmit = () => + handleSubmit((data) => { + if (!joystream || !memberId) { + return + } + handleTransaction({ + txFactory: async (updateStatus) => + (await joystream.extrinsics).sellTokenOnMarket( + tokenId, + memberId, + String(data.tokens), + proxyCallback(updateStatus) + ), + onError: () => { + displaySnackbar({ + title: 'Something went wrong', + iconType: 'error', + }) + }, + onTxSync: async () => { + displaySnackbar({ + title: `${tokens * pricePerUnit} ${atlasConfig.joystream.tokenTicker} received`, + description: `${tokens} $${title} sold`, + }) + onClose() + }, + }) + })() + return ( { onExitClick={onClose} primaryButton={{ text: 'Sell tokens', + onClick: onSubmit, }} > @@ -87,31 +149,40 @@ export const SellTokenModal = ({ tokenId, onClose }: SellTokenModalProps) => { withDenomination /> - - - - $0.00 - - Max - - } - /> - + ( + + + + ${convertTokensToUSD(field.value * pricePerUnit)?.toFixed(2)} + + field.onChange(userBalance)}>Max + + } + /> + + )} + /> {details.map((row, i) => ( - - - {row.title} - - - + + {row.title} + {row.content} ))}