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