From fd3d65b2db5aa1d9b553208761d3f484b6354c0d Mon Sep 17 00:00:00 2001 From: Iacopo Leardini Date: Mon, 6 May 2024 15:33:38 +0200 Subject: [PATCH] solve NaN target size and refactor form overlay --- .../components/campaignForm/FormOverlay.tsx | 33 +++++++++++++ .../components/campaignForm/FormProvider.tsx | 15 +++--- .../campaignForm/fields/InputField.tsx | 5 +- .../components/campaignForm/index.tsx | 46 ++++--------------- 4 files changed, 51 insertions(+), 48 deletions(-) create mode 100644 src/pages/campaigns/components/campaignForm/FormOverlay.tsx diff --git a/src/pages/campaigns/components/campaignForm/FormOverlay.tsx b/src/pages/campaigns/components/campaignForm/FormOverlay.tsx new file mode 100644 index 00000000..a5de6726 --- /dev/null +++ b/src/pages/campaigns/components/campaignForm/FormOverlay.tsx @@ -0,0 +1,33 @@ +import { styled } from "styled-components"; +import { useFormikContext } from "formik"; + +const Overlay = styled.div` + position: fixed; + top: 0; + left: 0; + width: 100%; + height: 100%; + z-index: 1000; + background-color: rgba(255, 255, 255, 0.8); + display: flex; + justify-content: center; + align-items: center; + text-align: center; +`; + +const FormOverlay = () => { + const { isSubmitting, isValid } = useFormikContext(); + + if (!isSubmitting || !isValid) return null; + + return ( + + Saving campaign in progress +
+ It may take a few seconds... +
+
+ ); +}; + +export default FormOverlay; diff --git a/src/pages/campaigns/components/campaignForm/FormProvider.tsx b/src/pages/campaigns/components/campaignForm/FormProvider.tsx index 6513921c..b73eea86 100644 --- a/src/pages/campaigns/components/campaignForm/FormProvider.tsx +++ b/src/pages/campaigns/components/campaignForm/FormProvider.tsx @@ -48,7 +48,7 @@ export interface NewCampaignValues { outOfScope?: string; deviceRequirements?: string; targetNotes?: string; - targetSize?: number; + targetSize?: string; browsersList?: string[]; productType?: string; } @@ -114,7 +114,7 @@ const FormProvider = ({ outOfScope: dossier?.outOfScope || "", deviceRequirements: dossier?.deviceRequirements || "", targetNotes: dossier?.target?.notes || "", - targetSize: dossier?.target?.size || 0, + targetSize: dossier?.target?.size?.toString(), browsersList: dossier?.browsers?.map((browser) => browser.id.toString()) || [], productType: dossier?.productType?.id.toString() || "", @@ -155,7 +155,8 @@ const FormProvider = ({ initialValues={initialValues} enableReinitialize validationSchema={validationSchema} - onSubmit={async (values) => { + onSubmit={async (values, action) => { + action.setSubmitting(true); let roles = []; if (values.pm) { roles.push({ role: 1, user: parseInt(values.pm) }); @@ -195,7 +196,9 @@ const FormProvider = ({ deviceRequirements: values.deviceRequirements, target: { notes: values.targetNotes, - size: values.targetSize, + size: !!values.targetSize + ? parseInt(values.targetSize) + : undefined, }, browsers: values.browsersList?.map((browser) => parseInt(browser, 10) @@ -211,8 +214,6 @@ const FormProvider = ({ dossierCreationData: body, }).unwrap(); } else { - setIsCreating(true); - const resp = await postDossiers({ body: { ...body, @@ -237,7 +238,7 @@ const FormProvider = ({ ) ); } - setIsCreating(false); + action.setSubmitting(false); }} > {children} diff --git a/src/pages/campaigns/components/campaignForm/fields/InputField.tsx b/src/pages/campaigns/components/campaignForm/fields/InputField.tsx index eb053292..85e23ec9 100644 --- a/src/pages/campaigns/components/campaignForm/fields/InputField.tsx +++ b/src/pages/campaigns/components/campaignForm/fields/InputField.tsx @@ -17,10 +17,7 @@ const InputField = ({ name, label, type = "text", ...props }: InputProps) => { {({ field, form, meta }: FieldProps) => { const handleChange = (val: string) => { - form.setFieldValue( - field.name, - type === "number" ? parseInt(val || "0") : val - ); + form.setFieldValue(field.name, val); }; const handleBlur = () => { form.setFieldTouched(field.name, true); diff --git a/src/pages/campaigns/components/campaignForm/index.tsx b/src/pages/campaigns/components/campaignForm/index.tsx index b633a870..ec86891f 100644 --- a/src/pages/campaigns/components/campaignForm/index.tsx +++ b/src/pages/campaigns/components/campaignForm/index.tsx @@ -37,6 +37,7 @@ import CsmSelect from "./fields/roles/CsmSelect"; import PmSelect from "./fields/roles/PMSelect"; import ResearcherSelect from "./fields/roles/ResearcherSelect"; import TlSelect from "./fields/roles/TLSelect"; +import FormOverlay from "./FormOverlay"; interface FormProps { dossier?: GetDossiersByCampaignApiResponse; @@ -68,33 +69,11 @@ const CampaignForm = (props: FormProps) => ( ); -const CreatingOverlay = styled.div` - position: fixed; - top: 0; - left: 0; - width: 100%; - height: 100%; - z-index: 1000; - background-color: rgba(255, 255, 255, 0.8); - display: flex; - justify-content: center; - align-items: center; - text-align: center; -`; - const CampaignFormContent = ({ dossier, isEdit, duplicate }: FormProps) => { - const { isCreating } = useCampaignFormContext(); return ( - {isCreating && ( - - Creating campaign in progress -
- It may take a few seconds... -
-
- )} +
@@ -113,7 +92,7 @@ const CampaignFormContent = ({ dossier, isEdit, duplicate }: FormProps) => { @@ -147,23 +126,16 @@ const CampaignFormContent = ({ dossier, isEdit, duplicate }: FormProps) => { - - - - +