import type { IconName } from "@fortawesome/free-solid-svg-icons" import { FontAwesomeIcon } from "@fortawesome/react-native-fontawesome" import { zodResolver } from "@hookform/resolvers/zod" import { useState } from "react" import { Controller, useForm } from "react-hook-form" import { ScrollView, StyleSheet, View } from "react-native" import { Button, HelperText, Snackbar, Text, TextInput, } from "react-native-paper" import { SafeAreaView } from "react-native-safe-area-context" import ColorPicker, { HueSlider, Panel1, Preview, } from "reanimated-color-picker" import type { Habit, HabitEditData } from "@/domain/entities/Habit" import { HabitEditSchema } from "@/domain/entities/Habit" import { useHabitsTracker } from "@/presentation/react/contexts/HabitsTracker" import { useBoolean } from "@/presentation/react/hooks/useBoolean" import { IconSelectorModal } from "./IconSelectorModal" export interface HabitEditFormProps { habit: Habit } export const HabitEditForm: React.FC = ({ habit }) => { const { habitEdit, habitStop, habitsTrackerPresenter } = useHabitsTracker() const { control, formState: { errors, isValid }, handleSubmit, } = useForm({ mode: "onChange", resolver: zodResolver(HabitEditSchema), defaultValues: { id: habit.id, userId: habit.userId, name: habit.name, color: habit.color, icon: habit.icon, }, }) const { value: isModalIconSelectorVisible, setTrue: openModalIconSelector, setFalse: closeModalIconSelector, } = useBoolean() const [isVisibleSnackbar, setIsVisibleSnackbar] = useState(false) const onDismissSnackbar = (): void => { setIsVisibleSnackbar(false) } const onSubmit = async (data: HabitEditData): Promise => { await habitsTrackerPresenter.habitEdit(data) setIsVisibleSnackbar(true) } return ( { return ( <> {errors.name != null ? ( {errors.name.type === "too_big" ? "Name is too long" : "Name is required"} ) : null} ) }} name="name" /> { return ( { onChange(value.hex) }} > ) }} name="color" /> { return ( ) }} name="icon" /> {habit.endDate == null ? ( ) : ( 🛑 The habit has been stopped! (No further progress can be saved) )} ✅ Habit Saved successfully! ) } const styles = StyleSheet.create({ spacing: { marginVertical: 16, }, })