diff --git a/web/cashtab/src/components/Send/Send.js b/web/cashtab/src/components/Send/Send.js --- a/web/cashtab/src/components/Send/Send.js +++ b/web/cashtab/src/components/Send/Send.js @@ -3,7 +3,6 @@ import { WalletContext } from '@utils/context'; import { Form, notification, message, Modal, Alert } from 'antd'; import { Row, Col } from 'antd'; -import Paragraph from 'antd/lib/typography/Paragraph'; import PrimaryButton, { SecondaryButton, } from '@components/Common/PrimaryButton'; @@ -13,6 +12,7 @@ } from '@components/Common/EnhancedInputs'; import useBCH from '@hooks/useBCH'; import useWindowDimensions from '@hooks/useWindowDimensions'; +import { sendXecNotification } from '@hooks/Notifications'; import { isMobile, isIOS, isSafari } from 'react-device-detect'; import { currency, @@ -30,7 +30,6 @@ AlertMsg, } from '@components/Common/Atoms'; import { getWalletState } from '@utils/cashMethods'; -import { CashReceivedNotificationIcon } from '@components/Common/CustomIcons'; import ApiError from '@components/Common/ApiError'; // Note jestBCH is only used for unit tests; BCHJS must be mocked for jest @@ -192,21 +191,7 @@ bchValue, currency.defaultFee, ); - - notification.success({ - message: 'Success', - description: ( - - - Transaction successful. Click to view in block - explorer. - - - ), - duration: 3, - icon: , - style: { width: '100%' }, - }); + sendXecNotification(); } catch (e) { // Set loading to false here as well, as balance may not change depending on where error occured in try loop passLoadingStatus(false); diff --git a/web/cashtab/src/components/Send/SendToken.js b/web/cashtab/src/components/Send/SendToken.js --- a/web/cashtab/src/components/Send/SendToken.js +++ b/web/cashtab/src/components/Send/SendToken.js @@ -10,7 +10,6 @@ Alert, Descriptions, } from 'antd'; -import Paragraph from 'antd/lib/typography/Paragraph'; import PrimaryButton, { SecondaryButton, } from '@components/Common/PrimaryButton'; @@ -36,8 +35,8 @@ getWalletState, convertEtokenToSimpleledger, } from '@utils/cashMethods'; -import { TokenReceivedNotificationIcon } from '@components/Common/CustomIcons'; import ApiError from '@components/Common/ApiError'; +import { sendTokenNotification } from '../../hooks/Notifications'; const SendToken = ({ tokenId, jestBCH, passLoadingStatus }) => { const { wallet, apiError } = React.useContext(WalletContext); @@ -114,20 +113,7 @@ amount: value, }); - notification.success({ - message: 'Success', - description: ( - - - Transaction successful. Click to view in block - explorer. - - - ), - duration: 3, - icon: , - style: { width: '100%' }, - }); + sendTokenNotification(); } catch (e) { passLoadingStatus(false); let message; diff --git a/web/cashtab/src/components/Tokens/CreateTokenForm.js b/web/cashtab/src/components/Tokens/CreateTokenForm.js --- a/web/cashtab/src/components/Tokens/CreateTokenForm.js +++ b/web/cashtab/src/components/Tokens/CreateTokenForm.js @@ -15,9 +15,8 @@ import { SmartButton } from '@components/Common/PrimaryButton'; import { Collapse, Form, Input, Modal, notification } from 'antd'; const { Panel } = Collapse; -import Paragraph from 'antd/lib/typography/Paragraph'; import { TokenParamLabel } from '@components/Common/Atoms'; -import { TokenReceivedNotificationIcon } from '@components/Common/CustomIcons'; +import { createTokenNotification } from '../../hooks/Notifications'; const CreateTokenForm = ({ BCH, @@ -50,9 +49,8 @@ // New Token Decimals const [newTokenDecimals, setNewTokenDecimals] = useState(0); - const [newTokenDecimalsIsValid, setNewTokenDecimalsIsValid] = useState( - true, - ); + const [newTokenDecimalsIsValid, setNewTokenDecimalsIsValid] = + useState(true); const handleNewTokenDecimalsInput = e => { const { value } = e.target; // validation @@ -69,9 +67,8 @@ // New Token Initial Quantity const [newTokenInitialQty, setNewTokenInitialQty] = useState(''); - const [newTokenInitialQtyIsValid, setNewTokenInitialQtyIsValid] = useState( - null, - ); + const [newTokenInitialQtyIsValid, setNewTokenInitialQtyIsValid] = + useState(null); const handleNewTokenInitialQtyInput = e => { const { value } = e.target; // validation @@ -83,10 +80,8 @@ // New Token document URL const [newTokenDocumentUrl, setNewTokenDocumentUrl] = useState(''); // Start with this as true, field is not required - const [ - newTokenDocumentUrlIsValid, - setNewTokenDocumentUrlIsValid, - ] = useState(true); + const [newTokenDocumentUrlIsValid, setNewTokenDocumentUrlIsValid] = + useState(true); const handleNewTokenDocumentUrlInput = e => { const { value } = e.target; @@ -140,19 +135,7 @@ currency.defaultFee, configObj, ); - - notification.success({ - message: 'Success', - description: ( - - - Token created! Click to view in block explorer. - - - ), - icon: , - style: { width: '100%' }, - }); + createTokenNotification(); } catch (e) { // Set loading to false here as well, as balance may not change depending on where error occured in try loop passLoadingStatus(false); diff --git a/web/cashtab/src/hooks/Notifications.js b/web/cashtab/src/hooks/Notifications.js new file mode 100644 --- /dev/null +++ b/web/cashtab/src/hooks/Notifications.js @@ -0,0 +1,92 @@ +import * as React from 'react'; +import { notification } from 'antd'; +import { CashReceivedNotificationIcon } from '@components/Common/CustomIcons'; +import Paragraph from 'antd/lib/typography/Paragraph'; +import { TokenReceivedNotificationIcon } from '@components/Common/CustomIcons'; +import { currency } from '@components/Common/Ticker'; + +const sendXecNotification = link => { + notification.success({ + message: 'Success', + description: ( + + + Transaction successful. Click to view in block explorer. + + + ), + duration: 3, + icon: , + style: { width: '100%' }, + }); +}; + +const createTokenNotification = link => { + notification.success({ + message: 'Success', + description: ( + + + Token created! Click to view in block explorer. + + + ), + icon: , + style: { width: '100%' }, + }); +}; + +const sendTokenNotification = link => { + notification.success({ + message: 'Success', + description: ( + + + Transaction successful. Click to view in block explorer. + + + ), + duration: 3, + icon: , + style: { width: '100%' }, + }); +}; + +const transactionReceivedNotification = (balances, previousBalances) => { + notification.success({ + message: 'Transaction received', + description: ( + + +{' '} + {parseFloat( + Number( + balances.totalBalance - previousBalances.totalBalance, + ).toFixed(currency.cashDecimals), + ).toLocaleString()}{' '} + {currency.ticker}{' '} + {cashtabSettings && + cashtabSettings.fiatCurrency && + `(${ + currency.fiatCurrencies[cashtabSettings.fiatCurrency] + .symbol + }${( + Number( + balances.totalBalance - + previousBalances.totalBalance, + ) * fiatPrice + ).toFixed( + currency.cashDecimals, + )} ${cashtabSettings.fiatCurrency.toUpperCase()})`} + + ), + duration: 3, + icon: , + style: { width: '100%' }, + }); +}; +export { + sendXecNotification, + createTokenNotification, + sendTokenNotification, + transactionReceivedNotification, +}; diff --git a/web/cashtab/src/hooks/useWallet.js b/web/cashtab/src/hooks/useWallet.js --- a/web/cashtab/src/hooks/useWallet.js +++ b/web/cashtab/src/hooks/useWallet.js @@ -21,6 +21,7 @@ CashReceivedNotificationIcon, TokenReceivedNotificationIcon, } from '@components/Common/CustomIcons'; +import { transactionReceivedNotification } from './Notifications'; const useWallet = () => { const [wallet, setWallet] = useState(false); @@ -900,38 +901,7 @@ .minus(previousBalances.totalBalance) .gt(0) ) { - notification.success({ - message: 'Transaction received', - description: ( - - +{' '} - {parseFloat( - Number( - balances.totalBalance - - previousBalances.totalBalance, - ).toFixed(currency.cashDecimals), - ).toLocaleString()}{' '} - {currency.ticker}{' '} - {cashtabSettings && - cashtabSettings.fiatCurrency && - `(${ - currency.fiatCurrencies[ - cashtabSettings.fiatCurrency - ].symbol - }${( - Number( - balances.totalBalance - - previousBalances.totalBalance, - ) * fiatPrice - ).toFixed( - currency.cashDecimals, - )} ${cashtabSettings.fiatCurrency.toUpperCase()})`} - - ), - duration: 3, - icon: , - style: { width: '100%' }, - }); + transactionReceivedNotification(); } // Parse for incoming eToken transactions @@ -977,9 +947,8 @@ // Calculate amount received //console.log(`receivedTokenObject:`, tokens[receivedTokenObjectIndex]); - const receivedSlpQty = tokens[ - receivedTokenObjectIndex - ].balance.toString(); + const receivedSlpQty = + tokens[receivedTokenObjectIndex].balance.toString(); const receivedSlpTicker = tokens[receivedTokenObjectIndex].info.tokenTicker; const receivedSlpName =