diff --git a/src/components/ContainerComponents/IndicatorsContainer/IndicatorContainerSwitcher.tsx b/src/components/ContainerComponents/IndicatorsContainer/IndicatorContainerSwitcher.tsx index 1c742ebe59155530163b1fff4cebe848432e1262..835686a75b160d8ac678411a5d47cd8751cf1a3a 100644 --- a/src/components/ContainerComponents/IndicatorsContainer/IndicatorContainerSwitcher.tsx +++ b/src/components/ContainerComponents/IndicatorsContainer/IndicatorContainerSwitcher.tsx @@ -5,7 +5,7 @@ import { IPerformanceIndicator, ITimePeriod, } from 'services/dataConsumptionContracts' -import { convertDateByTimeStep } from 'utils/utils' +import { convertDateByTimeStep } from 'utils/date' import FluidConfigService from 'services/fluidConfigService' import FluidPerformanceIndicator from 'components/ContentComponents/PerformanceIndicator/FluidPerformanceIndicator' import KonnectorViewer from 'components/ContentComponents/KonnectorViewer/KonnectorViewer' diff --git a/src/components/ContainerComponents/IndicatorsContainer/MultiFluidIndicatorsContainer.tsx b/src/components/ContainerComponents/IndicatorsContainer/MultiFluidIndicatorsContainer.tsx index eec3a6f671745dd9364b65dad8c8fa0f4dbdac42..b8652701919cc875a397a7451c237855109d015b 100644 --- a/src/components/ContainerComponents/IndicatorsContainer/MultiFluidIndicatorsContainer.tsx +++ b/src/components/ContainerComponents/IndicatorsContainer/MultiFluidIndicatorsContainer.tsx @@ -12,7 +12,7 @@ import ConsumptionPeriodSelector from 'services/consumptionPeriodSelectorService import ConsumptionDataManager from 'services/consumptionDataManagerService' import PerformanceIndicatorAggregateCalculator from 'services/performanceIndicatorAggregateCalculatorService' import { translate } from 'cozy-ui/react/I18n' -import { convertDateByTimeStep } from 'utils/utils' +import { convertDateByTimeStep } from 'utils/date' interface MultiFluidIndicatorsContainerProps { timeStep: TimeStep diff --git a/src/components/ContainerComponents/IndicatorsContainer/SingleFluidIndicatorsContainer.tsx b/src/components/ContainerComponents/IndicatorsContainer/SingleFluidIndicatorsContainer.tsx index 3147c6643490d5ccf39e33617130d908a4d10193..d797ff132899a3d54bce813c850995ff28277fa1 100644 --- a/src/components/ContainerComponents/IndicatorsContainer/SingleFluidIndicatorsContainer.tsx +++ b/src/components/ContainerComponents/IndicatorsContainer/SingleFluidIndicatorsContainer.tsx @@ -12,7 +12,7 @@ import ConsumptionPeriodSelector from 'services/consumptionPeriodSelectorService import ConsumptionDataManager from 'services/consumptionDataManagerService' import PerformanceIndicatorAggregateCalculator from 'services/performanceIndicatorAggregateCalculatorService' import { translate } from 'cozy-ui/react/I18n' -import { convertDateByTimeStep } from 'utils/utils' +import { convertDateByTimeStep } from 'utils/date' import RedirectionMiniCard from 'components/ContentComponents/Card/RedirectionMiniCard' interface SingleFluidIndicatorsContainerProps { diff --git a/src/components/ContainerComponents/ViewContainer/OngoingChallengeDetailsViewContainer.tsx b/src/components/ContainerComponents/ViewContainer/OngoingChallengeDetailsViewContainer.tsx index ff39bebd96360d30d8eccd0fceb6e05d2a4f15b3..32aca37a92b1cbd124be6f490ea5f103540f8ceb 100644 --- a/src/components/ContainerComponents/ViewContainer/OngoingChallengeDetailsViewContainer.tsx +++ b/src/components/ContainerComponents/ViewContainer/OngoingChallengeDetailsViewContainer.tsx @@ -17,7 +17,7 @@ import EcogestureCard from 'components/ContentComponents/EcogestureCard/Ecogestu import ChallengeManager from 'services/challengeDataManagerService' import { Client, withClient } from 'cozy-client' import StyledStopButton from 'components/CommonKit/Button/StyledStopButton' -import { formatCompareChallengeDate } from 'utils/utils' +import { formatCompareChallengeDate } from 'utils/date' import OngoingChallengePile from 'components/ContentComponents/Challenge/OngoingChallengePile' import OngoingChallengeViewingDate from 'components/ContentComponents/Challenge/OngoingChallengeViewingDate' import FollowChallengeTimeline from 'components/ContentComponents/Challenge/FollowChallengeTimeline' diff --git a/src/services/consumptionDataFormatterService.ts b/src/services/consumptionDataFormatterService.ts index de3c0f75e6c9088c206eb5a156ce5242caf0b905..cba6bdb209af663b38134d0590e3ba9aa3a73407 100644 --- a/src/services/consumptionDataFormatterService.ts +++ b/src/services/consumptionDataFormatterService.ts @@ -1,6 +1,6 @@ /* eslint-disable @typescript-eslint/interface-name-prefix */ import { TimeStep, ITimePeriod, IDataload } from './dataConsumptionContracts' -import { compareDates } from 'utils/utils' +import { compareDates } from 'utils/date' import { compareStepDate } from './dateChartService' //import _ from 'lodash' diff --git a/src/utils/date.ts b/src/utils/date.ts new file mode 100644 index 0000000000000000000000000000000000000000..aac28530cb9be702d9da5a34cc5cf169057e918e --- /dev/null +++ b/src/utils/date.ts @@ -0,0 +1,53 @@ +import { DateTime } from 'luxon' +import { ITimePeriod, TimeStep } from '../services/dataConsumptionContracts' +import { UserChallenge } from 'services/dataChallengeContracts' + +export function compareDates(dateA: DateTime, dateB: DateTime) { + return dateA < dateB ? -1 : 1 +} + +export const formatCompareChallengeDate = (challenge: UserChallenge) => { + let durationTimeStep = '' + let duration = 0 + if (challenge && challenge.challengeType) { + durationTimeStep = Object.keys(challenge.challengeType.duration)[0] + duration = (challenge.challengeType.duration as any)[durationTimeStep] + } + const delay = { [durationTimeStep]: -duration } + const startDate = challenge.startingDate.plus(delay) + const endDate = challenge.startingDate.plus({ days: -1 }).endOf('day') + return ` (du ${startDate.toFormat('dd/MM')} au ${endDate.toFormat('dd/MM')})` +} + +export const convertDateByTimeStep = ( + timeperiod: ITimePeriod | null, + timeStep: TimeStep, + header = false +): string => { + if (!timeperiod) return '' + switch (timeStep) { + case TimeStep.HALF_AN_HOUR: + return ' du ' + timeperiod.startDate.toFormat('dd/MM') + + case TimeStep.HOUR: + return ' du ' + timeperiod.startDate.toFormat('dd/MM') + + case TimeStep.DAY: + return ( + (!header ? 'semaine ' : '') + + ' du ' + + timeperiod.startDate.toFormat('dd/MM') + + ' au ' + + timeperiod.endDate.toFormat('dd/MM') + ) + + case TimeStep.MONTH: + return ' du ' + timeperiod.startDate.toFormat('MM/y') + + case TimeStep.YEAR: + return ' de ' + timeperiod.startDate.toFormat('y') + + default: + return '' + } +} diff --git a/src/utils/utils.ts b/src/utils/utils.ts index cddcb6dcfd63e535ea7182a9abe69e79ca606e66..3ac51c04d7ede5e08ee4dec320757ccccd7d2303 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -11,9 +11,6 @@ import ElecParamIcon from 'assets/icons/visu/elec-param.svg' import WaterParamIcon from 'assets/icons/visu/water-param.svg' import GasParamIcon from 'assets/icons/visu/gas-param.svg' import { FluidType } from '../enum/fluid.enum' -import { DateTime } from 'luxon' -import { ITimePeriod, TimeStep } from '../services/dataConsumptionContracts' -import { UserChallenge } from 'services/dataChallengeContracts' /** * Return an icon corresponding to FuildType enum @@ -83,53 +80,3 @@ export function formatNumberValues(value: number) { return '--,--' } } - -export function compareDates(dateA: DateTime, dateB: DateTime) { - return dateA < dateB ? -1 : 1 -} - -export const formatCompareChallengeDate = (challenge: UserChallenge) => { - let durationTimeStep = '' - let duration = 0 - if (challenge && challenge.challengeType) { - durationTimeStep = Object.keys(challenge.challengeType.duration)[0] - duration = (challenge.challengeType.duration as any)[durationTimeStep] - } - const delay = { [durationTimeStep]: -duration } - const startDate = challenge.startingDate.plus(delay) - const endDate = challenge.startingDate.plus({ days: -1 }).endOf('day') - return ` (du ${startDate.toFormat('dd/MM')} au ${endDate.toFormat('dd/MM')})` -} - -export const convertDateByTimeStep = ( - timeperiod: ITimePeriod | null, - timeStep: TimeStep, - header = false -): string => { - if (!timeperiod) return '' - switch (timeStep) { - case TimeStep.HALF_AN_HOUR: - return ' du ' + timeperiod.startDate.toFormat('dd/MM') - - case TimeStep.HOUR: - return ' du ' + timeperiod.startDate.toFormat('dd/MM') - - case TimeStep.DAY: - return ( - (!header ? 'semaine ' : '') + - ' du ' + - timeperiod.startDate.toFormat('dd/MM') + - ' au ' + - timeperiod.endDate.toFormat('dd/MM') - ) - - case TimeStep.MONTH: - return ' du ' + timeperiod.startDate.toFormat('MM/y') - - case TimeStep.YEAR: - return ' de ' + timeperiod.startDate.toFormat('y') - - default: - return '' - } -}