Skip to content

Commit a4c2362

Browse files
committed
better habit target updates
1 parent 8a88e6f commit a4c2362

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/components/habit/form.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Input } from "@/components/ui/input"
55
import { Label } from "@/components/ui/label"
66
import { Separator } from "@/components/ui/separator"
77
import * as colors from "@/lib/colors"
8+
import { formatDate } from "@/lib/dates"
89
import * as icons from "@/lib/icons"
910
import { randomArrayElement } from "@/lib/utils"
1011
import { 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

Comments
 (0)