import React, { useState } from 'react'; import { useTranslation } from 'react-i18next'; import { BigNumber } from 'ethers'; import { Formik, Field, Form } from 'formik'; import { Box, useTheme, Grid, ListItemAvatar, Avatar, Button, IconButton, FormControl, InputLabel, Input, InputAdornment, makeStyles } from '@material-ui/core'; import { Edit, HighlightOff } from '@material-ui/icons'; import { energyImageByType, moment, EnergyFormatter, EnergyTypes, useValidation, ICertificateEnergy, ICertificateViewItem, LightenColor } from '@energyweb/origin-ui-core'; import { IOriginTypography } from '../../types/typography'; import { useOriginConfiguration } from '../../utils/configuration'; import { MyDevice } from '../../types'; export interface IBundledCertificateEnergy extends ICertificateEnergy { volumeToBundle: BigNumber; } export interface IBundledCertificate extends ICertificateViewItem { energy: IBundledCertificateEnergy; } interface IOwnProps { certificate: IBundledCertificate; totalVolume: () => BigNumber; onChange: (cert: IBundledCertificate) => void; device: MyDevice; } export const BundleItemEdit = (props: IOwnProps) => { const { t } = useTranslation(); const { certificate, device, onChange, totalVolume } = props; const { creationTime, energy: { publicVolume, volumeToBundle } } = certificate; const [selected, setSelected] = useState(false); const configuration = useOriginConfiguration(); const textColorDefault = configuration?.styleConfig?.TEXT_COLOR_DEFAULT; const originBgColor = configuration?.styleConfig?.MAIN_BACKGROUND_COLOR; const bgDarker = LightenColor(originBgColor, -3); const fontSizeMd = ((useTheme().typography as unknown) as IOriginTypography)?.fontSizeMd; const spacing = useTheme().spacing; const classes = makeStyles(() => ({ formControl: { width: '100%', backgroundColor: bgDarker }, formLabel: { marginTop: spacing(1), marginLeft: spacing(1), transform: `scale(0.9)` }, formInput: { textAlign: 'right' } }))(); const type = device ? (device.deviceType.split(';')[0].toLowerCase() as EnergyTypes) : EnergyTypes.SOLAR; const { Yup } = useValidation(); const validationSchema = Yup.object().shape({ volumeToBundle: Yup.number() .required() .min(1) .max(EnergyFormatter.getValueInDisplayUnit(publicVolume)) .integer() }); return ( setSelected(true) : null} style={{ cursor: !selected ? 'pointer' : 'auto' }} > {device && ( {'province' in device ? device.province : device.address}, {'facilityName' in device ? device.facilityName : device.name} )} {moment.unix(creationTime).format('MMM, YYYY')} {EnergyFormatter.format(volumeToBundle, true)} {((100 * volumeToBundle.toNumber()) / totalVolume().toNumber()).toFixed( 0 )} % {!selected ? ( ) : ( setSelected(false)} style={{ textTransform: 'uppercase', cursor: 'pointer', transform: `scale(0.75)`, fontWeight: 'bold' }} > {t('general.actions.cancel')} )} {selected && ( { setSelected(false); onChange({ ...certificate, energy: { ...certificate.energy, volumeToBundle: EnergyFormatter.getBaseValueFromValueInDisplayUnit( Number(values.volumeToBundle) ) } }); }} initialValues={{ volumeToBundle: EnergyFormatter.getValueInDisplayUnit(volumeToBundle) }} > {(formikProps) => { const { isValid, setFieldValue } = formikProps; return (
{({ field }) => ( {t('bundle.info.editBundleVolume')} setFieldValue( 'volumeToBundle', 0 ) } > } /> )}
); }}
)}
); };