File size: 3,558 Bytes
b136cbe | 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 | import { useState } from "react";
import { db, auth } from "../firebase";
import { doc, setDoc } from "firebase/firestore";
const PostSurveyForm = ({ onComplete }) => {
const [difficulty, setDifficulty] = useState("");
const [feedback, setFeedback] = useState("");
const [error, setError] = useState("");
const handleSubmit = async (e) => {
e.preventDefault();
if (!difficulty) {
setError("Please select a difficulty rating.");
return;
}
try {
await setDoc(doc(db, "participants", auth.currentUser.uid), {
postSurvey: {
difficulty: parseInt(difficulty, 10),
feedback: feedback.trim() || null,
submittedAt: new Date().toISOString()
},
postSurveyCompleted: true,
studyCompleted: true,
studyCompletedAt: new Date().toISOString()
}, { merge: true });
onComplete();
} catch (err) {
console.error("Error saving post-survey feedback:", err);
setError("An error occurred. Please try again.");
}
};
return (
<div style={{ paddingLeft: "2rem" }}>
<form
onSubmit={handleSubmit}
style={{
display: "flex",
flexDirection: "column",
gap: "1.5rem",
maxWidth: "600px"
}}
>
<h2>Post-Study Questions</h2>
{error && <p style={{ color: "red" }}>{error}</p>}
<div>
<label style={{ display: "block", marginBottom: "0.5rem", fontWeight: "bold" }}>
How difficult was this task? *
</label>
<div style={{ display: "flex", flexDirection: "column", gap: "0.5rem" }}>
{[1, 2, 3, 4, 5].map((value) => (
<label
key={value}
style={{
display: "flex",
alignItems: "center",
gap: "0.5rem",
cursor: "pointer"
}}
>
<input
type="radio"
name="difficulty"
value={value}
checked={difficulty === String(value)}
onChange={(e) => setDifficulty(e.target.value)}
style={{ cursor: "pointer" }}
/>
<span>
{value === 1 && "1 - Very Easy"}
{value === 2 && "2 - Easy"}
{value === 3 && "3 - Moderate"}
{value === 4 && "4 - Difficult"}
{value === 5 && "5 - Very Difficult"}
</span>
</label>
))}
</div>
</div>
<label style={{ display: "flex", flexDirection: "column", gap: "0.5rem" }}>
<span style={{ fontWeight: "bold" }}>
Additional comments or feedback (optional):
</span>
<textarea
value={feedback}
onChange={(e) => setFeedback(e.target.value)}
rows={5}
style={{
padding: "0.5rem",
fontFamily: "inherit",
fontSize: "inherit",
resize: "vertical"
}}
placeholder="Please share any thoughts about your experience..."
/>
</label>
<button
type="submit"
style={{
width: "fit-content",
alignSelf: "flex-start",
padding: "0.5rem 1rem",
cursor: "pointer"
}}
>
Submit
</button>
</form>
</div>
);
};
export default PostSurveyForm;
|