Skip to content

Commit

Permalink
update state handling and add email field
Browse files Browse the repository at this point in the history
  • Loading branch information
jackiequach committed Sep 5, 2024
1 parent 133e98b commit 457a173
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 18 deletions.
23 changes: 15 additions & 8 deletions src/components/Feedback/Feedback.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import React, { useContext, useState } from "react";
import React, { useContext, useEffect, useState } from "react";
import { submitFeedback } from "~/src/lib/api/FeedbackApi";
import { FeedbackBoxViewType } from "@nypl/design-system-react-components";
import { FeedbackContext } from "~/src/context/FeedbackContext";

const DEFAULT_DESCRIPTION_TEXT = "Please share your question or feedback.";
const ERROR_DESCRIPTION_TEXT = "We are here to help!";

const Feedback: React.FC<any> = ({ location }) => {
const [view, setView] = useState<FeedbackBoxViewType>("form");
const [descriptionText, setDescriptionText] = useState(
DEFAULT_DESCRIPTION_TEXT
);
const {
FeedbackBox,
isOpen,
Expand All @@ -16,6 +22,11 @@ const Feedback: React.FC<any> = ({ location }) => {
setNotificationText,
} = useContext(FeedbackContext);

useEffect(() => {
if (isError) setDescriptionText(ERROR_DESCRIPTION_TEXT);
else setDescriptionText(DEFAULT_DESCRIPTION_TEXT);
}, [isError]);

const onCloseAndReset = () => {
if (isError) setIsError(false);
if (notificationText) setNotificationText(null);
Expand All @@ -28,8 +39,9 @@ const Feedback: React.FC<any> = ({ location }) => {
) => {
submitFeedback({
feedback: values.comment,
category: values.category,
category: isError ? "Bug" : values.category,
url: location,
email: values.email,
})
.then((res) => {
if (res.ok) setView("confirmation");
Expand All @@ -41,9 +53,6 @@ const Feedback: React.FC<any> = ({ location }) => {
setView("confirmation");
};

const DEFAULT_DESCRIPTION_TEXT = "Please share your question or feedback.";
const ERROR_DESCRIPTION_TEXT = "We are here to help!";

return (
<FeedbackBox
showCategoryField={!isError}
Expand All @@ -53,9 +62,7 @@ const Feedback: React.FC<any> = ({ location }) => {
onOpen={onOpen}
onSubmit={handleFeedbackSubmit}
confirmationText="Thank you, your feedback has been submitted."
descriptionText={
isError ? ERROR_DESCRIPTION_TEXT : DEFAULT_DESCRIPTION_TEXT
}
descriptionText={descriptionText}
notificationText={notificationText}
id="feedbackBox-id"
title="Help and Feedback"
Expand Down
14 changes: 8 additions & 6 deletions src/context/FeedbackContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,21 @@ type FeedbackContextType = {
FeedbackBox: ChakraComponent<any>;
onClose: () => void;
isOpen?: boolean;
isError?: boolean;
setIsError: (value: boolean) => void;
notificationText: string;
setNotificationText: (value: string) => void;
isError: boolean | null;
setIsError: React.Dispatch<React.SetStateAction<boolean | null>>;
notificationText: string | null;
setNotificationText: React.Dispatch<React.SetStateAction<string | null>>;
};

export const FeedbackContext = createContext<FeedbackContextType | null>(null);
export const FeedbackContext = createContext<FeedbackContextType | undefined>(
undefined
);

export const FeedbackProvider: React.FC<{
children?: React.ReactNode;
}> = ({ children }) => {
const { FeedbackBox, isOpen, onOpen, onClose } = useFeedbackBox();
const [isError, setIsError] = useState(false);
const [isError, setIsError] = useState(null);
const [notificationText, setNotificationText] = useState(null);

return (
Expand Down
10 changes: 6 additions & 4 deletions src/pages/_error.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useContext } from "react";
import React, { useContext, useEffect } from "react";
import { NextPageContext } from "next";
import Layout from "../components/Layout/Layout";
import Link from "../components/Link/Link";
Expand Down Expand Up @@ -44,8 +44,10 @@ const Error = ({ statusCode }) => {
const { onOpen, setNotificationText, setIsError } =
useContext(FeedbackContext);

setIsError(true);
setNotificationText(getNotificationText(statusCode));
useEffect(() => {
setIsError(true);
setNotificationText(getNotificationText(statusCode));
}, [setIsError, setNotificationText, statusCode]);

return (
<Layout>
Expand All @@ -67,7 +69,7 @@ const Error = ({ statusCode }) => {
{errorMap[statusCode].heading}
</Heading>
<Box>
<Text noSpace display={{ base: "inline-block", md: "block" }}>
<Text noSpace display={{ base: "inline", md: "block" }}>
{errorMap[statusCode].subText}
</Text>
<Text noSpace display="inline">
Expand Down
1 change: 1 addition & 0 deletions src/types/Feedback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export type Feedback = {
feedback: string;
category: string;
url: string;
email?: string;
};

0 comments on commit 457a173

Please sign in to comment.