1
1
mirror of https://github.com/theoludwig/p61-project.git synced 2024-07-17 07:00:12 +02:00

feat: group habits history by frequencies (daily, weekly, monthly)

This commit is contained in:
Théo LUDWIG 2024-03-25 13:05:15 +01:00
parent 8774bc735a
commit bb126e907c
Signed by: theoludwig
GPG Key ID: ADFE5A563D718F3B
5 changed files with 78 additions and 15 deletions

View File

@ -21,7 +21,7 @@ const HabitsPage: React.FC = () => {
{retrieveHabitsTracker.state === "loading" ? ( {retrieveHabitsTracker.state === "loading" ? (
<ActivityIndicator animating size="large" /> <ActivityIndicator animating size="large" />
) : ( ) : (
<HabitsHistory habitsHistory={habitsTracker.habitsHistory} /> <HabitsHistory habitsTracker={habitsTracker} />
)} )}
</SafeAreaView> </SafeAreaView>
) )

View File

@ -1,11 +1,14 @@
import type { GoalFrequency } from "./Goal"
import type { HabitHistory } from "./HabitHistory" import type { HabitHistory } from "./HabitHistory"
export interface HabitsTrackerData { export interface HabitsTrackerData {
habitsHistory: HabitHistory[] habitsHistory: {
[key in GoalFrequency]: HabitHistory[]
}
} }
export class HabitsTracker implements HabitsTrackerData { export class HabitsTracker implements HabitsTrackerData {
public habitsHistory: HabitHistory[] public habitsHistory: HabitsTrackerData["habitsHistory"]
public constructor(options: HabitsTrackerData) { public constructor(options: HabitsTrackerData) {
const { habitsHistory } = options const { habitsHistory } = options
@ -13,6 +16,12 @@ export class HabitsTracker implements HabitsTrackerData {
} }
public static default(): HabitsTracker { public static default(): HabitsTracker {
return new HabitsTracker({ habitsHistory: [] }) return new HabitsTracker({
habitsHistory: {
daily: [],
weekly: [],
monthly: [],
},
})
} }
} }

View File

@ -1,4 +1,5 @@
import { HabitHistory } from "../entities/HabitHistory" import { HabitHistory } from "../entities/HabitHistory"
import type { HabitsTrackerData } from "../entities/HabitsTracker"
import { HabitsTracker } from "../entities/HabitsTracker" import { HabitsTracker } from "../entities/HabitsTracker"
import type { User } from "../entities/User" import type { User } from "../entities/User"
import type { GetHabitProgressHistoryRepository } from "../repositories/GetHabitProgressHistory" import type { GetHabitProgressHistoryRepository } from "../repositories/GetHabitProgressHistory"
@ -44,8 +45,23 @@ export class RetrieveHabitsTrackerUseCase
}) })
}), }),
) )
const habitsHistory = habitProgressHistories.reduce<
HabitsTrackerData["habitsHistory"]
>(
(accumulator, habitHistory) => {
const { habit } = habitHistory
const frequency = habit.goal.frequency
accumulator[frequency].push(habitHistory)
return accumulator
},
{
daily: [],
weekly: [],
monthly: [],
},
)
const habitsTracker = new HabitsTracker({ const habitsTracker = new HabitsTracker({
habitsHistory: habitProgressHistories, habitsHistory,
}) })
return habitsTracker return habitsTracker
} }

View File

@ -0,0 +1,3 @@
export const capitalize = (string: string): string => {
return string.charAt(0).toUpperCase() + string.slice(1)
}

View File

@ -1,30 +1,65 @@
import { FlatList } from "react-native" import { FlatList } from "react-native"
import { List } from "react-native-paper" import { List } from "react-native-paper"
import { useState } from "react"
import type { HabitHistory as HabitHistoryType } from "@/domain/entities/HabitHistory" import type { GoalFrequency } from "@/domain/entities/Goal"
import { GOAL_FREQUENCIES } from "@/domain/entities/Goal"
import type { HabitsTracker } from "@/domain/entities/HabitsTracker"
import { capitalize } from "@/presentation/presenters/utils/strings"
import { HabitHistory } from "./HabitHistory" import { HabitHistory } from "./HabitHistory"
export interface HabitsHistoryProps { export interface HabitsHistoryProps {
habitsHistory: HabitHistoryType[] habitsTracker: HabitsTracker
} }
export const HabitsHistory: React.FC<HabitsHistoryProps> = (props) => { export const HabitsHistory: React.FC<HabitsHistoryProps> = (props) => {
const { habitsHistory } = props const { habitsTracker } = props
const [accordionExpanded, setAccordionExpanded] = useState<{
[key in GoalFrequency]: boolean
}>({
daily: true,
weekly: true,
monthly: true,
})
return ( return (
<List.Section <List.Section
style={[ style={[
{ {
width: "90%", width: "92%",
}, },
]} ]}
> >
<FlatList {GOAL_FREQUENCIES.map((frequency) => {
data={habitsHistory} return (
renderItem={({ item }) => { <List.Accordion
return <HabitHistory habitHistory={item} /> expanded={accordionExpanded[frequency]}
}} onPress={() => {
/> setAccordionExpanded((old) => {
return {
...old,
[frequency]: !old[frequency],
}
})
}}
key={frequency}
title={capitalize(frequency)}
titleStyle={[
{
fontSize: 26,
},
]}
>
<FlatList
data={habitsTracker.habitsHistory[frequency]}
renderItem={({ item }) => {
return <HabitHistory habitHistory={item} />
}}
/>
</List.Accordion>
)
})}
</List.Section> </List.Section>
) )
} }