mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-04 11:07:40 +01:00
refactor(haptics): centralize tick in useCreateMutation
Move haptic() into create() so every "New/Add X" surface gets the tick by
default — NewGoalButton, NewSubscriptionButton, AddCommitmentButton plus
NewHabitButton, NewPersonButton, NewProjectButton (the latter three picked
up the behavior for free).
Removes the three per-caller haptic imports and inline calls added in 25cb863.
By the time create() runs the caller's own validation has already passed
(ModalForm's canSubmit gates the modal path; AddCommitmentButton's
description-required guard runs first), so the tick still only fires on
real submission attempts.
One central SSOT instead of N copies — exactly the consolidation the
session next: called for.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
4 files changed
+23
-26
No files matched your search
@@ -7,7 +7,6 @@ import { GOAL_STATUS } from "@/lib/constants/statuses";
|
||||
import { Field } from "@/components/ui/form";
|
||||
import { ModalForm } from "@/components/ui/modal-form";
|
||||
import { useCreateMutation } from "@/hooks/use-create-mutation";
|
||||
import { haptic } from "@/lib/haptics";
|
||||
|
||||
export function NewGoalButton({ goals }: { goals: GoalWithChildren[] }) {
|
||||
const [title, setTitle] = useState("");
|
||||
@@ -23,15 +22,12 @@ export function NewGoalButton({ goals }: { goals: GoalWithChildren[] }) {
|
||||
setTitle(""); setDescription(""); setTargetDate(""); setParentGoalId(""); setError(null);
|
||||
};
|
||||
|
||||
const onSubmit = () => {
|
||||
haptic();
|
||||
return create({
|
||||
title: title.trim(),
|
||||
description: description.trim() || undefined,
|
||||
targetDate: targetDate || undefined,
|
||||
parentGoalId: parentGoalId || undefined,
|
||||
});
|
||||
};
|
||||
const onSubmit = () => create({
|
||||
title: title.trim(),
|
||||
description: description.trim() || undefined,
|
||||
targetDate: targetDate || undefined,
|
||||
parentGoalId: parentGoalId || undefined,
|
||||
});
|
||||
|
||||
// Flatten goal tree for parent selector (exclude completed goals)
|
||||
const flatGoals: Array<{ id: string; title: string; depth: number }> = [];
|
||||
|
||||
@@ -7,7 +7,6 @@ import { ModalForm } from "@/components/ui/modal-form";
|
||||
import { useCreateMutation } from "@/hooks/use-create-mutation";
|
||||
import { postJson } from "@/lib/api/fetch";
|
||||
import type { CreateSubscriptionInput } from "@/db/queries/money";
|
||||
import { haptic } from "@/lib/haptics";
|
||||
|
||||
export function NewSubscriptionButton() {
|
||||
const [name, setName] = useState("");
|
||||
@@ -29,19 +28,16 @@ export function NewSubscriptionButton() {
|
||||
setError(null);
|
||||
};
|
||||
|
||||
const onSubmit = () => {
|
||||
haptic();
|
||||
return create({
|
||||
name: name.trim(),
|
||||
vendor: vendor.trim() || undefined,
|
||||
amount: amount ? parseFloat(amount) : undefined,
|
||||
currency,
|
||||
frequency,
|
||||
nextDue: nextDue || undefined,
|
||||
paymentMethod: paymentMethod.trim() || undefined,
|
||||
notes: notes.trim() || undefined,
|
||||
});
|
||||
};
|
||||
const onSubmit = () => create({
|
||||
name: name.trim(),
|
||||
vendor: vendor.trim() || undefined,
|
||||
amount: amount ? parseFloat(amount) : undefined,
|
||||
currency,
|
||||
frequency,
|
||||
nextDue: nextDue || undefined,
|
||||
paymentMethod: paymentMethod.trim() || undefined,
|
||||
notes: notes.trim() || undefined,
|
||||
});
|
||||
|
||||
return (
|
||||
<ModalForm
|
||||
|
||||
@@ -5,7 +5,6 @@ import { Plus, X, Loader2 } from "lucide-react";
|
||||
import { postJson } from "@/lib/api/fetch";
|
||||
import { useCreateMutation } from "@/hooks/use-create-mutation";
|
||||
import type { CreateCommitmentInput } from "@/db/queries/today";
|
||||
import { haptic } from "@/lib/haptics";
|
||||
|
||||
export function AddCommitmentButton() {
|
||||
const [open, setOpen] = useState(false);
|
||||
@@ -29,7 +28,6 @@ export function AddCommitmentButton() {
|
||||
setError("Description is required");
|
||||
return;
|
||||
}
|
||||
haptic();
|
||||
const ok = await create({
|
||||
description,
|
||||
dueDate: dueDate || undefined,
|
||||
|
||||
@@ -2,12 +2,18 @@
|
||||
|
||||
import { useState } from "react";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { haptic } from "@/lib/haptics";
|
||||
|
||||
/**
|
||||
* Wraps the create-via-POST flow that every "New X" button shares: track
|
||||
* saving + error state, await the response, decode `{ ok, error }`, call
|
||||
* `router.refresh()` on success, and surface a typed error string.
|
||||
*
|
||||
* Fires a haptic tick at the start of every `create()` call — by this point
|
||||
* the caller has already passed its own validation gates (e.g. ModalForm's
|
||||
* canSubmit, AddCommitmentButton's description-required check), so the tick
|
||||
* only confirms valid submission attempts.
|
||||
*
|
||||
* Returns `{ create, saving, error, setError }` — `create(body)` resolves
|
||||
* to `true` on success so the caller can decide whether to close its modal.
|
||||
*/
|
||||
@@ -24,6 +30,7 @@ export function useCreateMutation<TBody>({
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
const create = async (body: TBody): Promise<boolean> => {
|
||||
haptic();
|
||||
setSaving(true);
|
||||
setError(null);
|
||||
try {
|
||||
|
||||
Reference in new issue
Block a user