2024-03-16 00:36:44 +01:00
|
|
|
import { HabitsTracker } from "@/domain/entities/HabitsTracker"
|
2024-03-22 23:41:51 +01:00
|
|
|
import type { FetchState } from "./_Presenter"
|
2024-03-15 22:48:28 +01:00
|
|
|
import { Presenter } from "./_Presenter"
|
|
|
|
import type {
|
|
|
|
RetrieveHabitsTrackerUseCase,
|
|
|
|
RetrieveHabitsTrackerUseCaseOptions,
|
2024-03-16 00:36:44 +01:00
|
|
|
} from "@/domain/use-cases/RetrieveHabitsTracker"
|
2024-03-15 22:48:28 +01:00
|
|
|
|
|
|
|
export interface HabitsTrackerPresenterState {
|
|
|
|
habitsTracker: HabitsTracker
|
2024-03-22 23:41:51 +01:00
|
|
|
|
|
|
|
retrieveHabitsTracker: {
|
|
|
|
state: FetchState
|
|
|
|
}
|
2024-03-15 22:48:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface HabitsTrackerPresenterOptions {
|
|
|
|
retrieveHabitsTrackerUseCase: RetrieveHabitsTrackerUseCase
|
|
|
|
}
|
|
|
|
|
|
|
|
export class HabitsTrackerPresenter
|
|
|
|
extends Presenter<HabitsTrackerPresenterState>
|
|
|
|
implements HabitsTrackerPresenterOptions
|
|
|
|
{
|
|
|
|
public retrieveHabitsTrackerUseCase: RetrieveHabitsTrackerUseCase
|
|
|
|
|
|
|
|
public constructor(options: HabitsTrackerPresenterOptions) {
|
|
|
|
const { retrieveHabitsTrackerUseCase } = options
|
|
|
|
const habitsTracker = HabitsTracker.default()
|
2024-03-22 23:41:51 +01:00
|
|
|
super({ habitsTracker, retrieveHabitsTracker: { state: "idle" } })
|
2024-03-15 22:48:28 +01:00
|
|
|
this.retrieveHabitsTrackerUseCase = retrieveHabitsTrackerUseCase
|
|
|
|
}
|
|
|
|
|
|
|
|
public async retrieveHabitsTracker(
|
|
|
|
options: RetrieveHabitsTrackerUseCaseOptions,
|
|
|
|
): Promise<void> {
|
|
|
|
this.setState((state) => {
|
2024-03-22 23:41:51 +01:00
|
|
|
state.retrieveHabitsTracker.state = "loading"
|
|
|
|
state.habitsTracker = HabitsTracker.default()
|
2024-03-15 22:48:28 +01:00
|
|
|
})
|
2024-03-22 23:41:51 +01:00
|
|
|
try {
|
|
|
|
const habitsTracker =
|
|
|
|
await this.retrieveHabitsTrackerUseCase.execute(options)
|
|
|
|
this.setState((state) => {
|
|
|
|
state.habitsTracker = habitsTracker
|
|
|
|
state.retrieveHabitsTracker.state = "success"
|
|
|
|
})
|
|
|
|
} catch (error) {
|
|
|
|
this.setState((state) => {
|
|
|
|
state.retrieveHabitsTracker.state = "error"
|
|
|
|
})
|
|
|
|
}
|
2024-03-15 22:48:28 +01:00
|
|
|
}
|
|
|
|
}
|