@@ -5,6 +5,7 @@ import { Input } from "@/components/ui/input"
55import { Label } from "@/components/ui/label"
66import { Separator } from "@/components/ui/separator"
77import * as colors from "@/lib/colors"
8+ import { formatDate } from "@/lib/dates"
89import * as icons from "@/lib/icons"
910import { randomArrayElement } from "@/lib/utils"
1011import { type Habit , HabitSchema } from "@/proto/models/v1/models_pb"
@@ -38,6 +39,17 @@ export const HabitForm: React.FC<P> = ({ value, onChange, onCancel }) => {
3839 setHabit ( next )
3940 }
4041
42+ const updateDailyTarget = ( v : number ) => {
43+ const next = clone ( HabitSchema , habit )
44+ next . dailyTarget = v
45+
46+ const date = formatDate ( new Date ( ) )
47+ if ( next . completions [ date ] !== undefined ) {
48+ next . completions [ date ] . target = v
49+ }
50+
51+ setHabit ( next )
52+ }
4153 const valid = ! ! habit . name
4254
4355 const handleSubmit = ( ) => {
@@ -97,7 +109,7 @@ export const HabitForm: React.FC<P> = ({ value, onChange, onCancel }) => {
97109 disabled = { loading }
98110 value = { habit . dailyTarget }
99111 onFocus = { ( e ) => e . target . select ( ) }
100- onChange = { ( e ) => update ( ( h ) => ( h . dailyTarget = e . target . valueAsNumber || 0 ) ) }
112+ onChange = { ( e ) => updateDailyTarget ( e . target . valueAsNumber || 0 ) }
101113 />
102114 </ div >
103115
0 commit comments