Skip to content
9 changes: 5 additions & 4 deletions src/components/mui/__tests__/additional-input-list.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,13 @@ describe("AdditionalInputList", () => {
expect(screen.getByTestId("item-name-2")).toHaveTextContent("Field 3");
});

test("renders nothing when meta_fields is empty", () => {
test("renders a default metafield when meta_fields is empty", () => {
renderWithFormik(defaultProps, { meta_fields: [] });

expect(
screen.queryByTestId("additional-input-0")
).not.toBeInTheDocument();
// Should render one default empty field
expect(screen.getByTestId("additional-input-0")).toBeInTheDocument();
expect(screen.getByTestId("item-name-0")).toHaveTextContent("");
expect(screen.getByTestId("item-type-0")).toHaveTextContent("");
});
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from "react";
import React, { useEffect } from "react";
import { useFormikContext, getIn } from "formik";
import T from "i18n-react";
import AdditionalInput from "./additional-input";
Expand All @@ -19,8 +19,19 @@ const AdditionalInputList = ({ name, onDelete, onDeleteValue, entityId }) => {

const metaFields = values[name] || [];

useEffect(() => {
if (metaFields.length === 0) {
setFieldValue(name, [
{ ...DEFAULT_META_FIELD, _key: `draft_${Date.now()}` }
]);
}
}, [metaFields.length]);

const handleAddItem = () => {
setFieldValue(name, [...metaFields, { ...DEFAULT_META_FIELD }]);
setFieldValue(name, [
...metaFields,
{ ...DEFAULT_META_FIELD, _key: `draft_${Date.now()}` }
]);
};

const handleRemove = async (item, index) => {
Expand All @@ -39,7 +50,7 @@ const AdditionalInputList = ({ name, onDelete, onDeleteValue, entityId }) => {
const removeFromUI = () => {
const newValues = metaFields.filter((_, idx) => idx !== index);
if (newValues.length === 0) {
newValues.push({ ...DEFAULT_META_FIELD });
newValues.push({ ...DEFAULT_META_FIELD, _key: `draft_${Date.now()}` });
}
setFieldValue(name, newValues);
};
Expand Down Expand Up @@ -80,7 +91,7 @@ const AdditionalInputList = ({ name, onDelete, onDeleteValue, entityId }) => {
<>
{metaFields.map((item, itemIdx) => (
<AdditionalInput
key={item.id || `additional_input_${itemIdx}`}
key={item.id || item._key}
item={item}
itemIdx={itemIdx}
baseName={name}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const AdditionalInput = ({
my: 2
}}
>
<Grid2 container spacing={2} sx={{ alignItems: "end" }}>
<Grid2 container spacing={2} sx={{ alignItems: "start" }}>
<Grid2 size={4}>
<InputLabel htmlFor={buildFieldName("name")}>
{T.translate("additional_inputs.meta_field_title")}
Expand Down
11 changes: 1 addition & 10 deletions src/pages/sponsors-global/form-templates/form-template-popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,7 @@ const FormTemplateDialog = ({
...initialEntity,
meta_fields: initialEntity?.meta_fields?.length
? initialEntity.meta_fields
: [
{
name: "",
type: "Text",
is_required: false,
minimum_quantity: 0,
maximum_quantity: 0,
values: []
}
]
: []
},
validationSchema: yup.object().shape({
code: requiredStringValidation(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,7 @@ const SponsorItemDialog = ({
...initialEntity,
meta_fields: initialEntity?.meta_fields?.length
? initialEntity.meta_fields
: [
{
name: "",
type: "Text",
is_required: false,
minimum_quantity: 0,
maximum_quantity: 0,
values: []
}
],
: [],
images: initialEntity?.images ?? []
},
validationSchema: yup.object().shape({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,7 @@ function createDefaultState() {
quantity_limit_per_sponsor: "",
default_quantity: "",
images: [],
meta_fields: [
{
name: "",
type: "Text",
is_required: false,
values: []
}
]
meta_fields: []
}
};
}
Expand Down Expand Up @@ -193,14 +186,7 @@ describe("SponsorFormItemsListReducer", () => {
early_bird_rate: "1.00",
standard_rate: "1.00",
onsite_rate: "1.00",
meta_fields: [
{
name: "",
type: "Text",
is_required: false,
values: []
}
]
meta_fields: []
}
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,7 @@ const DEFAULT_ITEM_ENTITY = {
quantity_limit_per_sponsor: 0,
default_quantity: 0,
images: [],
meta_fields: [
{
name: "",
type: "Text",
is_required: false,
values: []
}
]
meta_fields: []
};

const DEFAULT_STATE = {
Expand Down Expand Up @@ -123,17 +116,7 @@ const sponsorCustomizedFormItemsListReducer = (
early_bird_rate: amountFromCents(item.early_bird_rate),
standard_rate: amountFromCents(item.standard_rate),
onsite_rate: amountFromCents(item.onsite_rate),
meta_fields:
item.meta_fields.length > 0
? item.meta_fields
: [
{
name: "",
type: "Text",
is_required: false,
values: []
}
]
meta_fields: item.meta_fields.length > 0 ? item.meta_fields : []
};
return { ...state, currentItem };
}
Expand Down
11 changes: 1 addition & 10 deletions src/reducers/sponsors/sponsor-customized-form-reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,7 @@ const DEFAULT_ENTITY = {
opens_at: "",
expires_at: "",
instructions: "",
meta_fields: [
{
name: "",
type: "Text",
is_required: false,
minimum_quantity: 0,
maximum_quantity: 0,
values: []
}
]
meta_fields: []
};

const DEFAULT_STATE = {
Expand Down
21 changes: 2 additions & 19 deletions src/reducers/sponsors/sponsor-form-items-list-reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,7 @@ const DEFAULT_STATE = {
quantity_limit_per_sponsor: "",
default_quantity: "",
images: [],
meta_fields: [
{
name: "",
type: "Text",
is_required: false,
values: []
}
]
meta_fields: []
}
};

Expand Down Expand Up @@ -110,17 +103,7 @@ const sponsorFormItemsListReducer = (state = DEFAULT_STATE, action) => {
early_bird_rate: amountFromCents(item.early_bird_rate),
standard_rate: amountFromCents(item.standard_rate),
onsite_rate: amountFromCents(item.onsite_rate),
meta_fields:
item.meta_fields.length > 0
? item.meta_fields
: [
{
name: "",
type: "Text",
is_required: false,
values: []
}
]
meta_fields: item.meta_fields.length > 0 ? item.meta_fields : []
};

return { ...state, currentItem };
Expand Down
21 changes: 2 additions & 19 deletions src/reducers/sponsors/sponsor-forms-list-reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,7 @@ const DEFAULT_STATE = {
opens_at: null,
expires_at: null,
instructions: "",
meta_fields: [
{
name: "",
type: "Text",
is_required: false,
values: []
}
]
meta_fields: []
}
};

Expand Down Expand Up @@ -131,17 +124,7 @@ const sponsorFormsListReducer = (state = DEFAULT_STATE, action) => {
opens_at: form.opens_at,
expires_at: form.expires_at,
instructions: form.instructions,
meta_fields:
form.meta_fields.length > 0
? form.meta_fields
: [
{
name: "",
type: "Text",
is_required: false,
values: []
}
]
meta_fields: form.meta_fields.length > 0 ? form.meta_fields : []
};

return { ...state, formTemplate };
Expand Down
26 changes: 10 additions & 16 deletions src/utils/yup.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,22 +111,16 @@ export const formMetafieldsValidation = () =>
}),
type: yup.string().oneOf(METAFIELD_TYPES),
is_required: yup.boolean(),
minimum_quantity: yup
.number()
.nullable()
.when("type", {
is: (type) => type === "Quantity",
then: (schema) => schema.required(T.translate("validation.required")),
otherwise: (schema) => schema
}),
maximum_quantity: yup
.number()
.nullable()
.when("type", {
is: (type) => type === "Quantity",
then: (schema) => schema.required(T.translate("validation.required")),
otherwise: (schema) => schema
}),
minimum_quantity: positiveNumberValidation().when("type", {
is: (type) => type === "Quantity",
then: (schema) => schema.required(T.translate("validation.required")),
otherwise: (schema) => schema
}),
maximum_quantity: positiveNumberValidation().when("type", {
is: (type) => type === "Quantity",
then: (schema) => schema.required(T.translate("validation.required")),
otherwise: (schema) => schema
}),
values: yup.array().when("type", {
is: (type) => fieldTypesWithOptions.includes(type),
then: (schema) =>
Expand Down