Newer
Older
import Coins from 'assets/icons/ico/coins.svg'
import StyledIcon from 'components/CommonKit/Icon/StyledIcon'

Rémi PAILHAREY
committed
import { useClient } from 'cozy-client'

Rémi PAILHAREY
committed
import { TimeStep } from 'enum/timeStep.enum'
import React, { useCallback, useEffect, useState } from 'react'

Rémi PAILHAREY
committed
import { useSelector } from 'react-redux'
import ConsumptionService from 'services/consumption.service'
import ConverterService from 'services/converter.service'

Rémi PAILHAREY
committed
import { AppStore } from 'store'
import { formatNumberValues } from 'utils/utils'
import './totalConsumption.scss'

Rémi PAILHAREY
committed
const TotalConsumption = ({ fluidType }: { fluidType: FluidType }) => {
const { currentTimeStep, showCompare, currentDatachart } = useSelector(

Rémi PAILHAREY
committed
(state: AppStore) => state.ecolyo.chart
)
const client = useClient()
const [totalValue, setTotalValue] = useState<string>()
const [previousTotalValue, setPreviousTotalValue] = useState<string>()

Rémi PAILHAREY
committed
const computeTotal = useCallback(
async (
dataload: Dataload[],
setState: React.Dispatch<React.SetStateAction<string | undefined>>
) => {

Rémi PAILHAREY
committed
const consumptionService = new ConsumptionService(client)
const activateHalfHourLoad =
fluidType === FluidType.ELECTRICITY
? await consumptionService.checkDoctypeEntries(
FluidType.ELECTRICITY,
TimeStep.HALF_AN_HOUR
)
: false

Rémi PAILHAREY
committed
const converterService = new ConverterService()

Hugo SUBTIL
committed
let totalPrice = 0
dataload.forEach(data => {

Hugo SUBTIL
committed
if (data.value !== -1) {
total += data.value
totalPrice += converterService.LoadToEuro(
data.value,
fluidType,
data.price
)
}

Rémi PAILHAREY
committed

Rémi PAILHAREY
committed
total <= 0 ||
(!activateHalfHourLoad &&
currentTimeStep === TimeStep.HALF_AN_HOUR &&
fluidType === FluidType.ELECTRICITY)
) {
displayedValue = '-----'
} else if (fluidType === FluidType.MULTIFLUID) {
displayedValue = formatNumberValues(total).toString()
} else if (totalPrice <= 0) {
displayedValue = formatNumberValues(
converterService.LoadToEuro(total, fluidType)
).toString()
} else {
displayedValue = formatNumberValues(totalPrice).toString()
}

Rémi PAILHAREY
committed
setState(displayedValue)
},
[client, currentTimeStep, fluidType]
)

Rémi PAILHAREY
committed
useEffect(() => {
computeTotal(currentDatachart.actualData, setTotalValue)
if (currentDatachart.comparisonData) {
computeTotal(currentDatachart.comparisonData, setPreviousTotalValue)
}
}, [currentDatachart, fluidType, currentTimeStep, client, computeTotal])

Rémi PAILHAREY
committed
{showCompare ? (
<StyledIcon className="pile-icon" icon={Coins} size={48} />
) : (
<StyledIcon className="pile-icon" icon={Coin} size={48} />
)}
<span className="euro-value">{totalValue}</span>
<span className="euro-symbol"> €</span>
</div>
{showCompare && (
<div className="compare">
<span className="euro-value">{previousTotalValue}</span>
<span className="euro-symbol"> €</span>
</div>
)}
</div>
)
}
export default TotalConsumption