diff --git a/src/components/Challenge/ChallengeViewingDate.tsx b/src/components/Challenge/ChallengeViewingDate.tsx index f8ae819877f66415da00b4633ece54d958f4e2d6..037ffbfba637a651ce894ebc66d447a09446f75c 100644 --- a/src/components/Challenge/ChallengeViewingDate.tsx +++ b/src/components/Challenge/ChallengeViewingDate.tsx @@ -28,12 +28,12 @@ const ChallengeViewingDate: React.FC<ChallengeViewingDateProps> = ({ const lag = getLagDays(challenge.fluidTypes) setFirstDateWithData( challenge.startingDate.plus({ - days: lag, + days: lag - 1, }) ) setLastDateWithData( challenge.endingDate.plus({ - days: lag, + days: lag - 1, }) ) } diff --git a/src/components/Challenge/OngoingChallengeDetailsView.tsx b/src/components/Challenge/OngoingChallengeDetailsView.tsx index 9e7c65833c2e11b3070423d82109ca3e931a00a1..cd9ddaf7c7286c1639d6d89363d3a36f2231a876 100644 --- a/src/components/Challenge/OngoingChallengeDetailsView.tsx +++ b/src/components/Challenge/OngoingChallengeDetailsView.tsx @@ -9,6 +9,7 @@ import { TypeChallenge } from 'enum/challenge.enum' import { ScreenType } from 'enum/screen.enum' import { UserChallenge, UserProfile } from 'models' import { userProfileState } from 'atoms/userProfile.state' +import { fluidTypeState } from 'atoms/fluidState.state' import { screenTypeState } from 'atoms/screenType.state' import { currentChallengeState } from 'atoms/challenge.state' import ChallengeService from 'services/challenge.service' @@ -38,6 +39,7 @@ const OngoingChallengeDetailsView: React.FC<OngoingChallengeDetailsViewProps> = const { t } = useI18n() const client = useClient() const screenType = useRecoilValue(screenTypeState) + const fluidTypes = useRecoilValue(fluidTypeState) const [userProfile, setUserProfile] = useRecoilState<UserProfile>( userProfileState ) @@ -51,6 +53,7 @@ const OngoingChallengeDetailsView: React.FC<OngoingChallengeDetailsViewProps> = const [maxEnergy, setMaxEnergy] = useState<number | null>(0) const challengeService = new ChallengeService(client) + const userProfileService = new UserProfileService(client) const defineHeaderHeight = (height: number) => { setHeaderHeight(height) @@ -58,7 +61,6 @@ const OngoingChallengeDetailsView: React.FC<OngoingChallengeDetailsViewProps> = const updateUserProfileNotification = useCallback( async (ecogestureList: string[]) => { - const userProfileService = new UserProfileService(client) await userProfileService .updateUserProfile({ notificationEcogesture: ecogestureList }) .then(updatedUserProfile => { diff --git a/src/components/Splash/SplashRoot.tsx b/src/components/Splash/SplashRoot.tsx index a0f4711934e7cf5a221a9ebe1ecff28c3aaf1832..35a6583ebb67ea341c4c280d459c99595be4bf9e 100644 --- a/src/components/Splash/SplashRoot.tsx +++ b/src/components/Splash/SplashRoot.tsx @@ -79,16 +79,13 @@ const SplashRoot = ({ } setUserProfile(profile) await initializationService.initUserChallenge() - // const fluidTypes = await initializationService.initFluidTypes() - // setFluidTypeState(fluidTypes) const fluidStatus = await initializationService.initFluidStatus() setFluidStatusState(fluidStatus) const currentChallenge = await initializationService.initCurrentChallenge() if (currentChallenge) { setCurrentChallengeState(currentChallenge) const isChallengeOver = await initializationService.isCurrentChallengeOver( - currentChallenge, - fluidTypes + currentChallenge ) setChallengeNotificationState(isChallengeOver) } diff --git a/src/services/challenge.service.ts b/src/services/challenge.service.ts index 5b51feb4c9de26f818339e7ff7fde150d60a1950..2cfd3e7c37734bb6aea226654a472245001519cf 100644 --- a/src/services/challenge.service.ts +++ b/src/services/challenge.service.ts @@ -18,7 +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' +import { getCompareChallengePeriod, getLagDays } from 'utils/date' export default class ChallengeService { private readonly _client: Client @@ -61,16 +61,7 @@ export default class ChallengeService { challenge: UserChallenge, challengeFluidTypes: FluidType[] ): Promise<number> { - 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') - const period = { startDate, endDate } + const period = getCompareChallengePeriod(challenge, challengeFluidTypes) try { if (challenge && challenge.challengeType) { const consumptionService = new ConsumptionService(this._client) @@ -89,6 +80,7 @@ export default class ChallengeService { } return -1 } catch (error) { + console.log(error) return -1 } } @@ -103,6 +95,7 @@ export default class ChallengeService { await this.updateChallenge(challenge.id, { maxEnergy: maxEnergy }) return maxEnergy } catch (error) { + console.log(error) return -1 } } @@ -140,6 +133,7 @@ export default class ChallengeService { } } } catch (error) { + console.log(error) return 0 } } @@ -160,6 +154,7 @@ export default class ChallengeService { return spentEnergy } catch (error) { + console.log(error) return 0 } } @@ -192,7 +187,7 @@ export default class ChallengeService { const lagDays = getLagDays(fluidTypes) const endDate = typeChallenge === TypeChallenge.CHALLENGE - ? endingDate.plus({ days: lagDays }).startOf('day') + ? endingDate.plus({ days: lagDays - 1 }).startOf('day') : endingDate if (DateTime.local() > endDate) { return true diff --git a/src/services/initialization.service.ts b/src/services/initialization.service.ts index 8561ef7e19869f1c36aade9df83fb7c807d0784a..694a7f97c3e4890a3e86fc4c3d601f06cff67f6a 100644 --- a/src/services/initialization.service.ts +++ b/src/services/initialization.service.ts @@ -1,5 +1,4 @@ import { Client } from 'cozy-client' -import { DateTime } from 'luxon' import { CHALLENGETYPE_DOCTYPE, ECOGESTURE_DOCTYPE, @@ -19,7 +18,6 @@ import { } from 'doctypes' import { FluidType } from 'enum/fluid.enum' -import { TypeChallenge } from 'enum/challenge.enum' import { FluidStatus, UserChallenge, UserProfile } from 'models' import ChallengeService from 'services/challenge.service' @@ -447,13 +445,12 @@ export default class InitializationService { * else return false */ public async isCurrentChallengeOver( - challenge: UserChallenge, - fluidTypes: FluidType[] + challenge: UserChallenge ): Promise<boolean> { const challengeService = new ChallengeService(this._client) const isOver = await challengeService.isCurrentChallengeOver( challenge, - fluidTypes + challenge.fluidTypes ) return isOver } diff --git a/src/utils/date.ts b/src/utils/date.ts index b61bde9e909d7de7d2fada897bd92bd3e0599fd1..92cd0171282bad1a7e4bed0a98d5ed62d491766d 100644 --- a/src/utils/date.ts +++ b/src/utils/date.ts @@ -8,19 +8,51 @@ export function compareDates(dateA: DateTime, dateB: DateTime) { return dateA < dateB ? -1 : 1 } -export const formatCompareChallengeDate = (challenge: UserChallenge) => { +/* + * 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 + } +} + +export const getCompareChallengePeriod = ( + challenge: UserChallenge, + fluidTypes: FluidType[] +): TimePeriod => { let durationTimeStep = '' let duration = 0 + const lag = getLagDays(fluidTypes) if (challenge && challenge.challengeType) { durationTimeStep = Object.keys(challenge.challengeType.duration)[0] duration = (challenge.challengeType.duration as any)[durationTimeStep] } - const delay = { [durationTimeStep]: -duration } + const delay = { [durationTimeStep]: -duration - lag + 1 } 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')})` + const endDate = challenge.startingDate.plus({ days: -lag }).endOf('day') + return { startDate: startDate, endDate: endDate } } +export const formatCompareChallengeDate = (challenge: UserChallenge) => { + const period = getCompareChallengePeriod(challenge, challenge.fluidTypes) + return ` (du ${period.startDate.toFormat( + 'dd/MM' + )} au ${period.endDate.toFormat('dd/MM')})` +} export const convertDateByTimeStep = ( timeperiod: TimePeriod | null, timeStep: TimeStep, @@ -53,25 +85,3 @@ 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 - } -}