fix grammar feedback thing

This commit is contained in:
adueck 2024-03-02 23:19:41 -05:00
parent 6cb0cc1740
commit 0da29f4abb
1 changed files with 164 additions and 134 deletions

View File

@ -1,4 +1,4 @@
import { useState } from "react"; import { useEffect, useState } from "react";
import { useUser } from "../user-context"; import { useUser } from "../user-context";
const ratings = [ const ratings = [
@ -6,16 +6,22 @@ const ratings = [
{ value: 1, emoji: "😕" }, { value: 1, emoji: "😕" },
{ value: 2, emoji: "🙂" }, { value: 2, emoji: "🙂" },
{ value: 3, emoji: "🤩" }, { value: 3, emoji: "🤩" },
] ];
function ChapterFeedback({ chapter }: { chapter: string }) { function ChapterFeedback({ chapter }: { chapter: string }) {
const { user } = useUser(); const { user } = useUser();
const [rating, setRating] = useState<number | undefined>(undefined); const [rating, setRating] = useState<number | undefined>(undefined);
const [anonymous, setAnonymous] = useState<boolean>(false); const [anonymous, setAnonymous] = useState<boolean>(false);
const [feedback, setFeedback] = useState<string>(""); const [feedback, setFeedback] = useState<string>("");
const [feedbackStatus, setFeedbackStatus] = useState<"unsent" | "sending" | "sent">("unsent") const [feedbackStatus, setFeedbackStatus] = useState<
"unsent" | "sending" | "sent"
>("unsent");
useEffect(() => {
setFeedbackStatus("unsent");
setFeedback("");
}, [chapter]);
function handleRatingClick(v: number) { function handleRatingClick(v: number) {
setRating(o => o === v ? undefined : v); setRating((o) => (o === v ? undefined : v));
} }
// automatic sending of emoji click feedback if someone leaves the chapter without // automatic sending of emoji click feedback if someone leaves the chapter without
// filling in and sending the feedback textbox // filling in and sending the feedback textbox
@ -53,28 +59,37 @@ function ChapterFeedback({ chapter }: { chapter: string }) {
"Content-Type": "application/json", "Content-Type": "application/json",
}, },
body: JSON.stringify(toSend), body: JSON.stringify(toSend),
}).then(res => res.json()).then(res => { })
.then((res) => res.json())
.then((res) => {
if (res.ok) { if (res.ok) {
setFeedbackStatus("sent"); setFeedbackStatus("sent");
} else { } else {
setFeedbackStatus("unsent"); setFeedbackStatus("unsent");
alert("error sending feedback"); alert("error sending feedback");
} }
}).catch(() => { })
.catch(() => {
setFeedbackStatus("unsent"); setFeedbackStatus("unsent");
alert("connect to the internet to send feedback"); alert("connect to the internet to send feedback");
}); });
} }
if (feedbackStatus === "sent") { if (feedbackStatus === "sent") {
return <div> return (
<div>
<hr /> <hr />
<div className="d-flex flex-row justify-content-center align-items-center my-3" style={{ height: "6rem" }}> <div
className="d-flex flex-row justify-content-center align-items-center my-3"
style={{ height: "6rem" }}
>
<div className="lead">Thanks for your feedback!</div> <div className="lead">Thanks for your feedback!</div>
</div> </div>
<hr /> <hr />
</div> </div>
);
} }
return <div> return (
<div>
<hr /> <hr />
<div className="my-4"> <div className="my-4">
<div className="text-center">Was this chapter helpful?</div> <div className="text-center">Was this chapter helpful?</div>
@ -83,62 +98,77 @@ function ChapterFeedback({ chapter }: { chapter: string }) {
<div <div
key={value} key={value}
className="mx-2 clickable" className="mx-2 clickable"
style={rating !== value style={
rating !== value
? { ? {
filter: "grayscale(100%)", filter: "grayscale(100%)",
fontSize: "2.25rem", fontSize: "2.25rem",
} : { fontSize: "2.75rem" }} }
: { fontSize: "2.75rem" }
}
onClick={() => handleRatingClick(value)} onClick={() => handleRatingClick(value)}
> >
{emoji} {emoji}
</div> </div>
))} ))}
</div> </div>
{rating !== undefined && <div style={{ maxWidth: "30rem", margin: "0 auto" }}> {rating !== undefined && (
<div style={{ maxWidth: "30rem", margin: "0 auto" }}>
<div className="form-group"> <div className="form-group">
<label htmlFor="feedbackText" className="text-left">Feedback:</label> <label htmlFor="feedbackText" className="text-left">
Feedback:
</label>
<textarea <textarea
className="form-control" className="form-control"
id="feedbackText" id="feedbackText"
placeholder="Share more or leave blank" placeholder="Share more or leave blank"
rows={3} rows={3}
value={feedback} value={feedback}
onChange={e => setFeedback(e.target.value)} onChange={(e) => setFeedback(e.target.value)}
/> />
</div> </div>
<div className="d-flex flex-row justify-content-between align-items-center"> <div className="d-flex flex-row justify-content-between align-items-center">
<div className="small"> <div className="small">
{(user && !anonymous) {user && !anonymous
? `Private feedback will be sent as "${user.name}"` ? `Private feedback will be sent as "${user.name}"`
: `Private feedback will be anonymous`} : `Private feedback will be anonymous`}
</div> </div>
<div className="d-flex flex-row align-items-center"> <div className="d-flex flex-row align-items-center">
{feedbackStatus === "sending" && <div className="mr-3"> {feedbackStatus === "sending" && (
<div className="mr-3">
<samp>sending...</samp> <samp>sending...</samp>
</div>} </div>
)}
<div> <div>
<button className="btn btn-sm btn-primary" onClick={handleSendFeedback}>Send</button> <button
className="btn btn-sm btn-primary"
onClick={handleSendFeedback}
>
Send
</button>
</div> </div>
</div> </div>
</div> </div>
{user && <div className="form-check small"> {user && (
<div className="form-check small">
<input <input
className="form-check-input" className="form-check-input"
type="checkbox" type="checkbox"
checked={anonymous} checked={anonymous}
onChange={e => setAnonymous(e.target.checked)} onChange={(e) => setAnonymous(e.target.checked)}
id="anonymounCheck" id="anonymounCheck"
/> />
<label className="form-check-label" htmlFor="anonymousCheck"> <label className="form-check-label" htmlFor="anonymousCheck">
stay anonymouns stay anonymouns
</label> </label>
</div>} </div>
</div>} )}
</div>
)}
</div> </div>
<hr /> <hr />
</div>; </div>
);
} }
export default ChapterFeedback; export default ChapterFeedback;