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

feat: habit create zod schema

This commit is contained in:
Xc165543337 2024-04-04 12:31:56 +02:00
parent c9e720cb13
commit f17c7d6e11
4 changed files with 89 additions and 19 deletions

View File

@ -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 (
<SafeAreaView
style={[
{
flex: 1,
alignItems: "center",
justifyContent: "center",
},
]}
>
<Text>New Habit</Text>
</SafeAreaView>
)
const { user } = useAuthentication()
if (user === null) {
return null
}
return <HabitCreateForm user={user} />
}
export default NewHabitPage

View File

@ -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<typeof GoalCreateSchema>
interface GoalBase {
frequency: GoalFrequency
}

View File

@ -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<typeof HabitCreateSchema>
type HabitDataBase = z.infer<typeof HabitSchema>

View File

@ -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<HabitCreateFormProps> = ({ user }) => {
useForm<HabitCreateData>({
// const { control, handleSubmit, setValue } = useForm<HabitCreateData>({
defaultValues: {
userId: user.id,
name: "",
color: "#006CFF",
icon: "lightbulb",
goal: {
frequency: "daily",
target: {
type: "boolean",
},
},
},
})
return (
<SafeAreaView>
<Appbar.Header>
<Appbar.Content
title="New Habit"
style={{
alignItems: "center",
justifyContent: "center",
}}
/>
</Appbar.Header>
{/* <Controller></Controller> */}
</SafeAreaView>
)
}