From 96624f51e2030e8adcbfb4f6760288930e0e9b14 Mon Sep 17 00:00:00 2001 From: Oliver Barnwell Date: Sat, 27 Jul 2024 17:05:50 +0100 Subject: [PATCH] add optional name and email fields for feedback --- src/components/Feedback/Feedback.tsx | 47 ++++++++++++++----- .../components/InputField/InputField.tsx | 4 +- src/pages/api/feedback/index.page.ts | 21 +++++---- 3 files changed, 51 insertions(+), 21 deletions(-) diff --git a/src/components/Feedback/Feedback.tsx b/src/components/Feedback/Feedback.tsx index 26a7e2e0e..e8ccffb55 100644 --- a/src/components/Feedback/Feedback.tsx +++ b/src/components/Feedback/Feedback.tsx @@ -1,8 +1,9 @@ import { Stack } from '@mui/material'; import React, { useRef, useState } from 'react'; +import Badge from '../../design-system/components/Badge'; import Button from '../../design-system/components/Button'; import Box from '../Box'; -import Badge from '../../design-system/components/Badge'; +import InputField from '../../design-system/components/InputField'; enum FeedbackState { SUCCESS = 0, @@ -13,21 +14,28 @@ enum FeedbackState { const Feedback = () => { const [submitState, setSubmitState] = useState(FeedbackState.PENDING); - const textArea = useRef(); - const submitFeedback = () => { + const feedbackTextArea = useRef(); + const nameInput = useRef(); + const emailInput = useRef(); + + const submitFeedback = async () => { setSubmitState(FeedbackState.PENDING); // Only attempt submit if the user has typed something in the text area - const hasUserInputText = textArea.current?.value.length > 0; + const hasUserInputText = feedbackTextArea.current?.value.length > 0; if (hasUserInputText) { - const input = textArea.current.value; + const input = ` +Feedback :love_letter: : ${feedbackTextArea.current.value} +Name: ${nameInput.current.value ?? 'Not provided'} +Email: ${emailInput.current.value ?? 'Not provided'} + `; const payload = { text: input, }; try { - fetch('/api/feedback', { + await fetch('/api/feedback', { method: 'POST', headers: { 'Content-Type': 'application/json', @@ -36,7 +44,11 @@ const Feedback = () => { }); // eslint-disable-next-line functional/immutable-data - textArea.current.value = ''; + feedbackTextArea.current.value = ''; + // eslint-disable-next-line functional/immutable-data + nameInput.current.value = ''; + // eslint-disable-next-line functional/immutable-data + emailInput.current.value = ''; setSubmitState(FeedbackState.SUCCESS); } catch (e) { @@ -47,23 +59,34 @@ const Feedback = () => { return ( - {submitState === FeedbackState.SUCCESS && Thank you!} + + + + + + + aria-description={`The Toilet Map is a free and open source project that we maintain in our spare time. + + We'd be so grateful if you could take a moment to give us feedback on how we could make your experience even better.`} + > + + {submitState === FeedbackState.SUCCESS && Thank you!}