import React, { useState } from 'react'; import { useDispatch, useSelector } from 'react-redux'; import { useTranslation } from 'react-i18next'; import TextField from '@material-ui/core/TextField'; import { makeStyles } from '@material-ui/styles'; import { CertificateSource, requestPublishForSale, requestWithdrawCertificate, EnergyFormatter, fromUsersSelectors } from '@energyweb/origin-ui-core'; import { useOriginConfiguration } from '../../utils/configuration'; import { InboxPanel, TabContent, SelectedInboxList, IInboxCertificateData } from '../../components/certificates/inbox'; export function ExchangeInboxPage(): JSX.Element { const dispatch = useDispatch(); const { t } = useTranslation(); const user = useSelector(fromUsersSelectors.getUserOffchain); const [price, setPrice] = useState(0); async function publishForSale(certs: IInboxCertificateData[], callback: () => void) { certs.forEach((certificate) => { dispatch( requestPublishForSale({ certificateId: certificate.id, amount: certificate.energy, price: Math.round((price + Number.EPSILON) * 100), callback: () => { callback(); }, source: certificate.source, assetId: certificate.assetId }) ); }); } async function withdraw(certs: IInboxCertificateData[], callback: () => void) { const address = user.organization?.blockchainAccountAddress; certs.forEach((certificate) => { const assetId = certificate.assetId; const amount = certificate.energy.toString(); dispatch( requestWithdrawCertificate({ assetId, address, amount, callback: () => { callback(); } }) ); }); } const configuration = useOriginConfiguration(); const simpleTextColor = configuration?.styleConfig?.SIMPLE_TEXT_COLOR; const useStyles = makeStyles({ text_1: { fontSize: '16px', color: simpleTextColor }, text_2: { fontSize: '14px', color: simpleTextColor, opacity: '.5' } }); const classes = useStyles(); return ( {({ tabIndex, selectedCerts, getSelectedItems, setEnergy, getSelectedCertificates, updateView, totalVolume }) => { return ( <> {tabIndex === 0 && ( publishForSale(getSelectedCertificates(), updateView) } selectedCerts={selectedCerts} >
{t('certificate.info.totalVolume')}:{' '}
{EnergyFormatter.format(totalVolume, true)}
{ const newValue = parseFloat(ev.target.value); if (!isNaN(newValue)) setPrice(newValue); }} />
{t('certificate.info.totalPrice')}:{' '}
${EnergyFormatter.format(totalVolume.mul(price))}
)} {tabIndex === 1 && ( withdraw(getSelectedCertificates(), updateView)} selectedCerts={selectedCerts} >
{t('certificate.info.totalVolume')}:{' '}
{EnergyFormatter.format(totalVolume, true)}
)} ); }}
); }