style: clean up code with prettier and eslint
This commit is contained in:
1 parent
60278b9e71
commit
fb8e77bb68
378 files changed
+22407
-20689
No files matched your search
@@ -1,59 +1,53 @@
|
||||
'use client'
|
||||
"use client";
|
||||
|
||||
import { useForm } from 'react-hook-form'
|
||||
import { createPoll, updatePoll } from '@/actions/polls'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'
|
||||
import { DateTimePicker } from '@/components/ui/date-time-picker'
|
||||
import { Input } from '@/components/ui/input'
|
||||
import { Label } from '@/components/ui/label'
|
||||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
SelectItem,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
} from '@/components/ui/select'
|
||||
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 { useForm } from "react-hook-form";
|
||||
import { createPoll, updatePoll } from "@/actions/polls";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
|
||||
import { DateTimePicker } from "@/components/ui/date-time-picker";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Label } from "@/components/ui/label";
|
||||
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
|
||||
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";
|
||||
|
||||
interface PollFormProps {
|
||||
defaultValues?: Partial<CreatePollInput> & { id?: number }
|
||||
defaultValues?: Partial<CreatePollInput> & { id?: number };
|
||||
}
|
||||
|
||||
export function PollForm({ defaultValues }: PollFormProps) {
|
||||
const { run, isPending } = useServerAction()
|
||||
const isEditing = !!defaultValues?.id
|
||||
const { run, isPending } = useServerAction();
|
||||
const isEditing = !!defaultValues?.id;
|
||||
|
||||
const form = useForm({
|
||||
defaultValues: {
|
||||
title: '',
|
||||
description: '',
|
||||
status: 'draft',
|
||||
title: "",
|
||||
description: "",
|
||||
status: "draft",
|
||||
showResults: 1,
|
||||
multipleChoice: 0,
|
||||
...defaultValues,
|
||||
},
|
||||
})
|
||||
});
|
||||
|
||||
// biome-ignore lint/suspicious/noExplicitAny: explicitly chosen here, see surrounding code
|
||||
function onSubmit(data: any) {
|
||||
if (isEditing) {
|
||||
run(() => updatePoll({ id: defaultValues!.id!, ...data }), {
|
||||
successMessage: 'Poll updated!',
|
||||
})
|
||||
successMessage: "Poll updated!",
|
||||
});
|
||||
} else {
|
||||
run(() => createPoll(data), {
|
||||
successMessage: 'Poll created!',
|
||||
successMessage: "Poll created!",
|
||||
onSuccess: (result) => {
|
||||
const data = result.data as Record<string, unknown> | undefined
|
||||
const data = result.data as Record<string, unknown> | undefined;
|
||||
if (data?.id) {
|
||||
window.location.href = `/admin/polls/${data.id}`
|
||||
window.location.href = `/admin/polls/${data.id}`;
|
||||
}
|
||||
},
|
||||
})
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -61,12 +55,12 @@ export function PollForm({ defaultValues }: PollFormProps) {
|
||||
<form onSubmit={form.handleSubmit(onSubmit)}>
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>{isEditing ? 'Edit Poll' : 'Poll Details'}</CardTitle>
|
||||
<CardTitle>{isEditing ? "Edit Poll" : "Poll Details"}</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-4">
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="title">Title</Label>
|
||||
<Input id="title" {...form.register('title')} />
|
||||
<Input id="title" {...form.register("title")} />
|
||||
{form.formState.errors.title && (
|
||||
<p className="text-sm text-destructive">{form.formState.errors.title.message}</p>
|
||||
)}
|
||||
@@ -74,15 +68,15 @@ export function PollForm({ defaultValues }: PollFormProps) {
|
||||
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="description">Description</Label>
|
||||
<Textarea id="description" rows={3} {...form.register('description')} />
|
||||
<Textarea id="description" rows={3} {...form.register("description")} />
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-1 md:grid-cols-3 gap-4">
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="status">Status</Label>
|
||||
<Select
|
||||
value={form.watch('status')}
|
||||
onValueChange={(v) => form.setValue('status', v as CreatePollInput['status'])}
|
||||
value={form.watch("status")}
|
||||
onValueChange={(v) => form.setValue("status", v as CreatePollInput["status"])}
|
||||
>
|
||||
<SelectTrigger>
|
||||
<SelectValue />
|
||||
@@ -97,18 +91,18 @@ export function PollForm({ defaultValues }: PollFormProps) {
|
||||
<div className="space-y-2">
|
||||
<Label>Starts At</Label>
|
||||
<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) => form.setValue('startsAt', d as any, { shouldDirty: true })}
|
||||
onChange={(d) => form.setValue("startsAt", d as any, { shouldDirty: true })}
|
||||
placeholder="Select start date"
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<Label>Ends At</Label>
|
||||
<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) => form.setValue('endsAt', d as any, { shouldDirty: true })}
|
||||
onChange={(d) => form.setValue("endsAt", d as any, { shouldDirty: true })}
|
||||
placeholder="Select end date"
|
||||
/>
|
||||
</div>
|
||||
@@ -117,15 +111,15 @@ export function PollForm({ defaultValues }: PollFormProps) {
|
||||
<div className="flex gap-6">
|
||||
<div className="flex items-center gap-2">
|
||||
<Switch
|
||||
checked={form.watch('showResults') === 1}
|
||||
onCheckedChange={(v) => form.setValue('showResults', v ? 1 : 0)}
|
||||
checked={form.watch("showResults") === 1}
|
||||
onCheckedChange={(v) => form.setValue("showResults", v ? 1 : 0)}
|
||||
/>
|
||||
<Label>Show results to voters</Label>
|
||||
</div>
|
||||
<div className="flex items-center gap-2">
|
||||
<Switch
|
||||
checked={form.watch('multipleChoice') === 1}
|
||||
onCheckedChange={(v) => form.setValue('multipleChoice', v ? 1 : 0)}
|
||||
checked={form.watch("multipleChoice") === 1}
|
||||
onCheckedChange={(v) => form.setValue("multipleChoice", v ? 1 : 0)}
|
||||
/>
|
||||
<Label>Allow multiple answers</Label>
|
||||
</div>
|
||||
@@ -133,11 +127,11 @@ export function PollForm({ defaultValues }: PollFormProps) {
|
||||
|
||||
<div className="flex justify-end">
|
||||
<Button type="submit" disabled={isPending}>
|
||||
{isPending ? 'Saving...' : isEditing ? 'Update Poll' : 'Create Poll'}
|
||||
{isPending ? "Saving..." : isEditing ? "Update Poll" : "Create Poll"}
|
||||
</Button>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</form>
|
||||
)
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user