2024-04-05 00:08:40 +02:00
|
|
|
import { ZodError } from "zod"
|
|
|
|
|
2024-03-16 00:36:44 +01:00
|
|
|
import { HabitsTracker } from "@/domain/entities/HabitsTracker"
|
2024-04-05 00:08:40 +02:00
|
|
|
import type { ErrorGlobal, 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-04-11 13:07:17 +02:00
|
|
|
import type { HabitCreateData, HabitEditData } from "@/domain/entities/Habit"
|
2024-04-08 23:21:36 +02:00
|
|
|
import { getErrorsFieldsFromZodError } from "../../utils/zod"
|
2024-04-05 00:08:40 +02:00
|
|
|
import type { HabitCreateUseCase } from "@/domain/use-cases/HabitCreate"
|
2024-04-11 13:07:17 +02:00
|
|
|
import type { HabitEditUseCase } from "@/domain/use-cases/HabitEdit"
|
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-04-05 00:08:40 +02:00
|
|
|
|
|
|
|
habitCreate: {
|
|
|
|
state: FetchState
|
|
|
|
errors: {
|
|
|
|
fields: Array<keyof HabitCreateData>
|
|
|
|
global: ErrorGlobal
|
|
|
|
}
|
|
|
|
}
|
2024-04-11 12:31:45 +02:00
|
|
|
|
|
|
|
habitEdit: {
|
|
|
|
state: FetchState
|
|
|
|
errors: {
|
2024-04-11 13:07:17 +02:00
|
|
|
fields: Array<keyof HabitEditData>
|
2024-04-11 12:31:45 +02:00
|
|
|
global: ErrorGlobal
|
|
|
|
}
|
|
|
|
}
|
2024-03-15 22:48:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface HabitsTrackerPresenterOptions {
|
|
|
|
retrieveHabitsTrackerUseCase: RetrieveHabitsTrackerUseCase
|
2024-04-05 00:08:40 +02:00
|
|
|
habitCreateUseCase: HabitCreateUseCase
|
2024-04-11 13:07:17 +02:00
|
|
|
habitEditUseCase: HabitEditUseCase
|
2024-03-15 22:48:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export class HabitsTrackerPresenter
|
|
|
|
extends Presenter<HabitsTrackerPresenterState>
|
|
|
|
implements HabitsTrackerPresenterOptions
|
|
|
|
{
|
|
|
|
public retrieveHabitsTrackerUseCase: RetrieveHabitsTrackerUseCase
|
2024-04-05 00:08:40 +02:00
|
|
|
public habitCreateUseCase: HabitCreateUseCase
|
2024-04-11 13:07:17 +02:00
|
|
|
public habitEditUseCase: HabitEditUseCase
|
2024-03-15 22:48:28 +01:00
|
|
|
|
|
|
|
public constructor(options: HabitsTrackerPresenterOptions) {
|
2024-04-11 13:07:17 +02:00
|
|
|
const {
|
|
|
|
retrieveHabitsTrackerUseCase,
|
|
|
|
habitCreateUseCase,
|
|
|
|
habitEditUseCase,
|
|
|
|
} = options
|
2024-03-15 22:48:28 +01:00
|
|
|
const habitsTracker = HabitsTracker.default()
|
2024-04-05 00:08:40 +02:00
|
|
|
super({
|
|
|
|
habitsTracker,
|
|
|
|
retrieveHabitsTracker: { state: "idle" },
|
|
|
|
habitCreate: {
|
|
|
|
state: "idle",
|
|
|
|
errors: {
|
|
|
|
fields: [],
|
|
|
|
global: null,
|
|
|
|
},
|
|
|
|
},
|
2024-04-11 12:31:45 +02:00
|
|
|
habitEdit: {
|
|
|
|
state: "idle",
|
|
|
|
errors: {
|
|
|
|
fields: [],
|
|
|
|
global: null,
|
|
|
|
},
|
|
|
|
},
|
2024-04-05 00:08:40 +02:00
|
|
|
})
|
2024-03-15 22:48:28 +01:00
|
|
|
this.retrieveHabitsTrackerUseCase = retrieveHabitsTrackerUseCase
|
2024-04-05 00:08:40 +02:00
|
|
|
this.habitCreateUseCase = habitCreateUseCase
|
2024-04-11 13:07:17 +02:00
|
|
|
this.habitEditUseCase = habitEditUseCase
|
2024-04-05 00:08:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public async habitCreate(data: unknown): Promise<FetchState> {
|
|
|
|
try {
|
|
|
|
this.setState((state) => {
|
|
|
|
state.habitCreate.state = "loading"
|
|
|
|
state.habitCreate.errors = {
|
|
|
|
fields: [],
|
|
|
|
global: null,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
const habit = await this.habitCreateUseCase.execute(data)
|
|
|
|
this.setState((state) => {
|
|
|
|
state.habitCreate.state = "success"
|
|
|
|
state.habitsTracker.addHabit(habit)
|
|
|
|
})
|
|
|
|
return "success"
|
|
|
|
} catch (error) {
|
|
|
|
this.setState((state) => {
|
|
|
|
state.habitCreate.state = "error"
|
|
|
|
if (error instanceof ZodError) {
|
|
|
|
state.habitCreate.errors.fields =
|
|
|
|
getErrorsFieldsFromZodError<HabitCreateData>(error)
|
|
|
|
} else {
|
|
|
|
state.habitCreate.errors.global = "unknown"
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return "error"
|
|
|
|
}
|
2024-03-15 22:48:28 +01:00
|
|
|
}
|
|
|
|
|
2024-04-11 12:31:45 +02:00
|
|
|
public async habitEdit(data: unknown): Promise<FetchState> {
|
|
|
|
try {
|
|
|
|
this.setState((state) => {
|
|
|
|
state.habitEdit.state = "loading"
|
|
|
|
state.habitEdit.errors = {
|
|
|
|
fields: [],
|
|
|
|
global: null,
|
|
|
|
}
|
|
|
|
})
|
2024-04-11 13:07:17 +02:00
|
|
|
const habit = await this.habitEditUseCase.execute(data)
|
2024-04-11 12:31:45 +02:00
|
|
|
this.setState((state) => {
|
|
|
|
state.habitEdit.state = "success"
|
2024-04-11 13:07:17 +02:00
|
|
|
state.habitsTracker.editHabit(habit)
|
2024-04-11 12:31:45 +02:00
|
|
|
})
|
|
|
|
return "success"
|
|
|
|
} catch (error) {
|
|
|
|
this.setState((state) => {
|
|
|
|
state.habitEdit.state = "error"
|
|
|
|
if (error instanceof ZodError) {
|
|
|
|
state.habitEdit.errors.fields =
|
2024-04-11 13:07:17 +02:00
|
|
|
getErrorsFieldsFromZodError<HabitEditData>(error)
|
2024-04-11 12:31:45 +02:00
|
|
|
} else {
|
|
|
|
state.habitEdit.errors.global = "unknown"
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return "error"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-15 22:48:28 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|