Skip to content

Commit

Permalink
solve NaN target size and refactor form overlay
Browse files Browse the repository at this point in the history
  • Loading branch information
iacopolea committed May 6, 2024
1 parent ad733e2 commit fd3d65b
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 48 deletions.
33 changes: 33 additions & 0 deletions src/pages/campaigns/components/campaignForm/FormOverlay.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<Overlay>
Saving campaign in progress
<br />
It may take a few seconds...
<br />
</Overlay>
);
};

export default FormOverlay;
15 changes: 8 additions & 7 deletions src/pages/campaigns/components/campaignForm/FormProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export interface NewCampaignValues {
outOfScope?: string;
deviceRequirements?: string;
targetNotes?: string;
targetSize?: number;
targetSize?: string;
browsersList?: string[];
productType?: string;
}
Expand Down Expand Up @@ -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() || "",
Expand Down Expand Up @@ -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) });
Expand Down Expand Up @@ -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)
Expand All @@ -211,8 +214,6 @@ const FormProvider = ({
dossierCreationData: body,
}).unwrap();
} else {
setIsCreating(true);

const resp = await postDossiers({
body: {
...body,
Expand All @@ -237,7 +238,7 @@ const FormProvider = ({
)
);
}
setIsCreating(false);
action.setSubmitting(false);
}}
>
{children}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ const InputField = ({ name, label, type = "text", ...props }: InputProps) => {
<FormikField name={name}>
{({ 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);
Expand Down
46 changes: 9 additions & 37 deletions src/pages/campaigns/components/campaignForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -68,33 +69,11 @@ const CampaignForm = (props: FormProps) => (
</CampaignFormContext>
);

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 (
<FormProvider dossier={dossier} isEdit={isEdit} duplicate={duplicate}>
<FullGrid>
{isCreating && (
<CreatingOverlay>
Creating campaign in progress
<br />
It may take a few seconds...
<br />
</CreatingOverlay>
)}
<FormOverlay />
<BSCol size="col-lg-9">
<Form id="campaign-form">
<Section title="Campaign General Informations" id="general">
Expand All @@ -113,7 +92,7 @@ const CampaignFormContent = ({ dossier, isEdit, duplicate }: FormProps) => {
<TestTypeSelect />
<TextareaField
name="description"
label="Description"
label="Campaign Goal and description"
placeholder="The product to be tested is [...] and we need to verify that [...]"
/>
</FieldWrapper>
Expand Down Expand Up @@ -147,23 +126,16 @@ const CampaignFormContent = ({ dossier, isEdit, duplicate }: FormProps) => {
<FieldWrapper>
<ProductType />
<InputField
type="url"
type="text"
name="productLink"
label="Product Link"
/>
</FieldWrapper>
<FieldWrapper>
<TextareaField
name="goal"
label="Test goal"
placeholder="The test goal is to verify that..."
/>
<TextareaField
name="outOfScope"
label="Out of scope"
placeholder="The test will not cover..."
/>
</FieldWrapper>
<TextareaField
name="outOfScope"
label="Out of scope"
placeholder="The test will not cover..."
/>
</Section>
<Section title="Where do we need to test?" id="where">
<DeviceMultiselect />
Expand Down

0 comments on commit fd3d65b

Please sign in to comment.