Skip to content
Snippets Groups Projects
Commit 1d8ab85b authored by Yoan VALLET's avatar Yoan VALLET
Browse files

feat: move getLagDays to utils

parent 9e6ef557
No related branches found
No related tags found
2 merge requests!106v0.2.1,!100Features/us216 change challenge comparison period
......@@ -15,6 +15,7 @@ import { currentChallengeState } from 'atoms/challenge.state'
import ChallengeService from 'services/challenge.service'
import ConsumptionService from 'services/consumption.service'
import UserProfileService from 'services/userProfile.service'
import { getLagDays } from 'utils/date'
import CozyBar from 'components/Header/CozyBar'
import Header from 'components/Header/Header'
......@@ -114,7 +115,7 @@ const AvailableChallengeDetailsView: React.FC<AvailableChallengeDetailsViewProps
useEffect(() => {
let subscribed = true
async function checkPreviousData() {
const lag = challengeService.getLagDays(fluidTypes)
const lag = getLagDays(fluidTypes)
const firstDayOfPreviousPeriod = challengeState.duration.days
const timePeriod = {
startDate: DateTime.local()
......
import React from 'react'
import { useClient } from 'cozy-client'
import { DateTime, Interval } from 'luxon'
import { UserChallenge } from 'models'
import ChallengeService from 'services/challenge.service'
import { getLagDays } from 'utils/date'
interface ChallengeTimelineViewProps {
challenge: UserChallenge
......@@ -12,14 +11,11 @@ interface ChallengeTimelineViewProps {
const ChallengeTimeline: React.FC<ChallengeTimelineViewProps> = ({
challenge,
}: ChallengeTimelineViewProps) => {
const client = useClient()
const challengeService = new ChallengeService(client)
const viewingDate = () => {
if (challenge && challenge.challengeType) {
const startingDate = challenge.startingDate
return startingDate.plus({
days: challengeService.getLagDays(challenge.fluidTypes),
days: getLagDays(challenge.fluidTypes),
})
} else {
return DateTime.local()
......
import React, { useState, useEffect } from 'react'
import { useI18n } from 'cozy-ui/transpiled/react'
import { useClient } from 'cozy-client'
import { DateTime } from 'luxon'
import { UserChallenge } from 'models'
import ChallengeService from 'services/challenge.service'
import { getLagDays } from 'utils/date'
interface ChallengeViewingDateProps {
challenge: UserChallenge
......@@ -14,8 +13,6 @@ const ChallengeViewingDate: React.FC<ChallengeViewingDateProps> = ({
challenge,
}: ChallengeViewingDateProps) => {
const { t } = useI18n()
const client = useClient()
const challengeService = new ChallengeService(client)
const [firstDateWithData, setFirstDateWithData] = useState<DateTime | null>(
null
)
......@@ -28,7 +25,7 @@ const ChallengeViewingDate: React.FC<ChallengeViewingDateProps> = ({
useEffect(() => {
if (challenge) {
const lag = challengeService.getLagDays(challenge.fluidTypes)
const lag = getLagDays(challenge.fluidTypes)
setFirstDateWithData(
challenge.startingDate.plus({
days: lag,
......
......@@ -18,6 +18,7 @@ import { TimeStep } from 'enum/timeStep.enum'
import { Ecogesture, UserProfile, UserChallenge, ChallengeType } from 'models'
import ConsumptionService from 'services/consumption.service'
import PerformanceIndicatorService from 'services/performanceIndicator.service'
import { getLagDays } from 'utils/date'
export default class ChallengeService {
private readonly _client: Client
......@@ -112,7 +113,7 @@ export default class ChallengeService {
challenge: UserChallenge,
challengeFluidTypes: FluidType[]
): Promise<number> {
const lagDays = this.getLagDays(challengeFluidTypes)
const lagDays = getLagDays(challengeFluidTypes)
if (DateTime.local() > challenge.startingDate.plus({ days: lagDays })) {
const consumptionService = new ConsumptionService(this._client)
const startDate = challenge.startingDate
......@@ -188,7 +189,7 @@ export default class ChallengeService {
fluidTypes: FluidType[],
typeChallenge: TypeChallenge
) {
const lagDays = this.getLagDays(fluidTypes)
const lagDays = getLagDays(fluidTypes)
const endDate =
typeChallenge === TypeChallenge.CHALLENGE
? endingDate.plus({ days: lagDays }).startOf('day')
......@@ -294,35 +295,13 @@ export default class ChallengeService {
public getViewingDate = (challenge: UserChallenge): DateTime => {
const startingDate = challenge.startingDate
if (challenge && challenge.fluidTypes && challenge.fluidTypes.length > 0) {
const lagDays = this.getLagDays(challenge.fluidTypes)
const lagDays = getLagDays(challenge.fluidTypes)
return startingDate.plus({ days: lagDays })
} else {
return startingDate
}
}
/*
* Return the diff of day which represent
* the possible calculation of data based on configured fluidTypes
*/
public getLagDays = (fluidTypes: FluidType[]): number => {
if (
fluidTypes &&
fluidTypes.length > 0 &&
fluidTypes.includes(FluidType.WATER)
) {
return 3
} else if (
fluidTypes &&
fluidTypes.length > 0 &&
fluidTypes.includes(FluidType.GAS)
) {
return 2
} else {
return 1
}
}
public async getAllChallengeTypeEntities(): Promise<
ChallengeTypeEntity[] | null
> {
......
import { DateTime } from 'luxon'
import { TimeStep } from 'enum/timeStep.enum'
import { FluidType } from 'enum/fluid.enum'
import { TimePeriod } from 'models'
import { UserChallenge } from 'models'
......@@ -52,3 +53,25 @@ export const convertDateByTimeStep = (
return ''
}
}
/*
* Return the diff of day which represent
* the possible calculation of data based on configured fluidTypes
*/
export const getLagDays = (fluidTypes: FluidType[]): number => {
if (
fluidTypes &&
fluidTypes.length > 0 &&
fluidTypes.includes(FluidType.WATER)
) {
return 3
} else if (
fluidTypes &&
fluidTypes.length > 0 &&
fluidTypes.includes(FluidType.GAS)
) {
return 2
} else {
return 1
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment