-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquiz-question.tsx
195 lines (169 loc) · 5.26 KB
/
quiz-question.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import axios from "axios";
import Link from "next/link";
import { FormEvent, useState } from "react";
import {
CheckAnswerPayload,
CheckAnswerResponse,
} from "../pages/api/check-answer";
import PrimaryButton from "./primary-button";
import invariant from "tiny-invariant";
import { useWeb3 } from "@3rdweb/hooks";
type Props = {
questionIndex: number;
questionText: string;
image?: string;
answers: string[];
nextQuestionFunction: () => void;
};
type AnswerResult = "correct" | "incorrect";
export default function QuizQuestion({
questionIndex,
questionText,
image,
answers,
nextQuestionFunction,
}: Props) {
const [answerIndex, setAnswerIndex] = useState<number | undefined>(undefined);
const [submitting, setSubmitting] = useState(false);
const [error, setError] = useState<string | undefined>(undefined);
const [answerResult, setAnswerResult] = useState<AnswerResult | undefined>(
undefined
);
const [correctAnswerWas, setCorrectAnswerWas] = useState<number | undefined>(
undefined
);
//insert code; later added "provider"
const { address, provider } = useWeb3();
if (!address) {
return <p>Please connect your wallet to take the quiz</p>
}
const handleSubmit = async (e: FormEvent) => {
e.preventDefault();
setSubmitting(true);
//insert code
const payload: CheckAnswerPayload = {
address,
questionIndex,
answerIndex,
};
const checkResponse = await axios.post("/api/check-answer", payload);
try {
invariant(
answerIndex !== undefined,
"Answer index is required to submit"
);
//insert code
invariant(
provider !== undefined, "Provider must be defined to submit an answer"
);
const message = "Please sign this message to confirm your identity and submit the answer. This wont cost any gas!"
const signedMessage = await
provider.getSigner().signMessage(message);
const payload: CheckAnswerPayload = {
questionIndex,
answerIndex,
message,
signedMessage,
};
const checkResponse = await axios.post("/api/check-answer", payload);
const result = checkResponse.data as CheckAnswerResponse;
if (result.kind === "error") {
setError(result.error);
}
if (result.kind === "correct") {
setAnswerResult("correct");
setCorrectAnswerWas(answerIndex);
}
if (result.kind === "incorrect") {
setAnswerResult("incorrect");
setCorrectAnswerWas(result.correctAnswerIndex);
}
} finally {
setSubmitting(false);
}
};
const renderResult = () => {
if (submitting) {
return <PrimaryButton disabled={true}>Checking Answer...</PrimaryButton>;
}
if (answerResult === "correct") {
return (
<>
<p className="text-green-800">
Congratulations! That was the right answer!
</p>
<p>
A pack will be sent to you shortly. You'll be able to check it out
and open it in the{" "}
<Link href="/lounge">
<a className="underline hover:no-underline">lounge</a>
</Link>
!
</p>
</>
);
}
if (answerResult === "incorrect") {
return <p className="text-red-800">Sorry, that was incorrect!</p>;
}
return (
<>
<PrimaryButton
type="submit"
onClick={handleSubmit}
disabled={answerIndex === undefined}
>
Check Answer
</PrimaryButton>
{error ? <p className="text-red-500">{error}</p> : null}
</>
);
};
return (
<form>
<div className="flex flex-col gap-4">
<div>
<div className="flex flex-col gap-2">
<label className="font-medium text-lg text-gray-900">
{questionText}
</label>
{image ? (
<img src={image} className="object-cover h-48 w-96" alt="" />
) : null}
</div>
<fieldset className="mt-4">
<div className="space-y-4">
{answers.map((answerText, i) => (
<div key={i} className="flex items-center">
<input
id={i.toString()}
name="quiz-answer"
type="radio"
className="focus:ring-indigo-500 h-4 w-4 text-indigo-600 border-gray-300 peer disabled:cursor-not-allowed"
value={i}
checked={answerIndex === i}
onChange={(e) => setAnswerIndex(Number(e.target.value))}
disabled={answerResult !== undefined}
/>
<label
htmlFor={i.toString()}
className="ml-3 block text-sm font-medium text-gray-700 peer-disabled:text-gray-500 peer-disabled:cursor-not-allowed"
>
{answerText}
{i === correctAnswerWas ? <span> ✅</span> : null}
</label>
</div>
))}
</div>
</fieldset>
</div>
{renderResult()}
{answerResult !== undefined ? (
<PrimaryButton onClick={nextQuestionFunction}>
Next Question
</PrimaryButton>
) : null}
</div>
</form>
);
}