refactor: type PollQuestionsManager form with PollQuestionInput
This commit is contained in:
1 parent
0d027dba99
commit
907a4399d0
1 file changed
+13
-7
@@ -19,6 +19,7 @@ import {
|
||||
SelectValue,
|
||||
} from "@/components/ui/select";
|
||||
import { useServerAction } from "@/hooks/use-server-action";
|
||||
import type { PollQuestionInput } from "@/lib/validators/poll";
|
||||
|
||||
interface Question {
|
||||
id: number;
|
||||
@@ -33,11 +34,14 @@ interface Props {
|
||||
questions: Question[];
|
||||
}
|
||||
|
||||
type PollQuestionUpdateInput = PollQuestionInput & { id: number };
|
||||
|
||||
export function PollQuestionsManager({ pollId, questions }: Props) {
|
||||
const { run, isPending } = useServerAction();
|
||||
const [showForm, setShowForm] = useState(false);
|
||||
const [editingId, setEditingId] = useState<number | null>(null);
|
||||
const [form, setForm] = useState({
|
||||
const [form, setForm] = useState<PollQuestionInput>({
|
||||
pollId,
|
||||
question: "",
|
||||
type: "single",
|
||||
sortOrder: questions.length,
|
||||
@@ -46,6 +50,7 @@ export function PollQuestionsManager({ pollId, questions }: Props) {
|
||||
|
||||
function resetForm() {
|
||||
setForm({
|
||||
pollId,
|
||||
question: "",
|
||||
type: "single",
|
||||
sortOrder: questions.length,
|
||||
@@ -57,8 +62,9 @@ export function PollQuestionsManager({ pollId, questions }: Props) {
|
||||
|
||||
function handleEdit(q: Question) {
|
||||
setForm({
|
||||
pollId,
|
||||
question: q.question,
|
||||
type: q.type,
|
||||
type: q.type as PollQuestionInput["type"],
|
||||
sortOrder: q.sortOrder,
|
||||
options: q.options,
|
||||
});
|
||||
@@ -69,13 +75,11 @@ export function PollQuestionsManager({ pollId, questions }: Props) {
|
||||
function handleSubmit(e: React.FormEvent) {
|
||||
e.preventDefault();
|
||||
if (editingId) {
|
||||
// biome-ignore lint/suspicious/noExplicitAny: explicitly chosen here, see surrounding code
|
||||
run(() => updatePollQuestion({ id: editingId, ...form } as any), {
|
||||
run(() => updatePollQuestion({ id: editingId, ...form } as PollQuestionUpdateInput), {
|
||||
successMessage: "Question updated!",
|
||||
});
|
||||
} else {
|
||||
// biome-ignore lint/suspicious/noExplicitAny: explicitly chosen here, see surrounding code
|
||||
run(() => addPollQuestion({ pollId, ...form } as any), {
|
||||
run(() => addPollQuestion(form), {
|
||||
successMessage: "Question added!",
|
||||
});
|
||||
}
|
||||
@@ -115,7 +119,9 @@ export function PollQuestionsManager({ pollId, questions }: Props) {
|
||||
<Label>Type</Label>
|
||||
<Select
|
||||
value={form.type}
|
||||
onValueChange={(v) => setForm({ ...form, type: v })}
|
||||
onValueChange={(v) =>
|
||||
setForm({ ...form, type: v as PollQuestionInput["type"] })
|
||||
}
|
||||
>
|
||||
<SelectTrigger>
|
||||
<SelectValue />
|
||||
|
||||
Reference in new issue
Block a user