From f3bdfc62cc6d01a62d06dea9a0c56cba1bd66ee6 Mon Sep 17 00:00:00 2001 From: Yoan VALLET <ext.sopra.yvallet@grandlyon.com> Date: Wed, 2 Sep 2020 14:41:09 +0200 Subject: [PATCH] feat: move date utils to a specific file --- .../IndicatorContainerSwitcher.tsx | 2 +- .../MultiFluidIndicatorsContainer.tsx | 2 +- .../SingleFluidIndicatorsContainer.tsx | 2 +- .../OngoingChallengeDetailsViewContainer.tsx | 2 +- .../consumptionDataFormatterService.ts | 2 +- src/utils/date.ts | 53 +++++++++++++++++++ src/utils/utils.ts | 53 ------------------- 7 files changed, 58 insertions(+), 58 deletions(-) create mode 100644 src/utils/date.ts diff --git a/src/components/ContainerComponents/IndicatorsContainer/IndicatorContainerSwitcher.tsx b/src/components/ContainerComponents/IndicatorsContainer/IndicatorContainerSwitcher.tsx index 1c742ebe5..835686a75 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 eec3a6f67..b86527019 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 3147c6643..d797ff132 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 ff39bebd9..32aca37a9 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 de3c0f75e..cba6bdb20 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 000000000..aac28530c --- /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 cddcb6dcf..3ac51c04d 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 '' - } -} -- GitLab