fix grammar feedback thing
This commit is contained in:
parent
6cb0cc1740
commit
0da29f4abb
|
@ -1,144 +1,174 @@
|
||||||
import { useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import { useUser } from "../user-context";
|
import { useUser } from "../user-context";
|
||||||
|
|
||||||
const ratings = [
|
const ratings = [
|
||||||
{ value: 0, emoji: "😭" },
|
{ value: 0, emoji: "😭" },
|
||||||
{ 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<
|
||||||
function handleRatingClick(v: number) {
|
"unsent" | "sending" | "sent"
|
||||||
setRating(o => o === v ? undefined : v);
|
>("unsent");
|
||||||
}
|
useEffect(() => {
|
||||||
// automatic sending of emoji click feedback if someone leaves the chapter without
|
setFeedbackStatus("unsent");
|
||||||
// filling in and sending the feedback textbox
|
setFeedback("");
|
||||||
// not doing this yet because not sure if it's a clean setup like "componentWillUnmount"
|
}, [chapter]);
|
||||||
// or if it unecessarily unmounts and re-renders
|
function handleRatingClick(v: number) {
|
||||||
// useEffect(() => {
|
setRating((o) => (o === v ? undefined : v));
|
||||||
// return () => {
|
}
|
||||||
// if (rating === undefined || feedbackStatus === "sent") return;
|
// automatic sending of emoji click feedback if someone leaves the chapter without
|
||||||
// fetch("https://account.lingdocs.com/feedback", {
|
// filling in and sending the feedback textbox
|
||||||
// method: "PUT",
|
// not doing this yet because not sure if it's a clean setup like "componentWillUnmount"
|
||||||
// credentials: "include",
|
// or if it unecessarily unmounts and re-renders
|
||||||
// headers: {
|
// useEffect(() => {
|
||||||
// "Content-Type": "application/json",
|
// return () => {
|
||||||
// },
|
// if (rating === undefined || feedbackStatus === "sent") return;
|
||||||
// body: JSON.stringify({ feedback: "", rating }),
|
// fetch("https://account.lingdocs.com/feedback", {
|
||||||
// }).catch(() => {
|
// method: "PUT",
|
||||||
// console.error("couldn't send chapter feedback");
|
// credentials: "include",
|
||||||
// });
|
// headers: {
|
||||||
// }
|
// "Content-Type": "application/json",
|
||||||
// // eslint-disable-next-line
|
// },
|
||||||
// }, []);
|
// body: JSON.stringify({ feedback: "", rating }),
|
||||||
function handleSendFeedback() {
|
// }).catch(() => {
|
||||||
const toSend = {
|
// console.error("couldn't send chapter feedback");
|
||||||
chapter,
|
// });
|
||||||
feedback,
|
// }
|
||||||
rating,
|
// // eslint-disable-next-line
|
||||||
anonymous,
|
// }, []);
|
||||||
ts: Date.now(),
|
function handleSendFeedback() {
|
||||||
};
|
const toSend = {
|
||||||
setFeedbackStatus("sending");
|
chapter,
|
||||||
fetch("https://account.lingdocs.com/feedback", {
|
feedback,
|
||||||
method: "PUT",
|
rating,
|
||||||
credentials: "include",
|
anonymous,
|
||||||
headers: {
|
ts: Date.now(),
|
||||||
"Content-Type": "application/json",
|
};
|
||||||
},
|
setFeedbackStatus("sending");
|
||||||
body: JSON.stringify(toSend),
|
fetch("https://account.lingdocs.com/feedback", {
|
||||||
}).then(res => res.json()).then(res => {
|
method: "PUT",
|
||||||
if (res.ok) {
|
credentials: "include",
|
||||||
setFeedbackStatus("sent");
|
headers: {
|
||||||
} else {
|
"Content-Type": "application/json",
|
||||||
setFeedbackStatus("unsent");
|
},
|
||||||
alert("error sending feedback");
|
body: JSON.stringify(toSend),
|
||||||
}
|
})
|
||||||
}).catch(() => {
|
.then((res) => res.json())
|
||||||
setFeedbackStatus("unsent");
|
.then((res) => {
|
||||||
alert("connect to the internet to send feedback");
|
if (res.ok) {
|
||||||
});
|
setFeedbackStatus("sent");
|
||||||
}
|
} else {
|
||||||
if (feedbackStatus === "sent") {
|
setFeedbackStatus("unsent");
|
||||||
return <div>
|
alert("error sending feedback");
|
||||||
<hr/>
|
}
|
||||||
<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>
|
.catch(() => {
|
||||||
</div>
|
setFeedbackStatus("unsent");
|
||||||
<hr/>
|
alert("connect to the internet to send feedback");
|
||||||
</div>
|
});
|
||||||
}
|
}
|
||||||
return <div>
|
if (feedbackStatus === "sent") {
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
<hr />
|
<hr />
|
||||||
<div className="my-4">
|
<div
|
||||||
<div className="text-center">Was this chapter helpful?</div>
|
className="d-flex flex-row justify-content-center align-items-center my-3"
|
||||||
<div className="d-flex flex-row align-items-center justify-content-center">
|
style={{ height: "6rem" }}
|
||||||
{ratings.map(({ value, emoji }) => (
|
>
|
||||||
<div
|
<div className="lead">Thanks for your feedback!</div>
|
||||||
key={value}
|
|
||||||
className="mx-2 clickable"
|
|
||||||
style={rating !== value
|
|
||||||
? {
|
|
||||||
filter: "grayscale(100%)",
|
|
||||||
fontSize: "2.25rem",
|
|
||||||
} : { fontSize: "2.75rem" }}
|
|
||||||
onClick={() => handleRatingClick(value)}
|
|
||||||
>
|
|
||||||
{emoji}
|
|
||||||
</div>
|
|
||||||
))}
|
|
||||||
</div>
|
|
||||||
{rating !== undefined && <div style={{ maxWidth: "30rem", margin: "0 auto" }}>
|
|
||||||
<div className="form-group">
|
|
||||||
<label htmlFor="feedbackText" className="text-left">Feedback:</label>
|
|
||||||
<textarea
|
|
||||||
className="form-control"
|
|
||||||
id="feedbackText"
|
|
||||||
placeholder="Share more or leave blank"
|
|
||||||
rows={3}
|
|
||||||
value={feedback}
|
|
||||||
onChange={e => setFeedback(e.target.value)}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
<div className="d-flex flex-row justify-content-between align-items-center">
|
|
||||||
<div className="small">
|
|
||||||
{(user && !anonymous)
|
|
||||||
? `Private feedback will be sent as "${user.name}"`
|
|
||||||
: `Private feedback will be anonymous`}
|
|
||||||
</div>
|
|
||||||
<div className="d-flex flex-row align-items-center">
|
|
||||||
{feedbackStatus === "sending" && <div className="mr-3">
|
|
||||||
<samp>sending...</samp>
|
|
||||||
</div>}
|
|
||||||
<div>
|
|
||||||
<button className="btn btn-sm btn-primary" onClick={handleSendFeedback}>Send</button>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
{user && <div className="form-check small">
|
|
||||||
<input
|
|
||||||
className="form-check-input"
|
|
||||||
type="checkbox"
|
|
||||||
checked={anonymous}
|
|
||||||
onChange={e => setAnonymous(e.target.checked)}
|
|
||||||
id="anonymounCheck"
|
|
||||||
/>
|
|
||||||
<label className="form-check-label" htmlFor="anonymousCheck">
|
|
||||||
stay anonymouns
|
|
||||||
</label>
|
|
||||||
</div>}
|
|
||||||
</div>}
|
|
||||||
</div>
|
</div>
|
||||||
<hr />
|
<hr />
|
||||||
</div>;
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<hr />
|
||||||
|
<div className="my-4">
|
||||||
|
<div className="text-center">Was this chapter helpful?</div>
|
||||||
|
<div className="d-flex flex-row align-items-center justify-content-center">
|
||||||
|
{ratings.map(({ value, emoji }) => (
|
||||||
|
<div
|
||||||
|
key={value}
|
||||||
|
className="mx-2 clickable"
|
||||||
|
style={
|
||||||
|
rating !== value
|
||||||
|
? {
|
||||||
|
filter: "grayscale(100%)",
|
||||||
|
fontSize: "2.25rem",
|
||||||
|
}
|
||||||
|
: { fontSize: "2.75rem" }
|
||||||
|
}
|
||||||
|
onClick={() => handleRatingClick(value)}
|
||||||
|
>
|
||||||
|
{emoji}
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
{rating !== undefined && (
|
||||||
|
<div style={{ maxWidth: "30rem", margin: "0 auto" }}>
|
||||||
|
<div className="form-group">
|
||||||
|
<label htmlFor="feedbackText" className="text-left">
|
||||||
|
Feedback:
|
||||||
|
</label>
|
||||||
|
<textarea
|
||||||
|
className="form-control"
|
||||||
|
id="feedbackText"
|
||||||
|
placeholder="Share more or leave blank"
|
||||||
|
rows={3}
|
||||||
|
value={feedback}
|
||||||
|
onChange={(e) => setFeedback(e.target.value)}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div className="d-flex flex-row justify-content-between align-items-center">
|
||||||
|
<div className="small">
|
||||||
|
{user && !anonymous
|
||||||
|
? `Private feedback will be sent as "${user.name}"`
|
||||||
|
: `Private feedback will be anonymous`}
|
||||||
|
</div>
|
||||||
|
<div className="d-flex flex-row align-items-center">
|
||||||
|
{feedbackStatus === "sending" && (
|
||||||
|
<div className="mr-3">
|
||||||
|
<samp>sending...</samp>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
<div>
|
||||||
|
<button
|
||||||
|
className="btn btn-sm btn-primary"
|
||||||
|
onClick={handleSendFeedback}
|
||||||
|
>
|
||||||
|
Send
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{user && (
|
||||||
|
<div className="form-check small">
|
||||||
|
<input
|
||||||
|
className="form-check-input"
|
||||||
|
type="checkbox"
|
||||||
|
checked={anonymous}
|
||||||
|
onChange={(e) => setAnonymous(e.target.checked)}
|
||||||
|
id="anonymounCheck"
|
||||||
|
/>
|
||||||
|
<label className="form-check-label" htmlFor="anonymousCheck">
|
||||||
|
stay anonymouns
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
<hr />
|
||||||
|
</div>
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export default ChapterFeedback;
|
||||||
|
|
||||||
export default ChapterFeedback;
|
|
||||||
|
|
Loading…
Reference in New Issue