Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 82 additions & 66 deletions src/components/forms/sponsor-general-form/add-extra-question-popup.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useEffect } from "react";
import { connect } from "react-redux";
import T from "i18n-react/dist/i18n-react";
import { FormikProvider, useFormik } from "formik";
import { FieldArray, FormikProvider, useFormik } from "formik";
import * as yup from "yup";
import PropTypes from "prop-types";
import {
Expand Down Expand Up @@ -61,16 +61,16 @@ const AddSponsorExtraQuestionPopup = ({
},
validationSchema: yup.object({
id: yup.number(),
name: yup.string().required(),
label: yup.string().required(),
type: yup.string().required(),
name: yup.string().required(T.translate("validation.required")),
label: yup.string().required(T.translate("validation.required")),
type: yup.string().required(T.translate("validation.required")),
mandatory: yup.boolean(),
placeholder: yup.string(),
max_selected_values: yup.number(),
values: yup.array().of(
yup.object().shape({
value: yup.string().required(),
label: yup.string().required(),
value: yup.string().required(T.translate("validation.required")),
label: yup.string().required(T.translate("validation.required")),
is_default: yup.boolean(),
order: yup.number(),
_shouldSave: yup.boolean()
Expand Down Expand Up @@ -111,29 +111,6 @@ const AddSponsorExtraQuestionPopup = ({
}
}, [formik.values]);

const handleAddValue = () => {
const newValue = {
value: "",
label: "",
is_default: false,
_shouldSave: true
};

const updatedValues = [...formik.values.values, newValue];
formik.setFieldValue("values", updatedValues);
};

const handleRemoveValue = async (index) => {
const valueToRemove = formik.values.values[index];

if (valueToRemove.id) {
deleteSponsorExtraQuestionValue(formik.values.id, valueToRemove.id);
}

const updatedValues = formik.values.values.filter((_, i) => i !== index);
formik.setFieldValue("values", updatedValues);
};

const handleValueChange = (index, field, value) => {
if (field === "is_default" && value === true) {
const updatedValues = formik.values.values.map((v, i) => {
Expand Down Expand Up @@ -205,7 +182,14 @@ const AddSponsorExtraQuestionPopup = ({
}
};

const renderValueItem = (valueItem, index, provided, snapshot) => (
const areExtraQuestionsIncomplete = () => {
if (formik.errors.values) return true;
return formik.values.values.some(
(eq) => eq.name?.trim() === "" || eq.label?.trim() === ""
Copy link

Copilot AI Dec 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The areExtraQuestionsIncomplete function is checking for eq.name?.trim() === "" and eq.label?.trim() === "", but the values array contains objects with value and label fields, not name and label. This should be checking eq.value?.trim() === "" instead of eq.name?.trim() === "".

Suggested change
(eq) => eq.name?.trim() === "" || eq.label?.trim() === ""
(eq) => eq.value?.trim() === "" || eq.label?.trim() === ""

Copilot uses AI. Check for mistakes.
);
};

const renderValueItem = (valueItem, index, provided, snapshot, remove) => (
<Grid2
container
spacing={2}
Expand Down Expand Up @@ -266,10 +250,16 @@ const AddSponsorExtraQuestionPopup = ({
}}
>
<IconButton
onClick={() => handleRemoveValue(index)}
edge="end"
sx={{ color: "#666" }}
disableRipple
onClick={() => {
const valueToRemove = formik.values.values[index];
if (valueToRemove.id) {
deleteSponsorExtraQuestionValue(
formik.values.id,
valueToRemove.id
);
}
remove(index);
}}
Comment on lines +253 to +262
Copy link

Copilot AI Dec 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The removed IconButton properties edge="end", sx={{ color: "#666" }}, and disableRipple should be preserved in the new implementation to maintain consistent styling and behavior with the rest of the UI.

Copilot uses AI. Check for mistakes.
>
<CloseIcon />
</IconButton>
Expand Down Expand Up @@ -315,7 +305,12 @@ const AddSponsorExtraQuestionPopup = ({
autoComplete="off"
>
<DialogContent sx={{ p: 1 }}>
<Grid2 container spacing={2} size={12} sx={{ p: 2 }}>
<Grid2
container
spacing={2}
size={12}
sx={{ p: 2, alignItems: "flex-start" }}
>
<Grid2
container
spacing={2}
Expand Down Expand Up @@ -438,37 +433,58 @@ const AddSponsorExtraQuestionPopup = ({
</Grid2>
<Grid2 size={2} />
</Grid2>
<Grid2 container spacing={2} size={12}>
<DragAndDropList
items={formik.values.values.sort(
(a, b) => a.order - b.order
)}
onReorder={handleReorder}
renderItem={renderValueItem}
idKey="id"
updateOrderKey="order"
droppableId="sponsor-extra-question-values"
/>
</Grid2>

<Button
variant="text"
startIcon={<AddIcon />}
onClick={handleAddValue}
sx={{
color: "#666",
textTransform: "none",
fontSize: "14px",
fontWeight: 400,
"&:hover": {
backgroundColor: "transparent",
color: "#2196F3"
},
mb: 2
}}
>
{T.translate("edit_sponsor.add_value")}
</Button>
<FieldArray name="values">
{({ push, remove }) => (
<>
<Grid2 container spacing={2} size={12}>
<DragAndDropList
items={formik.values.values.sort(
(a, b) => a.order - b.order
)}
onReorder={handleReorder}
renderItem={(valueItem, index, provided, snapshot) =>
renderValueItem(
valueItem,
index,
provided,
snapshot,
remove
)
}
idKey="id"
updateOrderKey="order"
droppableId="sponsor-extra-question-values"
/>
</Grid2>
<Button
variant="text"
startIcon={<AddIcon />}
onClick={() =>
push({
value: "",
label: "",
is_default: false,
_shouldSave: true
})
}
disabled={areExtraQuestionsIncomplete()}
sx={{
color: "#666",
textTransform: "none",
fontSize: "14px",
fontWeight: 400,
"&:hover": {
backgroundColor: "transparent",
color: "#2196F3"
},
mb: 2
}}
>
{T.translate("edit_sponsor.add_value")}
</Button>
</>
)}
</FieldArray>
</Grid2>
)}
<Divider />
Expand Down