Skip to content

Commit 7ea9c45

Browse files
committed
add server action
1 parent 80273c8 commit 7ea9c45

File tree

3 files changed

+25
-13
lines changed

3 files changed

+25
-13
lines changed

packages/nextjs/app/lib/actions.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"use server";
2+
3+
export default async function createAnswer(formData: FormData) {
4+
const rawFormData = {
5+
radio: formData.get("radio-0"),
6+
};
7+
// Test it out:
8+
console.log(rawFormData);
9+
}
Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
1+
import createAnswer from "~~/app/lib/actions";
12
import { Question } from "~~/app/lib/definitions";
23

34
export default function VotingForm({ question }: { question: Question }) {
45
return (
5-
<div className="items-left mb-4">
6-
<div>{question.question}</div>
7-
{question.choices_array.map(choice => (
8-
<div className="flex items-center mb-4" key={choice}>
9-
<label className="label cursor-pointer ms-2">
10-
<input type="radio" name="radio-10" className="radio checked:bg-red-500" />
11-
<span className="ms-2">{choice}</span>
12-
</label>
13-
</div>
14-
))}
15-
<button className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 m-4 rounded">Submit</button>
16-
</div>
6+
<form action={createAnswer}>
7+
<div className="items-left m-8">
8+
<div>{question.question}</div>
9+
{question.choices_array.map(choice => (
10+
<div className="flex items-center mb-4" key={choice}>
11+
<label className="label cursor-pointer ms-2">
12+
<input type="radio" name="radio-0" value={choice} className="radio checked:bg-red-500" />
13+
<span className="ms-2">{choice}</span>
14+
</label>
15+
</div>
16+
))}
17+
<button className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 m-4 rounded">Submit</button>
18+
</div>
19+
</form>
1720
);
1821
}

packages/nextjs/app/voting/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default async function Page() {
55
const questions = await fetchQuestions();
66
return (
77
<>
8-
<VotingForm question={questions[0]} />
8+
<VotingForm question={questions[2]} />
99
</>
1010
);
1111
}

0 commit comments

Comments
 (0)