diff --git a/src/components/Export/__snapshots__/exportLoadingModal.spec.tsx.snap b/src/components/Export/__snapshots__/exportLoadingModal.spec.tsx.snap index 06a7f7c28cb0105bf9f7a5c4b82b63db25a1e8da..75eccb7dc838b92cb8613f13aca93de306ceaf9e 100644 --- a/src/components/Export/__snapshots__/exportLoadingModal.spec.tsx.snap +++ b/src/components/Export/__snapshots__/exportLoadingModal.spec.tsx.snap @@ -903,7 +903,7 @@ exports[`ExportLoadingModal component should be rendered correctly 1`] = ` "root": "btn-secondary-negative", } } - onClick={[Function]} + onClick={[MockFunction]} type="submit" > <ForwardRef(Button) @@ -941,7 +941,7 @@ exports[`ExportLoadingModal component should be rendered correctly 1`] = ` "textSizeSmall": "MuiButton-textSizeSmall", } } - onClick={[Function]} + onClick={[MockFunction]} type="submit" > <WithStyles(ForwardRef(ButtonBase)) @@ -951,7 +951,7 @@ exports[`ExportLoadingModal component should be rendered correctly 1`] = ` disabled={false} focusRipple={true} focusVisibleClassName="Mui-focusVisible" - onClick={[Function]} + onClick={[MockFunction]} type="submit" > <ForwardRef(ButtonBase) @@ -968,7 +968,7 @@ exports[`ExportLoadingModal component should be rendered correctly 1`] = ` disabled={false} focusRipple={true} focusVisibleClassName="Mui-focusVisible" - onClick={[Function]} + onClick={[MockFunction]} type="submit" > <button @@ -976,7 +976,7 @@ exports[`ExportLoadingModal component should be rendered correctly 1`] = ` className="MuiButtonBase-root MuiButton-root btn-secondary-negative MuiButton-text" disabled={false} onBlur={[Function]} - onClick={[Function]} + onClick={[MockFunction]} onDragLeave={[Function]} onFocus={[Function]} onKeyDown={[Function]} diff --git a/src/components/Export/exportLoadingModal.tsx b/src/components/Export/exportLoadingModal.tsx index 516a4a590111256fee86ad08c51baa7b67fc7492..3e6d40f3a77e687544d24e993dad146d88ee971c 100644 --- a/src/components/Export/exportLoadingModal.tsx +++ b/src/components/Export/exportLoadingModal.tsx @@ -1,19 +1,30 @@ import { Button, IconButton } from '@material-ui/core' import Dialog from '@material-ui/core/Dialog' -import CloseIcon from 'assets/icons/ico/close.svg' -import Loader from 'components/Loader/Loader' import { useClient } from 'cozy-client' import { useI18n } from 'cozy-ui/transpiled/react/I18n' import Icon from 'cozy-ui/transpiled/react/Icon' +import FileSaver from 'file-saver' +import React, { useCallback, useEffect } from 'react' +import * as XLSX from 'xlsx' + +import CloseIcon from 'assets/icons/ico/close.svg' +import Loader from 'components/Loader/Loader' import { FluidType } from 'enum/fluid.enum' import { TimeStep } from 'enum/timeStep.enum' -import * as FileSaver from 'file-saver' -import { Datachart, TimePeriod } from 'models' -import React, { useCallback, useEffect } from 'react' +import { Datachart, Dataload, TimePeriod } from 'models' import ConsumptionDataManager from 'services/consumption.service' -import * as XLSX from 'xlsx' +import EnedisMonthlyAnalysisDataService from 'services/enedisMonthlyAnalysisData.service' import './exportLoadingModal.scss' +interface ExportDataRow { + [key: string]: string | number +} + +interface ExportDataSheet { + fluidName: string + data: ExportDataRow[] +} + interface ExportLoadingModalProps { open: boolean handleCloseClick: () => void @@ -34,7 +45,10 @@ const ExportLoadingModal: React.FC<ExportLoadingModalProps> = ({ 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8' const fileExtension = '.xlsx' - const exportToXlsx = (exportDataSheets: any, fileName: string) => { + const exportToXlsx = ( + exportDataSheets: ExportDataSheet[], + fileName: string + ) => { const wb = XLSX.utils.book_new() for (const dataSheet of exportDataSheets) { const ws = XLSX.utils.json_to_sheet(dataSheet.data) @@ -45,8 +59,44 @@ const ExportLoadingModal: React.FC<ExportLoadingModalProps> = ({ FileSaver.saveAs(data, fileName + fileExtension) } - const getExportDataFluid = useCallback( - async (fluidType: FluidType): Promise<any> => { + const buildDataRow = useCallback( + async ( + dataload: Dataload, + fluidType: FluidType + ): Promise<ExportDataRow> => { + const dataRow: ExportDataRow = {} + dataRow[t('export.month')] = dataload.date.month + .toString() + .padStart(2, '0') + dataRow[t('export.year')] = dataload.date.year + dataRow[ + `${t('export.consumption')} (${t( + 'FLUID.' + FluidType[fluidType] + '.UNIT' + )})` + ] = dataload.value + if (fluidType === FluidType.ELECTRICITY) { + const emas = new EnedisMonthlyAnalysisDataService(client) + const maxPowerEntities = await emas.getMaxPowerByDate( + dataload.date.year, + dataload.date.month + ) + if (maxPowerEntities) { + const maxLoad = maxPowerEntities.reduce((max, entity) => { + if (entity.load > max) { + return entity.load + } + return max + }, 0) + dataRow[t('export.maxpower')] = maxLoad + } + } + return dataRow + }, + [client, t] + ) + + const getExportDataSheet = useCallback( + async (fluidType: FluidType): Promise<ExportDataSheet | null> => { const consumptionService = new ConsumptionDataManager(client) const firstDataDate = await consumptionService.fetchAllFirstDateData( [fluidType], @@ -73,45 +123,35 @@ const ExportLoadingModal: React.FC<ExportLoadingModalProps> = ({ true ) - if (dataLoad?.actualData) { - const exportDataFluid: any = {} - exportDataFluid.fluidName = t( - 'FLUID.' + FluidType[fluidType] + '.LABEL' - ) - exportDataFluid.data = [] - - for (const data of dataLoad.actualData) { - if (data.value != -1) { - const dataRow: any = {} - dataRow[t('export.month')] = data.date.month - .toString() - .padStart(2, '0') - dataRow[t('export.year')] = data.date.year - dataRow[ - `${t('export.consumption')} (${t( - 'FLUID.' + FluidType[fluidType] + '.UNIT' - )})` - ] = data.value - exportDataFluid.data.push(dataRow) - } - } - return exportDataFluid + if (!dataLoad?.actualData) return null + + const exportDataSheet: ExportDataSheet = { + fluidName: t('FLUID.' + FluidType[fluidType] + '.LABEL'), + data: [], + } + + for (const data of dataLoad.actualData) { + if (data.value === -1) continue + const dataRow = await buildDataRow(data, fluidType) + exportDataSheet.data.push(dataRow) } - return null + return exportDataSheet }, - [client, t] + [buildDataRow, client, t] ) useEffect(() => { let subscribed = true const date = new Date() - async function exportData(): Promise<void> { + const exportData = async (): Promise<void> => { try { - const exportDataSheets: any[] = [] + const exportDataSheets: ExportDataSheet[] = [] for (const fluidType of selectedFluids) { - const exportDataFluid = await getExportDataFluid(fluidType) - if (exportDataFluid) exportDataSheets.push(exportDataFluid) + const exportDataFluid = await getExportDataSheet(fluidType) + if (exportDataFluid) { + exportDataSheets.push(exportDataFluid) + } } await new Promise(r => setTimeout(r, 2000)) if (subscribed) { @@ -132,7 +172,7 @@ const ExportLoadingModal: React.FC<ExportLoadingModalProps> = ({ return () => { subscribed = false } - }, [client, getExportDataFluid, handleDone, selectedFluids, t]) + }, [getExportDataSheet, handleDone, selectedFluids]) return ( <Dialog @@ -142,13 +182,13 @@ const ExportLoadingModal: React.FC<ExportLoadingModalProps> = ({ handleCloseClick() } }} - aria-labelledby={'accessibility-title'} + aria-labelledby="accessibility-title" classes={{ root: 'modal-root', paper: 'modal-paper', }} > - <div id={'accessibility-title'}> + <div id="accessibility-title"> {t('export.modal_loading.accessibility_title')} </div> @@ -174,9 +214,7 @@ const ExportLoadingModal: React.FC<ExportLoadingModalProps> = ({ </div> <Button aria-label={t('export.modal_loading.button_cancel')} - onClick={() => { - handleCloseClick() - }} + onClick={handleCloseClick} classes={{ root: 'btn-secondary-negative', label: 'text-16-bold', diff --git a/src/locales/fr.json b/src/locales/fr.json index 87addeb078e03c01c04889cf7dcbabfd23fe9695..668cd81d695224d4b773d551132d1c528e607fda 100644 --- a/src/locales/fr.json +++ b/src/locales/fr.json @@ -1178,6 +1178,7 @@ "month": "Mois", "year": "Année", "consumption": "Consommation", + "maxpower": "Puissance max (kVA)", "button_close": "Fermer la fenêtre", "modal_start": { "accessibility_title": "Commencer le téléchargement",