refactor: type PollForm with explicit PollFormValues
This commit is contained in:
1 parent
4991008159
commit
41f5269b4e
1 file changed
+17
-10
@@ -17,17 +17,27 @@ import {
|
|||||||
import { Switch } from "@/components/ui/switch";
|
import { Switch } from "@/components/ui/switch";
|
||||||
import { Textarea } from "@/components/ui/textarea";
|
import { Textarea } from "@/components/ui/textarea";
|
||||||
import { useServerAction } from "@/hooks/use-server-action";
|
import { useServerAction } from "@/hooks/use-server-action";
|
||||||
import type { CreatePollInput } from "@/lib/validators/poll";
|
import type { CreatePollInput, UpdatePollInput } from "@/lib/validators/poll";
|
||||||
|
|
||||||
interface PollFormProps {
|
interface PollFormProps {
|
||||||
defaultValues?: Partial<CreatePollInput> & { id?: number };
|
defaultValues?: Partial<CreatePollInput> & { id?: number };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface PollFormValues {
|
||||||
|
title: string;
|
||||||
|
description: string | null;
|
||||||
|
status: string;
|
||||||
|
showResults: number;
|
||||||
|
multipleChoice: number;
|
||||||
|
startsAt?: Date | null;
|
||||||
|
endsAt?: Date | null;
|
||||||
|
}
|
||||||
|
|
||||||
export function PollForm({ defaultValues }: PollFormProps) {
|
export function PollForm({ defaultValues }: PollFormProps) {
|
||||||
const { run, isPending } = useServerAction();
|
const { run, isPending } = useServerAction();
|
||||||
const isEditing = !!defaultValues?.id;
|
const isEditing = !!defaultValues?.id;
|
||||||
|
|
||||||
const form = useForm({
|
const form = useForm<PollFormValues>({
|
||||||
defaultValues: {
|
defaultValues: {
|
||||||
title: "",
|
title: "",
|
||||||
description: "",
|
description: "",
|
||||||
@@ -38,14 +48,13 @@ export function PollForm({ defaultValues }: PollFormProps) {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
// biome-ignore lint/suspicious/noExplicitAny: explicitly chosen here, see surrounding code
|
function onSubmit(data: PollFormValues) {
|
||||||
function onSubmit(data: any) {
|
|
||||||
if (isEditing) {
|
if (isEditing) {
|
||||||
run(() => updatePoll({ id: defaultValues!.id, ...data }), {
|
run(() => updatePoll({ id: defaultValues!.id, ...data } as UpdatePollInput), {
|
||||||
successMessage: "Poll updated!",
|
successMessage: "Poll updated!",
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
run(() => createPoll(data), {
|
run(() => createPoll(data as CreatePollInput), {
|
||||||
successMessage: "Poll created!",
|
successMessage: "Poll created!",
|
||||||
onSuccess: (result) => {
|
onSuccess: (result) => {
|
||||||
const data = result.data as Record<string, unknown> | undefined;
|
const data = result.data as Record<string, unknown> | undefined;
|
||||||
@@ -106,9 +115,8 @@ export function PollForm({ defaultValues }: PollFormProps) {
|
|||||||
<Label>Starts At</Label>
|
<Label>Starts At</Label>
|
||||||
<DateTimePicker
|
<DateTimePicker
|
||||||
value={form.watch("startsAt")}
|
value={form.watch("startsAt")}
|
||||||
// biome-ignore lint/suspicious/noExplicitAny: form value type inferred loose; runtime validated by zod on submit
|
|
||||||
onChange={(d) =>
|
onChange={(d) =>
|
||||||
form.setValue("startsAt", d as any, { shouldDirty: true })
|
form.setValue("startsAt", d, { shouldDirty: true })
|
||||||
}
|
}
|
||||||
placeholder="Select start date"
|
placeholder="Select start date"
|
||||||
/>
|
/>
|
||||||
@@ -117,9 +125,8 @@ export function PollForm({ defaultValues }: PollFormProps) {
|
|||||||
<Label>Ends At</Label>
|
<Label>Ends At</Label>
|
||||||
<DateTimePicker
|
<DateTimePicker
|
||||||
value={form.watch("endsAt")}
|
value={form.watch("endsAt")}
|
||||||
// biome-ignore lint/suspicious/noExplicitAny: form value type inferred loose; runtime validated by zod on submit
|
|
||||||
onChange={(d) =>
|
onChange={(d) =>
|
||||||
form.setValue("endsAt", d as any, { shouldDirty: true })
|
form.setValue("endsAt", d, { shouldDirty: true })
|
||||||
}
|
}
|
||||||
placeholder="Select end date"
|
placeholder="Select end date"
|
||||||
/>
|
/>
|
||||||
|
|||||||
Reference in new issue
Block a user