From 03454a39786eee7d15e057f656359434720561ec Mon Sep 17 00:00:00 2001 From: Jo Humphrey <31373245+jamdelion@users.noreply.github.com> Date: Wed, 27 Nov 2024 10:45:02 +0000 Subject: [PATCH] Display errors nicely --- .../src/pages/VerifyEmail/VerifyEmail.tsx | 89 ++++++++++++------- .../useQueryApplicationFileDownload.ts | 3 +- .../src/ui/shared/ErrorWrapper.tsx | 2 +- 3 files changed, 60 insertions(+), 34 deletions(-) diff --git a/editor.planx.uk/src/pages/VerifyEmail/VerifyEmail.tsx b/editor.planx.uk/src/pages/VerifyEmail/VerifyEmail.tsx index 6f1a78fabb..48535bfb6d 100644 --- a/editor.planx.uk/src/pages/VerifyEmail/VerifyEmail.tsx +++ b/editor.planx.uk/src/pages/VerifyEmail/VerifyEmail.tsx @@ -3,15 +3,20 @@ import Container from "@mui/material/Container"; import Typography from "@mui/material/Typography"; import Card from "@planx/components/shared/Preview/Card"; import { CardHeader } from "@planx/components/shared/Preview/CardHeader/CardHeader"; +import { useQueryClient } from "@tanstack/react-query"; +import DelayedLoadingIndicator from "components/DelayedLoadingIndicator/DelayedLoadingIndicator"; import { useFormik } from "formik"; import React, { useEffect, useMemo, useState } from "react"; import InputLabel from "ui/public/InputLabel"; +import ErrorWrapper from "ui/shared/ErrorWrapper"; import Input from "ui/shared/Input/Input"; import InputRow from "ui/shared/InputRow"; import { object, string } from "yup"; import { downloadZipFile } from "./helpers/downloadZip"; -import useQueryApplicationFileDownload from "./queries/useQueryApplicationFileDownload"; +import useQueryApplicationFileDownload, { + DOWNLOAD_APPLICATION_FILE_QUERY_KEY, +} from "./queries/useQueryApplicationFileDownload"; const verifyEmailSchema = object({ email: string().email("Invalid email").required("Email address required"), @@ -24,14 +29,16 @@ export const VerifyEmail = ({ params }: VerifyEmailProps): JSX.Element => { const { sessionId, team } = params; const [email, setEmail] = useState(""); const emailInputIsValid = useMemo(() => email !== "", [email]); + const queryClient = useQueryClient(); const { data: downloadData, isLoading, - isError, + status: downloadStatus, error: downloadError, } = useQueryApplicationFileDownload(sessionId, email, team, { enabled: emailInputIsValid, + retry: false, }); useEffect(() => { @@ -40,54 +47,72 @@ export const VerifyEmail = ({ params }: VerifyEmailProps): JSX.Element => { } }, [downloadData]); + useEffect(() => { + // if the request has finished, invalidate query cache so other requests can be run + if (downloadStatus !== "pending") { + setEmail(""); + queryClient.invalidateQueries({ + queryKey: [`${DOWNLOAD_APPLICATION_FILE_QUERY_KEY}`, sessionId], + }); + } + }, [downloadStatus]); + const formik = useFormik({ initialValues: { email: "", }, - onSubmit: (values) => setEmail(values.email), + onSubmit: (values, { resetForm }) => { + setEmail(values.email); + resetForm(); + }, validateOnChange: false, validateOnBlur: false, validationSchema: verifyEmailSchema, }); - // ema return ( Download your application - {/* // TODO: make error and loading nicer */} - {isLoading && Loading!} - {isError && ( - {`${downloadError} ${downloadError.message}`} - )} - { + {isLoading ? ( + + ) : ( - - - - + <> + - - + + + + + + + - } + )} ); }; diff --git a/editor.planx.uk/src/pages/VerifyEmail/queries/useQueryApplicationFileDownload.ts b/editor.planx.uk/src/pages/VerifyEmail/queries/useQueryApplicationFileDownload.ts index f1b32415a8..33703bc8a4 100644 --- a/editor.planx.uk/src/pages/VerifyEmail/queries/useQueryApplicationFileDownload.ts +++ b/editor.planx.uk/src/pages/VerifyEmail/queries/useQueryApplicationFileDownload.ts @@ -8,7 +8,8 @@ export const DOWNLOAD_APPLICATION_FILE_URL = `${ export interface DownloadApplicationFileResponse { data: Blob; } -const DOWNLOAD_APPLICATION_FILE_QUERY_KEY = "DOWNLOAD_APPLICATION_FILE/GET"; +export const DOWNLOAD_APPLICATION_FILE_QUERY_KEY = + "DOWNLOAD_APPLICATION_FILE/GET"; const useQueryApplicationFileDownload = ( sessionId: string, diff --git a/editor.planx.uk/src/ui/shared/ErrorWrapper.tsx b/editor.planx.uk/src/ui/shared/ErrorWrapper.tsx index 6e4e7cb1c7..b168c96c8a 100644 --- a/editor.planx.uk/src/ui/shared/ErrorWrapper.tsx +++ b/editor.planx.uk/src/ui/shared/ErrorWrapper.tsx @@ -7,7 +7,7 @@ import React, { ReactElement, useEffect } from "react"; import { FONT_WEIGHT_SEMI_BOLD } from "theme"; export interface Props { - error?: string; + error?: string | null; children?: ReactElement; id?: string; // "alert" - important and time-sensitive information (e.g. invalid input feedback)