diff --git a/src/components/ChapterFeedback.tsx b/src/components/ChapterFeedback.tsx index 0a4dc4c..6fe3a5c 100644 --- a/src/components/ChapterFeedback.tsx +++ b/src/components/ChapterFeedback.tsx @@ -1,144 +1,174 @@ -import { useState } from "react"; +import { useEffect, useState } from "react"; import { useUser } from "../user-context"; const ratings = [ - { value: 0, emoji: "😭" }, - { value: 1, emoji: "😕" }, - { value: 2, emoji: "🙂" }, - { value: 3, emoji: "🤩" }, -] + { value: 0, emoji: "😭" }, + { value: 1, emoji: "😕" }, + { value: 2, emoji: "🙂" }, + { value: 3, emoji: "🤩" }, +]; function ChapterFeedback({ chapter }: { chapter: string }) { - const { user } = useUser(); - const [rating, setRating] = useState(undefined); - const [anonymous, setAnonymous] = useState(false); - const [feedback, setFeedback] = useState(""); - const [feedbackStatus, setFeedbackStatus] = useState<"unsent" | "sending" | "sent">("unsent") - function handleRatingClick(v: number) { - setRating(o => o === v ? undefined : v); - } - // automatic sending of emoji click feedback if someone leaves the chapter without - // filling in and sending the feedback textbox - // not doing this yet because not sure if it's a clean setup like "componentWillUnmount" - // or if it unecessarily unmounts and re-renders - // useEffect(() => { - // return () => { - // if (rating === undefined || feedbackStatus === "sent") return; - // fetch("https://account.lingdocs.com/feedback", { - // method: "PUT", - // credentials: "include", - // headers: { - // "Content-Type": "application/json", - // }, - // body: JSON.stringify({ feedback: "", rating }), - // }).catch(() => { - // console.error("couldn't send chapter feedback"); - // }); - // } - // // eslint-disable-next-line - // }, []); - function handleSendFeedback() { - const toSend = { - chapter, - feedback, - rating, - anonymous, - ts: Date.now(), - }; - setFeedbackStatus("sending"); - fetch("https://account.lingdocs.com/feedback", { - method: "PUT", - credentials: "include", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify(toSend), - }).then(res => res.json()).then(res => { - if (res.ok) { - setFeedbackStatus("sent"); - } else { - setFeedbackStatus("unsent"); - alert("error sending feedback"); - } - }).catch(() => { - setFeedbackStatus("unsent"); - alert("connect to the internet to send feedback"); - }); - } - if (feedbackStatus === "sent") { - return
-
-
-
Thanks for your feedback!
-
-
-
- } - return
+ const { user } = useUser(); + const [rating, setRating] = useState(undefined); + const [anonymous, setAnonymous] = useState(false); + const [feedback, setFeedback] = useState(""); + const [feedbackStatus, setFeedbackStatus] = useState< + "unsent" | "sending" | "sent" + >("unsent"); + useEffect(() => { + setFeedbackStatus("unsent"); + setFeedback(""); + }, [chapter]); + function handleRatingClick(v: number) { + setRating((o) => (o === v ? undefined : v)); + } + // automatic sending of emoji click feedback if someone leaves the chapter without + // filling in and sending the feedback textbox + // not doing this yet because not sure if it's a clean setup like "componentWillUnmount" + // or if it unecessarily unmounts and re-renders + // useEffect(() => { + // return () => { + // if (rating === undefined || feedbackStatus === "sent") return; + // fetch("https://account.lingdocs.com/feedback", { + // method: "PUT", + // credentials: "include", + // headers: { + // "Content-Type": "application/json", + // }, + // body: JSON.stringify({ feedback: "", rating }), + // }).catch(() => { + // console.error("couldn't send chapter feedback"); + // }); + // } + // // eslint-disable-next-line + // }, []); + function handleSendFeedback() { + const toSend = { + chapter, + feedback, + rating, + anonymous, + ts: Date.now(), + }; + setFeedbackStatus("sending"); + fetch("https://account.lingdocs.com/feedback", { + method: "PUT", + credentials: "include", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(toSend), + }) + .then((res) => res.json()) + .then((res) => { + if (res.ok) { + setFeedbackStatus("sent"); + } else { + setFeedbackStatus("unsent"); + alert("error sending feedback"); + } + }) + .catch(() => { + setFeedbackStatus("unsent"); + alert("connect to the internet to send feedback"); + }); + } + if (feedbackStatus === "sent") { + return ( +

-
-
Was this chapter helpful?
-
- {ratings.map(({ value, emoji }) => ( -
handleRatingClick(value)} - > - {emoji} -
- ))} -
- {rating !== undefined &&
-
- -