From f985797a218949c683ec4288600ed354d61bb20a Mon Sep 17 00:00:00 2001 From: Naomi Carrigan Date: Tue, 12 Nov 2024 01:19:00 +0000 Subject: [PATCH] feat: add page for anonymous questions (#40) ### Explanation _No response_ ### Issue _No response_ ### Attestations - [x] I have read and agree to the [Code of Conduct](https://docs.nhcarrigan.com/community/coc/) - [x] I have read and agree to the [Community Guidelines](https://docs.nhcarrigan.com/community/guide/). - [x] My contribution complies with the [Contributor Covenant](https://docs.nhcarrigan.com/dev/covenant/). ### Dependencies - [x] I have pinned the dependencies to a specific patch version. ### Style - [x] I have run the linter and resolved any errors. - [x] My pull request uses an appropriate title, matching the conventional commit standards. - [x] My scope of feat/fix/chore/etc. correctly matches the nature of changes in my pull request. ### Tests - [ ] My contribution adds new code, and I have added tests to cover it. - [ ] My contribution modifies existing code, and I have updated the tests to reflect these changes. - [x] All new and existing tests pass locally with my changes. - [x] Code coverage remains at or above the configured threshold. ### Documentation _No response_ ### Versioning Minor - My pull request introduces a new non-breaking feature. Reviewed-on: https://codeberg.org/nhcarrigan/portfolio/pulls/40 Co-authored-by: Naomi Carrigan Co-committed-by: Naomi Carrigan --- src/app/ask/page.tsx | 169 +++++++++++++++++++++++++++++++++++++++++ src/config/NavItems.ts | 1 + 2 files changed, 170 insertions(+) create mode 100644 src/app/ask/page.tsx diff --git a/src/app/ask/page.tsx b/src/app/ask/page.tsx new file mode 100644 index 0000000..c87e12d --- /dev/null +++ b/src/app/ask/page.tsx @@ -0,0 +1,169 @@ +/** + * @copyright nhcarrigan + * @license Naomi's Public License + * @author Naomi Carrigan + */ +"use client"; +import { useState, type ChangeEvent, type JSX } from "react"; +import { Rule } from "../../components/rule"; + +type Category = "question" | "compliment" | "confess" | "never"; + +const ColourMap: Record + = { + compliment: { + background: "#a6f1c6", + foreground: "#0f9d58", + }, + confess: { + background: "#f7c6d2", + foreground: "#b03060", + }, + never: { + background: "#f7e6c6", + foreground: "#b8860b", + }, + question: { + background: "#a6e6fc", + foreground: "#03779e", + }, + }; + +const TitleMap: Record = { + compliment: "Pay Naomi a Compliment", + confess: "Confess Your Sins to Naomi", + never: "Never Have I Ever?", + question: "Ask Naomi a Question", +}; + +const DescriptionMap: Record = { + compliment: + // eslint-disable-next-line stylistic/max-len + "Have you wanted to tell Naomi she's cute, but you've been too shy? Here's your chance!", + confess: + // eslint-disable-next-line stylistic/max-len + "Is there something weighing on your mind that you can't tell anyone else? Confess it here!", + never: + // eslint-disable-next-line stylistic/max-len + "What do you think Naomi has done that you haven't? Let's play a game of Never Have I Ever!", + question: "Do you have a question for Naomi? Ask it here!", +}; + +/** + * Renders the /ask page. + * @returns A React Component. + */ +const Ask = (): JSX.Element => { + const [ category, setCategory ] = useState("question"); + const [ question, setQuestion ] = useState(""); + const [ submitted, setSubmitted ] = useState(false); + const [ errored, setErrored ] = useState(false); + + const handleCategoryChange = ( + event: ChangeEvent, + ): void => { + // eslint-disable-next-line @typescript-eslint/consistent-type-assertions + setCategory(event.target.value as Category); + }; + + const handleQuestionUpdate = ( + event: ChangeEvent, + ): void => { + setQuestion(event.target.value); + }; + + const submitForm = (): void => { + fetch("https://anon.nhcarrigan.com/api", { + body: JSON.stringify({ + category, + question, + }), + headers: { + "Content-Type": "application/json", + }, + method: "POST", + }).then((response) => { + if (response.ok) { + setSubmitted(true); + } else { + setErrored(true); + } + }). + catch(() => { + setErrored(true); + }); + }; + return ( +
+

{`Ask Me Anything!`}

+

+ {`This page allows you to anonymously ask any question you might wish! Once approved and answered, your question and our response will be visible in our `} + {`Discord`} + {`!`} +

+ + {!submitted && !errored &&
+

{TitleMap[category]}

+

{DescriptionMap[category]}

+
+ +