This commit is contained in:
1 parent
e85e4d74ea
commit
2e4ed76121
378 files changed
+22404
-20686
No files matched your search
@@ -1,58 +1,52 @@
|
||||
import { notFound, redirect } from 'next/navigation'
|
||||
import { AdminBreadcrumb } from '@/components/admin/admin-breadcrumb'
|
||||
import { Badge } from '@/components/ui/badge'
|
||||
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'
|
||||
import { canAccess, getAdminContext, PERMS } from '@/lib/permissions'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
import { PollForm } from '../poll-form'
|
||||
import { PollQuestionsManager } from './poll-questions'
|
||||
import { PollResultsView } from './poll-results'
|
||||
import { notFound, redirect } from "next/navigation";
|
||||
import { AdminBreadcrumb } from "@/components/admin/admin-breadcrumb";
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
|
||||
import { canAccess, getAdminContext, PERMS } from "@/lib/permissions";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { PollForm } from "../poll-form";
|
||||
import { PollQuestionsManager } from "./poll-questions";
|
||||
import { PollResultsView } from "./poll-results";
|
||||
|
||||
export default async function PollDetailPage({
|
||||
params,
|
||||
}: {
|
||||
params: Promise<{ locale: string; id: string }>
|
||||
params: Promise<{ locale: string; id: string }>;
|
||||
}) {
|
||||
const { id } = await params
|
||||
const { session, permissions } = await getAdminContext()
|
||||
const { id } = await params;
|
||||
const { session, permissions } = await getAdminContext();
|
||||
if (!canAccess(permissions, PERMS.POLLS_EDIT, session.user.rank)) {
|
||||
redirect('/admin/polls')
|
||||
redirect("/admin/polls");
|
||||
}
|
||||
|
||||
const pollId = parseInt(id, 10)
|
||||
if (Number.isNaN(pollId)) notFound()
|
||||
const pollId = parseInt(id, 10);
|
||||
if (Number.isNaN(pollId)) notFound();
|
||||
|
||||
const poll = await prisma.websitePoll.findUnique({
|
||||
where: { id: pollId },
|
||||
include: {
|
||||
questions: {
|
||||
orderBy: { sortOrder: 'asc' },
|
||||
orderBy: { sortOrder: "asc" },
|
||||
include: {
|
||||
votes: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
});
|
||||
|
||||
if (!poll) notFound()
|
||||
if (!poll) notFound();
|
||||
|
||||
const totalVoters = new Set(poll.questions.flatMap((q) => q.votes.map((v) => v.userId))).size
|
||||
const totalVoters = new Set(poll.questions.flatMap((q) => q.votes.map((v) => v.userId))).size;
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<div>
|
||||
<AdminBreadcrumb
|
||||
items={[{ label: 'Polls', href: '/admin/polls' }, { label: poll.title }]}
|
||||
/>
|
||||
<AdminBreadcrumb items={[{ label: "Polls", href: "/admin/polls" }, { label: poll.title }]} />
|
||||
<div className="flex items-center gap-3">
|
||||
<h1 className="text-3xl font-bold tracking-tight">{poll.title}</h1>
|
||||
<Badge
|
||||
variant={
|
||||
poll.status === 'active'
|
||||
? 'default'
|
||||
: poll.status === 'closed'
|
||||
? 'outline'
|
||||
: 'secondary'
|
||||
poll.status === "active" ? "default" : poll.status === "closed" ? "outline" : "secondary"
|
||||
}
|
||||
>
|
||||
{poll.status}
|
||||
@@ -97,7 +91,7 @@ export default async function PollDetailPage({
|
||||
id: poll.id,
|
||||
title: poll.title,
|
||||
description: poll.description,
|
||||
status: poll.status as 'draft' | 'active' | 'closed',
|
||||
status: poll.status as "draft" | "active" | "closed",
|
||||
showResults: poll.showResults,
|
||||
multipleChoice: poll.multipleChoice,
|
||||
startsAt: poll.startsAt,
|
||||
@@ -129,5 +123,5 @@ export default async function PollDetailPage({
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
);
|
||||
}
|
||||
@@ -1,71 +1,65 @@
|
||||
'use client'
|
||||
"use client";
|
||||
|
||||
import { GripVertical, Pencil, Plus, Trash2 } from 'lucide-react'
|
||||
import { useState } from 'react'
|
||||
import { addPollQuestion, deletePollQuestion, updatePollQuestion } from '@/actions/polls'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'
|
||||
import { Input } from '@/components/ui/input'
|
||||
import { Label } from '@/components/ui/label'
|
||||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
SelectItem,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
} from '@/components/ui/select'
|
||||
import { useServerAction } from '@/hooks/use-server-action'
|
||||
import { GripVertical, Pencil, Plus, Trash2 } from "lucide-react";
|
||||
import { useState } from "react";
|
||||
import { addPollQuestion, deletePollQuestion, updatePollQuestion } from "@/actions/polls";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Label } from "@/components/ui/label";
|
||||
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
|
||||
import { useServerAction } from "@/hooks/use-server-action";
|
||||
|
||||
interface Question {
|
||||
id: number
|
||||
question: string
|
||||
type: string
|
||||
sortOrder: number
|
||||
options: string
|
||||
id: number;
|
||||
question: string;
|
||||
type: string;
|
||||
sortOrder: number;
|
||||
options: string;
|
||||
}
|
||||
|
||||
interface Props {
|
||||
pollId: number
|
||||
questions: Question[]
|
||||
pollId: number;
|
||||
questions: Question[];
|
||||
}
|
||||
|
||||
export function PollQuestionsManager({ pollId, questions }: Props) {
|
||||
const { run, isPending } = useServerAction()
|
||||
const [showForm, setShowForm] = useState(false)
|
||||
const [editingId, setEditingId] = useState<number | null>(null)
|
||||
const { run, isPending } = useServerAction();
|
||||
const [showForm, setShowForm] = useState(false);
|
||||
const [editingId, setEditingId] = useState<number | null>(null);
|
||||
const [form, setForm] = useState({
|
||||
question: '',
|
||||
type: 'single',
|
||||
question: "",
|
||||
type: "single",
|
||||
sortOrder: questions.length,
|
||||
options: '',
|
||||
})
|
||||
options: "",
|
||||
});
|
||||
|
||||
function resetForm() {
|
||||
setForm({ question: '', type: 'single', sortOrder: questions.length, options: '' })
|
||||
setEditingId(null)
|
||||
setShowForm(false)
|
||||
setForm({ question: "", type: "single", sortOrder: questions.length, options: "" });
|
||||
setEditingId(null);
|
||||
setShowForm(false);
|
||||
}
|
||||
|
||||
function handleEdit(q: Question) {
|
||||
setForm({ question: q.question, type: q.type, sortOrder: q.sortOrder, options: q.options })
|
||||
setEditingId(q.id)
|
||||
setShowForm(true)
|
||||
setForm({ question: q.question, type: q.type, sortOrder: q.sortOrder, options: q.options });
|
||||
setEditingId(q.id);
|
||||
setShowForm(true);
|
||||
}
|
||||
|
||||
function handleSubmit(e: React.FormEvent) {
|
||||
e.preventDefault()
|
||||
e.preventDefault();
|
||||
if (editingId) {
|
||||
// biome-ignore lint/suspicious/noExplicitAny: explicitly chosen here, see surrounding code
|
||||
run(() => updatePollQuestion({ id: editingId, ...form } as any), {
|
||||
successMessage: 'Question updated!',
|
||||
})
|
||||
successMessage: "Question updated!",
|
||||
});
|
||||
} else {
|
||||
// biome-ignore lint/suspicious/noExplicitAny: explicitly chosen here, see surrounding code
|
||||
run(() => addPollQuestion({ pollId, ...form } as any), {
|
||||
successMessage: 'Question added!',
|
||||
})
|
||||
successMessage: "Question added!",
|
||||
});
|
||||
}
|
||||
resetForm()
|
||||
resetForm();
|
||||
}
|
||||
|
||||
return (
|
||||
@@ -75,8 +69,8 @@ export function PollQuestionsManager({ pollId, questions }: Props) {
|
||||
<Button
|
||||
size="sm"
|
||||
onClick={() => {
|
||||
resetForm()
|
||||
setShowForm(!showForm)
|
||||
resetForm();
|
||||
setShowForm(!showForm);
|
||||
}}
|
||||
>
|
||||
<Plus className="h-4 w-4 mr-1" /> Add Question
|
||||
@@ -122,7 +116,7 @@ export function PollQuestionsManager({ pollId, questions }: Props) {
|
||||
className="w-full rounded-md border bg-transparent px-3 py-2 text-sm min-h-[80px]"
|
||||
value={form.options}
|
||||
onChange={(e) => setForm({ ...form, options: e.target.value })}
|
||||
placeholder={'Option A\nOption B\nOption C'}
|
||||
placeholder={"Option A\nOption B\nOption C"}
|
||||
/>
|
||||
</div>
|
||||
<div className="flex gap-2 justify-end">
|
||||
@@ -130,7 +124,7 @@ export function PollQuestionsManager({ pollId, questions }: Props) {
|
||||
Cancel
|
||||
</Button>
|
||||
<Button type="submit" size="sm" disabled={isPending}>
|
||||
{editingId ? 'Update' : 'Add'}
|
||||
{editingId ? "Update" : "Add"}
|
||||
</Button>
|
||||
</div>
|
||||
</form>
|
||||
@@ -155,13 +149,10 @@ export function PollQuestionsManager({ pollId, questions }: Props) {
|
||||
{q.options && (
|
||||
<div className="mt-1 text-xs text-muted-foreground">
|
||||
{q.options
|
||||
.split('\n')
|
||||
.split("\n")
|
||||
.filter(Boolean)
|
||||
.map((opt) => (
|
||||
<span
|
||||
key={opt}
|
||||
className="inline-block mr-2 bg-muted px-1.5 py-0.5 rounded mb-1"
|
||||
>
|
||||
<span key={opt} className="inline-block mr-2 bg-muted px-1.5 py-0.5 rounded mb-1">
|
||||
{opt}
|
||||
</span>
|
||||
))}
|
||||
@@ -169,12 +160,7 @@ export function PollQuestionsManager({ pollId, questions }: Props) {
|
||||
)}
|
||||
</div>
|
||||
<div className="flex gap-1 shrink-0">
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
className="h-7 w-7"
|
||||
onClick={() => handleEdit(q)}
|
||||
>
|
||||
<Button variant="ghost" size="icon" className="h-7 w-7" onClick={() => handleEdit(q)}>
|
||||
<Pencil className="h-3.5 w-3.5" />
|
||||
</Button>
|
||||
<Button
|
||||
@@ -183,7 +169,7 @@ export function PollQuestionsManager({ pollId, questions }: Props) {
|
||||
className="h-7 w-7"
|
||||
onClick={() =>
|
||||
run(() => deletePollQuestion({ id: q.id }), {
|
||||
successMessage: 'Question deleted',
|
||||
successMessage: "Question deleted",
|
||||
})
|
||||
}
|
||||
>
|
||||
@@ -195,5 +181,5 @@ export function PollQuestionsManager({ pollId, questions }: Props) {
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
)
|
||||
);
|
||||
}
|
||||
@@ -1,16 +1,16 @@
|
||||
'use client'
|
||||
"use client";
|
||||
|
||||
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'
|
||||
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
|
||||
|
||||
interface QuestionResult {
|
||||
id: number
|
||||
question: string
|
||||
options: string
|
||||
votes: string[] // array of answer strings
|
||||
id: number;
|
||||
question: string;
|
||||
options: string;
|
||||
votes: string[]; // array of answer strings
|
||||
}
|
||||
|
||||
interface Props {
|
||||
questions: QuestionResult[]
|
||||
questions: QuestionResult[];
|
||||
}
|
||||
|
||||
export function PollResultsView({ questions }: Props) {
|
||||
@@ -21,14 +21,14 @@ export function PollResultsView({ questions }: Props) {
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-6">
|
||||
{questions.map((q) => {
|
||||
const options = q.options.split('\n').filter(Boolean)
|
||||
const totalVotes = q.votes.length
|
||||
const options = q.options.split("\n").filter(Boolean);
|
||||
const totalVotes = q.votes.length;
|
||||
|
||||
// Count votes per option
|
||||
const voteCounts = new Map<string, number>()
|
||||
for (const opt of options) voteCounts.set(opt, 0)
|
||||
const voteCounts = new Map<string, number>();
|
||||
for (const opt of options) voteCounts.set(opt, 0);
|
||||
for (const vote of q.votes) {
|
||||
voteCounts.set(vote, (voteCounts.get(vote) ?? 0) + 1)
|
||||
voteCounts.set(vote, (voteCounts.get(vote) ?? 0) + 1);
|
||||
}
|
||||
|
||||
return (
|
||||
@@ -37,8 +37,8 @@ export function PollResultsView({ questions }: Props) {
|
||||
{options.length > 0 ? (
|
||||
<div className="space-y-2">
|
||||
{options.map((opt) => {
|
||||
const count = voteCounts.get(opt) ?? 0
|
||||
const pct = totalVotes > 0 ? Math.round((count / totalVotes) * 100) : 0
|
||||
const count = voteCounts.get(opt) ?? 0;
|
||||
const pct = totalVotes > 0 ? Math.round((count / totalVotes) * 100) : 0;
|
||||
return (
|
||||
<div key={opt}>
|
||||
<div className="flex justify-between text-xs mb-1">
|
||||
@@ -54,21 +54,19 @@ export function PollResultsView({ questions }: Props) {
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
) : (
|
||||
<div className="text-sm text-muted-foreground">
|
||||
{totalVotes} free-text responses
|
||||
</div>
|
||||
<div className="text-sm text-muted-foreground">{totalVotes} free-text responses</div>
|
||||
)}
|
||||
<p className="text-xs text-muted-foreground mt-2">
|
||||
{totalVotes} total vote{totalVotes !== 1 ? 's' : ''}
|
||||
{totalVotes} total vote{totalVotes !== 1 ? "s" : ""}
|
||||
</p>
|
||||
</div>
|
||||
)
|
||||
);
|
||||
})}
|
||||
</CardContent>
|
||||
</Card>
|
||||
)
|
||||
);
|
||||
}
|
||||
@@ -1,11 +1,11 @@
|
||||
import { redirect } from 'next/navigation'
|
||||
import { canAccess, getAdminContext, PERMS } from '@/lib/permissions'
|
||||
import { PollForm } from '../poll-form'
|
||||
import { redirect } from "next/navigation";
|
||||
import { canAccess, getAdminContext, PERMS } from "@/lib/permissions";
|
||||
import { PollForm } from "../poll-form";
|
||||
|
||||
export default async function CreatePollPage() {
|
||||
const { session, permissions } = await getAdminContext()
|
||||
const { session, permissions } = await getAdminContext();
|
||||
if (!canAccess(permissions, PERMS.POLLS_EDIT, session.user.rank)) {
|
||||
redirect('/admin/polls')
|
||||
redirect("/admin/polls");
|
||||
}
|
||||
|
||||
return (
|
||||
@@ -17,5 +17,5 @@ export default async function CreatePollPage() {
|
||||
|
||||
<PollForm />
|
||||
</div>
|
||||
)
|
||||
);
|
||||
}
|
||||
@@ -1,31 +1,31 @@
|
||||
import Link from 'next/link'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { fetchAdminList } from '@/lib/admin-list'
|
||||
import { PERMS } from '@/lib/permissions'
|
||||
import { PollsTable } from './polls-table'
|
||||
import Link from "next/link";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { fetchAdminList } from "@/lib/admin-list";
|
||||
import { PERMS } from "@/lib/permissions";
|
||||
import { PollsTable } from "./polls-table";
|
||||
|
||||
export default async function PollsPage({
|
||||
params: paramsPromise,
|
||||
searchParams,
|
||||
}: {
|
||||
params: Promise<{ locale: string }>
|
||||
searchParams: Promise<Record<string, string>>
|
||||
params: Promise<{ locale: string }>;
|
||||
searchParams: Promise<Record<string, string>>;
|
||||
}) {
|
||||
const result = await fetchAdminList(
|
||||
{
|
||||
permission: PERMS.POLLS_VIEW,
|
||||
model: 'websitePoll',
|
||||
searchFields: ['title'],
|
||||
defaultSort: { createdAt: 'desc' },
|
||||
model: "websitePoll",
|
||||
searchFields: ["title"],
|
||||
defaultSort: { createdAt: "desc" },
|
||||
mapRow: (p: {
|
||||
id: number
|
||||
title: string
|
||||
status: string
|
||||
showResults: number
|
||||
multipleChoice: number
|
||||
startsAt: Date | null
|
||||
endsAt: Date | null
|
||||
createdAt: Date
|
||||
id: number;
|
||||
title: string;
|
||||
status: string;
|
||||
showResults: number;
|
||||
multipleChoice: number;
|
||||
startsAt: Date | null;
|
||||
endsAt: Date | null;
|
||||
createdAt: Date;
|
||||
}) => ({
|
||||
id: p.id,
|
||||
title: p.title,
|
||||
@@ -39,7 +39,7 @@ export default async function PollsPage({
|
||||
},
|
||||
paramsPromise,
|
||||
searchParams,
|
||||
)
|
||||
);
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
@@ -63,5 +63,5 @@ export default async function PollsPage({
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
);
|
||||
}
|
||||
@@ -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>
|
||||
)
|
||||
);
|
||||
}
|
||||
@@ -1,64 +1,64 @@
|
||||
'use client'
|
||||
"use client";
|
||||
|
||||
import { Pencil } from 'lucide-react'
|
||||
import Link from 'next/link'
|
||||
import { DataTable } from '@/components/admin/data-table'
|
||||
import { Badge } from '@/components/ui/badge'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import type { DataTableColumn, PaginatedResult } from '@/types'
|
||||
import { Pencil } from "lucide-react";
|
||||
import Link from "next/link";
|
||||
import { DataTable } from "@/components/admin/data-table";
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import type { DataTableColumn, PaginatedResult } from "@/types";
|
||||
|
||||
interface PollRow {
|
||||
id: number
|
||||
title: string
|
||||
status: string
|
||||
showResults: number
|
||||
multipleChoice: number
|
||||
startsAt: Date | null
|
||||
endsAt: Date | null
|
||||
createdAt: Date
|
||||
id: number;
|
||||
title: string;
|
||||
status: string;
|
||||
showResults: number;
|
||||
multipleChoice: number;
|
||||
startsAt: Date | null;
|
||||
endsAt: Date | null;
|
||||
createdAt: Date;
|
||||
}
|
||||
|
||||
const statusVariants: Record<string, 'default' | 'secondary' | 'outline'> = {
|
||||
active: 'default',
|
||||
draft: 'secondary',
|
||||
closed: 'outline',
|
||||
}
|
||||
const statusVariants: Record<string, "default" | "secondary" | "outline"> = {
|
||||
active: "default",
|
||||
draft: "secondary",
|
||||
closed: "outline",
|
||||
};
|
||||
|
||||
function getColumns(): DataTableColumn<PollRow>[] {
|
||||
return [
|
||||
{ key: 'id', label: 'ID', sortable: true },
|
||||
{ key: "id", label: "ID", sortable: true },
|
||||
{
|
||||
key: 'title',
|
||||
label: 'Title',
|
||||
key: "title",
|
||||
label: "Title",
|
||||
sortable: true,
|
||||
render: (value) => <span className="font-medium">{String(value)}</span>,
|
||||
},
|
||||
{
|
||||
key: 'status',
|
||||
label: 'Status',
|
||||
key: "status",
|
||||
label: "Status",
|
||||
sortable: true,
|
||||
render: (value) => (
|
||||
<Badge variant={statusVariants[String(value)] ?? 'secondary'}>{String(value)}</Badge>
|
||||
<Badge variant={statusVariants[String(value)] ?? "secondary"}>{String(value)}</Badge>
|
||||
),
|
||||
},
|
||||
{
|
||||
key: 'multipleChoice',
|
||||
label: 'Type',
|
||||
key: "multipleChoice",
|
||||
label: "Type",
|
||||
render: (value) => (
|
||||
<span className="text-sm">{Number(value) ? 'Multiple choice' : 'Single choice'}</span>
|
||||
<span className="text-sm">{Number(value) ? "Multiple choice" : "Single choice"}</span>
|
||||
),
|
||||
},
|
||||
{
|
||||
key: 'createdAt',
|
||||
label: 'Created',
|
||||
key: "createdAt",
|
||||
label: "Created",
|
||||
sortable: true,
|
||||
render: (value) => (
|
||||
<span className="text-sm">{new Date(value as unknown as string).toLocaleDateString()}</span>
|
||||
),
|
||||
},
|
||||
{
|
||||
key: 'actions',
|
||||
label: 'Actions',
|
||||
key: "actions",
|
||||
label: "Actions",
|
||||
render: (_, row) => (
|
||||
<Button variant="ghost" size="icon" asChild>
|
||||
<Link href={`/admin/polls/${row.id}`}>
|
||||
@@ -67,13 +67,13 @@ function getColumns(): DataTableColumn<PollRow>[] {
|
||||
</Button>
|
||||
),
|
||||
},
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
interface PollsTableProps {
|
||||
data: PaginatedResult<PollRow>
|
||||
data: PaginatedResult<PollRow>;
|
||||
}
|
||||
|
||||
export function PollsTable({ data }: PollsTableProps) {
|
||||
return <DataTable data={data} columns={getColumns()} searchPlaceholder="Search polls..." />
|
||||
return <DataTable data={data} columns={getColumns()} searchPlaceholder="Search polls..." />;
|
||||
}
|
||||
Reference in new issue
Block a user