Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import React, { useContext } from 'react'
import { translate } from 'cozy-ui/react/I18n'
import { UserChallenge } from 'services/dataChallengeContracts'
import { DateTime, Interval } from 'luxon'
import { FluidType } from 'enum/fluid.enum'
import ChallengeManager from 'services/challengeDataManagerService'
import { AppContext } from 'components/Contexts/AppContextProvider'
import { Client } from 'cozy-client'
interface FollowChallengeTimelineViewProps {
challenge: UserChallenge
client: Client
}
const FollowChallengeTimeline: React.FC<FollowChallengeTimelineViewProps> = ({
challenge,
client,
}: FollowChallengeTimelineViewProps) => {
const { fluidTypes } = useContext(AppContext)
const challengeManager = new ChallengeManager(client)
const viewingDate = () => {
if (challenge && challenge.challengeType) {
const startingDate = challenge.startingDate
return startingDate.plus({
days: challengeManager.getLagDays(fluidTypes),
})
} else {
return DateTime.local()
}
}
const getListOfDays = () => {
if (challenge && challenge.challengeType) {
const startingDate = DateTime.fromISO(challenge.startingDate.toString())
const listOfChallengeDays = []
let i = 0
while (challenge.challengeType.duration.days > i) {
listOfChallengeDays.push({
letter: startingDate.plus({ days: i }).weekdayShort[0].toUpperCase(),
date: startingDate.plus({ days: i }).day,
})
i++
}
return listOfChallengeDays
} else {
return []
}
}
const getListOfPastDays = () => {
if (Interval.fromDateTimes(viewingDate(), DateTime.local()).isValid) {
if (challenge && challenge.challengeType) {
return Interval.fromDateTimes(viewingDate(), DateTime.local()).count(
'days'
)
} else {
return 0
}
} else {
return 0
}
}
return (
<React.Fragment>
<div className="list-of-days-duration">
{getListOfDays().map((day, index) => (
<div key={index} className="day-solo">
<div className="day-line-label">
<div
className={`date-dash ${
index < getListOfPastDays() ? 'past' : 'futur'
}`}
></div>
<div
className={`date-label ${
index < getListOfPastDays() ? 'past' : 'futur'
}`}
></div>
<div
className={`date-dash ${
index + 1 < getListOfPastDays() ? 'past' : 'futur'
}`}
></div>
</div>
<div className="day-letter">{day.letter}</div>
<div className="day-date">{day.date}</div>
</div>
))}
</div>
</React.Fragment>
)
}
export default translate()(FollowChallengeTimeline)