import React, { useState, useEffect } from 'react'; import { useSelector, useDispatch } from 'react-redux'; import { useTranslation } from 'react-i18next'; import { BigNumber } from 'ethers'; import CurrencyTextField from '@unicef/material-ui-currency-textfield'; import { List, ListItem, Grid, Checkbox, Button, FormControlLabel, Box, useTheme } from '@material-ui/core'; import { Unit } from '@energyweb/utils-general'; import { ICertificateViewItem, reloadCertificates, formatCurrencyComplete, EnergyFormatter, fromGeneralSelectors } from '@energyweb/origin-ui-core'; import { getEnvironment } from '../../features/general'; import { createBundle } from '../../features/bundles'; import { BundleItemDTO } from '../../utils/exchange'; import { BundleItemEdit, IBundledCertificate } from './BundleItemEdit'; import { IOriginTypography } from '../../types/typography'; import { MyDevice } from '../../types'; import { useOriginConfiguration } from '../../utils/configuration'; import { deviceById } from '../../utils/device'; interface IOwnProps { certificatesToBundle: ICertificateViewItem[]; callback: () => void; devices: MyDevice[]; } export const SelectedForSale = (props: IOwnProps) => { const { callback, devices } = props; const [certificatesToBundle, setCertificatesToBundle] = useState([]); const [price, setPrice] = useState(0); const [sellAsBundle, setSellAsBundle] = useState(false); const environment = useSelector(getEnvironment); const currency = useSelector(fromGeneralSelectors.getCurrencies)[0]; const { t } = useTranslation(); const dispatch = useDispatch(); const configuration = useOriginConfiguration(); const originBgColor = configuration?.styleConfig?.MAIN_BACKGROUND_COLOR; const fontSizeMd = ((useTheme().typography as unknown) as IOriginTypography)?.fontSizeMd; const totalVolume = () => certificatesToBundle.reduce( (total, { energy: { volumeToBundle } }) => total.add(volumeToBundle), BigNumber.from(0) ); useEffect(() => { setCertificatesToBundle( props.certificatesToBundle.map((c) => ({ ...c, energy: { ...c.energy, volumeToBundle: c.energy.publicVolume } })) ); }, [props.certificatesToBundle]); const handleItemEdit = (cert: IBundledCertificate) => { setCertificatesToBundle(certificatesToBundle.map((c) => (c.id === cert.id ? cert : c))); }; async function requestCreateBundle() { const items: BundleItemDTO[] = []; for (const cert of certificatesToBundle) { const { assetId, energy: { volumeToBundle } } = cert; items.push({ assetId, volume: volumeToBundle.toString() }); } dispatch( createBundle({ bundleDTO: { price: Number((price * 100).toFixed(0)), items }, callback }) ); dispatch(reloadCertificates()); } return ( SELECTED FOR SALE {certificatesToBundle.length > 0 && ( {certificatesToBundle.map((cert, index, arr) => { return ( ); })} )} {t('bundle.properties.totalVolume')} {EnergyFormatter.format(totalVolume(), true)} setPrice(value)} minimumValue="0" /> Total Price {formatCurrencyComplete( (totalVolume().toNumber() / Unit[EnergyFormatter.displayUnit]) * price, currency )} setSellAsBundle(!sellAsBundle)} /> } label={ {t('bundle.actions.sellAsBundle')} } > ); };