From f17c7d6e112d7d408dd5895e34ae9a042bd1eb75 Mon Sep 17 00:00:00 2001 From: Xc165543337 <90028194+Xc165543337@users.noreply.github.com> Date: Thu, 4 Apr 2024 12:31:56 +0200 Subject: [PATCH] feat: habit create zod schema --- app/application/habits/new.tsx | 24 ++++------- domain/entities/Goal.ts | 35 ++++++++++++++- domain/entities/Habit.ts | 6 ++- .../HabitCreateForm/HabitCreateForm.tsx | 43 +++++++++++++++++++ 4 files changed, 89 insertions(+), 19 deletions(-) create mode 100644 presentation/react/components/HabitCreateForm/HabitCreateForm.tsx diff --git a/app/application/habits/new.tsx b/app/application/habits/new.tsx index 053a29a..f2c5361 100644 --- a/app/application/habits/new.tsx +++ b/app/application/habits/new.tsx @@ -1,20 +1,14 @@ -import { Text } from "react-native-paper" -import { SafeAreaView } from "react-native-safe-area-context" +import { HabitCreateForm } from "@/presentation/react/components/HabitCreateForm/HabitCreateForm" +import { useAuthentication } from "@/presentation/react/contexts/Authentication" const NewHabitPage: React.FC = () => { - return ( - - New Habit - - ) + const { user } = useAuthentication() + + if (user === null) { + return null + } + + return } export default NewHabitPage diff --git a/domain/entities/Goal.ts b/domain/entities/Goal.ts index fa81fe3..7e3f1f3 100644 --- a/domain/entities/Goal.ts +++ b/domain/entities/Goal.ts @@ -1,9 +1,40 @@ -export const GOAL_FREQUENCIES = ["daily", "weekly", "monthly"] as const +import { z } from "zod" + +export const GOAL_FREQUENCIES_ZOD = [ + z.literal("daily"), + z.literal("weekly"), + z.literal("monthly"), +] as const +export const goalFrequencyZod = z.union(GOAL_FREQUENCIES_ZOD) +export const GOAL_FREQUENCIES = GOAL_FREQUENCIES_ZOD.map((frequency) => { + return frequency.value +}) export type GoalFrequency = (typeof GOAL_FREQUENCIES)[number] -export const GOAL_TYPES = ["numeric", "boolean"] as const +export const GOAL_TYPES_ZOD = [ + z.literal("numeric"), + z.literal("boolean"), +] as const +export const goalTypeZod = z.union(GOAL_TYPES_ZOD) +export const GOAL_TYPES = GOAL_TYPES_ZOD.map((type) => { + return type.value +}) export type GoalType = (typeof GOAL_TYPES)[number] +export const GoalCreateSchema = z.object({ + frequency: goalFrequencyZod, + target: z.discriminatedUnion("type", [ + z.object({ type: z.literal("boolean") }), + z.object({ + type: z.literal("numeric"), + value: z.number().int().min(0), + unit: z.string().min(1), + }), + ]), +}) + +export type GoalCreateData = z.infer + interface GoalBase { frequency: GoalFrequency } diff --git a/domain/entities/Habit.ts b/domain/entities/Habit.ts index 9c17f91..e212d80 100644 --- a/domain/entities/Habit.ts +++ b/domain/entities/Habit.ts @@ -1,6 +1,6 @@ import { z } from "zod" -import type { Goal, GoalBaseJSON } from "./Goal" +import { GoalCreateSchema, type Goal, type GoalBaseJSON } from "./Goal" import { Entity, EntitySchema } from "./_Entity" export const HabitSchema = EntitySchema.extend({ @@ -10,7 +10,9 @@ export const HabitSchema = EntitySchema.extend({ icon: z.string().min(1), }) -export const HabitCreateSchema = HabitSchema.extend({}).omit({ id: true }) +export const HabitCreateSchema = HabitSchema.extend({ + goal: GoalCreateSchema, +}).omit({ id: true }) export type HabitCreateData = z.infer type HabitDataBase = z.infer diff --git a/presentation/react/components/HabitCreateForm/HabitCreateForm.tsx b/presentation/react/components/HabitCreateForm/HabitCreateForm.tsx new file mode 100644 index 0000000..e588ebc --- /dev/null +++ b/presentation/react/components/HabitCreateForm/HabitCreateForm.tsx @@ -0,0 +1,43 @@ +import { useForm } from "react-hook-form" +import { Appbar } from "react-native-paper" +import { SafeAreaView } from "react-native-safe-area-context" + +import type { HabitCreateData } from "@/domain/entities/Habit" +import type { User } from "@/domain/entities/User" + +export interface HabitCreateFormProps { + user: User +} + +export const HabitCreateForm: React.FC = ({ user }) => { + useForm({ + // const { control, handleSubmit, setValue } = useForm({ + defaultValues: { + userId: user.id, + name: "", + color: "#006CFF", + icon: "lightbulb", + goal: { + frequency: "daily", + target: { + type: "boolean", + }, + }, + }, + }) + + return ( + + + + + {/* */} + + ) +}