mirror of
https://github.com/theoludwig/p61-project.git
synced 2024-07-17 07:00:12 +02:00
parent
42f5623c92
commit
ca122e9fce
@ -1,18 +1,18 @@
|
|||||||
import { Card, Text } from "react-native-paper"
|
import { Card, Text } from "react-native-paper"
|
||||||
import CircularProgress from "react-native-circular-progress-indicator"
|
import CircularProgress from "react-native-circular-progress-indicator"
|
||||||
import { Agenda } from "react-native-calendars"
|
import { Agenda } from "react-native-calendars"
|
||||||
import type { SetStateAction } from "react"
|
import { useState } from "react"
|
||||||
import { useState, useEffect } from "react"
|
|
||||||
|
|
||||||
import { getNowDateUTC, getISODate } from "@/utils/dates"
|
import { getNowDateUTC, getISODate } from "@/utils/dates"
|
||||||
import type { HabitsTracker } from "@/domain/entities/HabitsTracker"
|
import type { HabitsTracker } from "@/domain/entities/HabitsTracker"
|
||||||
import type { HabitHistory } from "@/domain/entities/HabitHistory"
|
|
||||||
|
|
||||||
export interface StatsProps {
|
export interface StatsProps {
|
||||||
habitsTracker: HabitsTracker
|
habitsTracker: HabitsTracker
|
||||||
}
|
}
|
||||||
|
|
||||||
export const Stats: React.FC<StatsProps> = ({ habitsTracker }) => {
|
export const Stats: React.FC<StatsProps> = (props) => {
|
||||||
|
const { habitsTracker } = props
|
||||||
|
|
||||||
const today = getNowDateUTC()
|
const today = getNowDateUTC()
|
||||||
const todayISO = getISODate(today)
|
const todayISO = getISODate(today)
|
||||||
|
|
||||||
@ -21,98 +21,81 @@ export const Stats: React.FC<StatsProps> = ({ habitsTracker }) => {
|
|||||||
|
|
||||||
const habitsHistory = habitsTracker.getAllHabitsHistory()
|
const habitsHistory = habitsTracker.getAllHabitsHistory()
|
||||||
|
|
||||||
const [goalDays, setGoalDays] = useState(0)
|
let goalDays = 0
|
||||||
const [totalGoalDays, setTotalGoalDays] = useState(0)
|
let totalGoalDays = 0
|
||||||
const [displayDaily, setDisplayDaily] = useState(true)
|
const dailyHabits = habitsHistory.filter((el) => {
|
||||||
const [goalWeek, setGoalWeek] = useState(0)
|
return (
|
||||||
const [totalGoalWeek, setTotalGoalWeek] = useState(0)
|
el.habit.goal.frequency === "daily" && el.habit.startDate <= selectedDate
|
||||||
const [displayWeekly, setDisplayWeekly] = useState(true)
|
)
|
||||||
const [goalMonth, setGoalMonth] = useState(0)
|
})
|
||||||
const [totalGoalMonth, setTotalGoalMonth] = useState(0)
|
let displayDaily = true
|
||||||
const [displayMonthly, setDisplayMonthly] = useState(true)
|
if (dailyHabits.length === 0) {
|
||||||
|
displayDaily = false
|
||||||
const updateStats = (date: Date): void => {
|
} else {
|
||||||
const dailyHabits = habitsHistory.filter((el) => {
|
for (const el of dailyHabits) {
|
||||||
return (
|
totalGoalDays++
|
||||||
el.habit.goal.frequency === "daily" &&
|
if (
|
||||||
el.habit.startDate.getFullYear() <= date.getFullYear() &&
|
el.getProgressesByDate(selectedDate)[0]?.goalProgress.isCompleted() ??
|
||||||
el.habit.startDate.getMonth() <= date.getMonth() &&
|
false
|
||||||
el.habit.startDate.getDate() <= date.getDate()
|
) {
|
||||||
)
|
goalDays++
|
||||||
})
|
}
|
||||||
const weeklyHabits = habitsHistory.filter((el) => {
|
|
||||||
return (
|
|
||||||
el.habit.goal.frequency === "weekly" &&
|
|
||||||
el.habit.startDate.getFullYear() <= date.getFullYear() &&
|
|
||||||
el.habit.startDate.getMonth() <= date.getMonth() &&
|
|
||||||
el.habit.startDate.getDate() <= date.getDate()
|
|
||||||
)
|
|
||||||
})
|
|
||||||
const monthlyHabits = habitsHistory.filter((el) => {
|
|
||||||
return (
|
|
||||||
el.habit.goal.frequency === "monthly" &&
|
|
||||||
el.habit.startDate.getFullYear() <= date.getFullYear() &&
|
|
||||||
el.habit.startDate.getMonth() <= date.getMonth() &&
|
|
||||||
el.habit.startDate.getDate() <= date.getDate()
|
|
||||||
)
|
|
||||||
})
|
|
||||||
|
|
||||||
const calculateGoals = (
|
|
||||||
habits: HabitHistory[],
|
|
||||||
setTotalGoals: {
|
|
||||||
(value: SetStateAction<number>): void
|
|
||||||
(value: SetStateAction<number>): void
|
|
||||||
(value: SetStateAction<number>): void
|
|
||||||
(arg0: any): void
|
|
||||||
},
|
|
||||||
setGoals: {
|
|
||||||
(value: SetStateAction<number>): void
|
|
||||||
(value: SetStateAction<number>): void
|
|
||||||
(value: SetStateAction<number>): void
|
|
||||||
(arg0: any): void
|
|
||||||
},
|
|
||||||
): void => {
|
|
||||||
setTotalGoals(habits.length)
|
|
||||||
const completedGoals = habits.filter((el) => {
|
|
||||||
return (
|
|
||||||
el.getProgressesByDate(date)[0]?.goalProgress.isCompleted() ?? false
|
|
||||||
)
|
|
||||||
}).length
|
|
||||||
setGoals(completedGoals)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (dailyHabits.length === 0) {
|
|
||||||
setDisplayDaily(false)
|
|
||||||
} else {
|
|
||||||
setDisplayDaily(true)
|
|
||||||
calculateGoals(dailyHabits, setTotalGoalDays, setGoalDays)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (weeklyHabits.length === 0) {
|
|
||||||
setDisplayWeekly(false)
|
|
||||||
} else {
|
|
||||||
setDisplayWeekly(true)
|
|
||||||
calculateGoals(weeklyHabits, setTotalGoalWeek, setGoalWeek)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (monthlyHabits.length === 0) {
|
|
||||||
setDisplayMonthly(false)
|
|
||||||
} else {
|
|
||||||
setDisplayMonthly(true)
|
|
||||||
calculateGoals(monthlyHabits, setTotalGoalMonth, setGoalMonth)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
let goalWeek = 0
|
||||||
updateStats(selectedDate)
|
let totalGoalWeek = 0
|
||||||
}, [selectedDate])
|
const weeklyHabits = habitsHistory.filter((el) => {
|
||||||
|
return (
|
||||||
|
el.habit.goal.frequency === "weekly" && el.habit.startDate <= selectedDate
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
let displayWeekly = true
|
||||||
|
if (weeklyHabits.length === 0) {
|
||||||
|
displayWeekly = false
|
||||||
|
} else {
|
||||||
|
for (const el of weeklyHabits) {
|
||||||
|
totalGoalWeek++
|
||||||
|
if (
|
||||||
|
el.getProgressesByDate(selectedDate)[0]?.goalProgress.isCompleted() ??
|
||||||
|
false
|
||||||
|
) {
|
||||||
|
goalWeek++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let goalMonth = 0
|
||||||
|
let totalGoalMonth = 0
|
||||||
|
const monthlyHabits = habitsHistory.filter((el) => {
|
||||||
|
return (
|
||||||
|
el.habit.goal.frequency === "monthly" &&
|
||||||
|
el.habit.startDate <= selectedDate
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
let displayMonthly = true
|
||||||
|
if (monthlyHabits.length === 0) {
|
||||||
|
displayMonthly = false
|
||||||
|
} else {
|
||||||
|
for (const el of monthlyHabits) {
|
||||||
|
totalGoalMonth++
|
||||||
|
if (
|
||||||
|
el.getProgressesByDate(selectedDate)[0]?.goalProgress.isCompleted() ??
|
||||||
|
false
|
||||||
|
) {
|
||||||
|
goalMonth++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Agenda
|
<Agenda
|
||||||
firstDay={1}
|
firstDay={1}
|
||||||
showClosingKnob
|
showClosingKnob
|
||||||
onDayPress={(date) => {
|
onDayPress={(date) => {
|
||||||
return setSelectedDate(new Date(date.dateString))
|
setSelectedDate(new Date(date.dateString))
|
||||||
}}
|
}}
|
||||||
markedDates={{
|
markedDates={{
|
||||||
[todayISO]: { marked: true, today: true },
|
[todayISO]: { marked: true, today: true },
|
||||||
@ -122,74 +105,60 @@ export const Stats: React.FC<StatsProps> = ({ habitsTracker }) => {
|
|||||||
renderList={() => {
|
renderList={() => {
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Card key="statsDay" mode="outlined">
|
{displayDaily ? (
|
||||||
<Card.Title title="Success Day" />
|
<Card key="statsDay" mode="outlined">
|
||||||
<Card.Content>
|
<Card.Title title="Sucess Day" />
|
||||||
{displayDaily ? (
|
<Card.Content>
|
||||||
<>
|
<Text variant="bodyMedium">
|
||||||
<Text variant="bodyMedium">
|
{goalDays} but réussi dans la journée sur {totalGoalDays}
|
||||||
{goalDays} but réussi dans la journée sur {totalGoalDays}
|
</Text>
|
||||||
</Text>
|
<CircularProgress
|
||||||
<CircularProgress
|
value={(goalDays / totalGoalDays) * 100}
|
||||||
value={(goalDays / totalGoalDays) * 100}
|
activeStrokeWidth={12}
|
||||||
activeStrokeWidth={12}
|
progressValueColor={"#ecf0f1"}
|
||||||
progressValueColor={"#ecf0f1"}
|
circleBackgroundColor="black"
|
||||||
circleBackgroundColor="black"
|
titleColor="white"
|
||||||
titleColor="white"
|
title="%"
|
||||||
title="%"
|
/>
|
||||||
/>
|
</Card.Content>
|
||||||
</>
|
</Card>
|
||||||
) : (
|
) : null}
|
||||||
<Text variant="bodyMedium">Aucun objectif quotidien</Text>
|
{displayWeekly ? (
|
||||||
)}
|
<Card key="statsWeek" mode="outlined">
|
||||||
</Card.Content>
|
<Card.Title title="Sucess Week" />
|
||||||
</Card>
|
<Card.Content>
|
||||||
|
<Text variant="bodyMedium">
|
||||||
<Card key="statsWeek" mode="outlined">
|
{goalWeek} but réussi dans la semaine sur {totalGoalWeek}
|
||||||
<Card.Title title="Success Week" />
|
</Text>
|
||||||
<Card.Content>
|
<CircularProgress
|
||||||
{displayWeekly ? (
|
value={(goalWeek / totalGoalWeek) * 100}
|
||||||
<>
|
activeStrokeWidth={12}
|
||||||
<Text variant="bodyMedium">
|
progressValueColor={"#ecf0f1"}
|
||||||
{goalWeek} but réussi dans la semaine sur {totalGoalWeek}
|
circleBackgroundColor="black"
|
||||||
</Text>
|
titleColor="white"
|
||||||
<CircularProgress
|
title="%"
|
||||||
value={(goalWeek / totalGoalWeek) * 100}
|
/>
|
||||||
activeStrokeWidth={12}
|
</Card.Content>
|
||||||
progressValueColor={"#ecf0f1"}
|
</Card>
|
||||||
circleBackgroundColor="black"
|
) : null}
|
||||||
titleColor="white"
|
{displayMonthly ? (
|
||||||
title="%"
|
<Card key="statsMonth" mode="outlined">
|
||||||
/>
|
<Card.Title title="Sucess Month" />
|
||||||
</>
|
<Card.Content>
|
||||||
) : (
|
<Text variant="bodyMedium">
|
||||||
<Text variant="bodyMedium">Aucun objectif hebdomadaire</Text>
|
{goalMonth} but réussi dans le mois sur {totalGoalMonth}
|
||||||
)}
|
</Text>
|
||||||
</Card.Content>
|
<CircularProgress
|
||||||
</Card>
|
value={(goalMonth / totalGoalMonth) * 100}
|
||||||
|
activeStrokeWidth={12}
|
||||||
<Card key="statsMonth" mode="outlined">
|
progressValueColor={"#ecf0f1"}
|
||||||
<Card.Title title="Success Month" />
|
circleBackgroundColor="black"
|
||||||
<Card.Content>
|
titleColor="white"
|
||||||
{displayMonthly ? (
|
title="%"
|
||||||
<>
|
/>
|
||||||
<Text variant="bodyMedium">
|
</Card.Content>
|
||||||
{goalMonth} but réussi dans le mois sur {totalGoalMonth}
|
</Card>
|
||||||
</Text>
|
) : null}
|
||||||
<CircularProgress
|
|
||||||
value={(goalMonth / totalGoalMonth) * 100}
|
|
||||||
activeStrokeWidth={12}
|
|
||||||
progressValueColor={"#ecf0f1"}
|
|
||||||
circleBackgroundColor="black"
|
|
||||||
titleColor="white"
|
|
||||||
title="%"
|
|
||||||
/>
|
|
||||||
</>
|
|
||||||
) : (
|
|
||||||
<Text variant="bodyMedium">Aucun objectif mensuel</Text>
|
|
||||||
)}
|
|
||||||
</Card.Content>
|
|
||||||
</Card>
|
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}}
|
}}
|
||||||
|
Loading…
Reference in New Issue
Block a user