|
1 | 1 | import React, { useEffect } from "react"; |
2 | 2 | import { connect } from "react-redux"; |
3 | 3 | import T from "i18n-react/dist/i18n-react"; |
4 | | -import { FormikProvider, useFormik } from "formik"; |
| 4 | +import { FieldArray, FormikProvider, useFormik } from "formik"; |
5 | 5 | import * as yup from "yup"; |
6 | 6 | import PropTypes from "prop-types"; |
7 | 7 | import { |
@@ -61,16 +61,16 @@ const AddSponsorExtraQuestionPopup = ({ |
61 | 61 | }, |
62 | 62 | validationSchema: yup.object({ |
63 | 63 | id: yup.number(), |
64 | | - name: yup.string().required(), |
65 | | - label: yup.string().required(), |
66 | | - type: yup.string().required(), |
| 64 | + name: yup.string().required(T.translate("validation.required")), |
| 65 | + label: yup.string().required(T.translate("validation.required")), |
| 66 | + type: yup.string().required(T.translate("validation.required")), |
67 | 67 | mandatory: yup.boolean(), |
68 | 68 | placeholder: yup.string(), |
69 | 69 | max_selected_values: yup.number(), |
70 | 70 | values: yup.array().of( |
71 | 71 | yup.object().shape({ |
72 | | - value: yup.string().required(), |
73 | | - label: yup.string().required(), |
| 72 | + value: yup.string().required(T.translate("validation.required")), |
| 73 | + label: yup.string().required(T.translate("validation.required")), |
74 | 74 | is_default: yup.boolean(), |
75 | 75 | order: yup.number(), |
76 | 76 | _shouldSave: yup.boolean() |
@@ -111,29 +111,6 @@ const AddSponsorExtraQuestionPopup = ({ |
111 | 111 | } |
112 | 112 | }, [formik.values]); |
113 | 113 |
|
114 | | - const handleAddValue = () => { |
115 | | - const newValue = { |
116 | | - value: "", |
117 | | - label: "", |
118 | | - is_default: false, |
119 | | - _shouldSave: true |
120 | | - }; |
121 | | - |
122 | | - const updatedValues = [...formik.values.values, newValue]; |
123 | | - formik.setFieldValue("values", updatedValues); |
124 | | - }; |
125 | | - |
126 | | - const handleRemoveValue = async (index) => { |
127 | | - const valueToRemove = formik.values.values[index]; |
128 | | - |
129 | | - if (valueToRemove.id) { |
130 | | - deleteSponsorExtraQuestionValue(formik.values.id, valueToRemove.id); |
131 | | - } |
132 | | - |
133 | | - const updatedValues = formik.values.values.filter((_, i) => i !== index); |
134 | | - formik.setFieldValue("values", updatedValues); |
135 | | - }; |
136 | | - |
137 | 114 | const handleValueChange = (index, field, value) => { |
138 | 115 | if (field === "is_default" && value === true) { |
139 | 116 | const updatedValues = formik.values.values.map((v, i) => { |
@@ -205,7 +182,14 @@ const AddSponsorExtraQuestionPopup = ({ |
205 | 182 | } |
206 | 183 | }; |
207 | 184 |
|
208 | | - const renderValueItem = (valueItem, index, provided, snapshot) => ( |
| 185 | + const areExtraQuestionsIncomplete = () => { |
| 186 | + if (formik.errors.values) return true; |
| 187 | + return formik.values.values.some( |
| 188 | + (eq) => eq.name?.trim() === "" || eq.label?.trim() === "" |
| 189 | + ); |
| 190 | + }; |
| 191 | + |
| 192 | + const renderValueItem = (valueItem, index, provided, snapshot, remove) => ( |
209 | 193 | <Grid2 |
210 | 194 | container |
211 | 195 | spacing={2} |
@@ -266,10 +250,16 @@ const AddSponsorExtraQuestionPopup = ({ |
266 | 250 | }} |
267 | 251 | > |
268 | 252 | <IconButton |
269 | | - onClick={() => handleRemoveValue(index)} |
270 | | - edge="end" |
271 | | - sx={{ color: "#666" }} |
272 | | - disableRipple |
| 253 | + onClick={() => { |
| 254 | + const valueToRemove = formik.values.values[index]; |
| 255 | + if (valueToRemove.id) { |
| 256 | + deleteSponsorExtraQuestionValue( |
| 257 | + formik.values.id, |
| 258 | + valueToRemove.id |
| 259 | + ); |
| 260 | + } |
| 261 | + remove(index); |
| 262 | + }} |
273 | 263 | > |
274 | 264 | <CloseIcon /> |
275 | 265 | </IconButton> |
@@ -315,7 +305,12 @@ const AddSponsorExtraQuestionPopup = ({ |
315 | 305 | autoComplete="off" |
316 | 306 | > |
317 | 307 | <DialogContent sx={{ p: 1 }}> |
318 | | - <Grid2 container spacing={2} size={12} sx={{ p: 2 }}> |
| 308 | + <Grid2 |
| 309 | + container |
| 310 | + spacing={2} |
| 311 | + size={12} |
| 312 | + sx={{ p: 2, alignItems: "flex-start" }} |
| 313 | + > |
319 | 314 | <Grid2 |
320 | 315 | container |
321 | 316 | spacing={2} |
@@ -438,37 +433,58 @@ const AddSponsorExtraQuestionPopup = ({ |
438 | 433 | </Grid2> |
439 | 434 | <Grid2 size={2} /> |
440 | 435 | </Grid2> |
441 | | - <Grid2 container spacing={2} size={12}> |
442 | | - <DragAndDropList |
443 | | - items={formik.values.values.sort( |
444 | | - (a, b) => a.order - b.order |
445 | | - )} |
446 | | - onReorder={handleReorder} |
447 | | - renderItem={renderValueItem} |
448 | | - idKey="id" |
449 | | - updateOrderKey="order" |
450 | | - droppableId="sponsor-extra-question-values" |
451 | | - /> |
452 | | - </Grid2> |
453 | | - |
454 | | - <Button |
455 | | - variant="text" |
456 | | - startIcon={<AddIcon />} |
457 | | - onClick={handleAddValue} |
458 | | - sx={{ |
459 | | - color: "#666", |
460 | | - textTransform: "none", |
461 | | - fontSize: "14px", |
462 | | - fontWeight: 400, |
463 | | - "&:hover": { |
464 | | - backgroundColor: "transparent", |
465 | | - color: "#2196F3" |
466 | | - }, |
467 | | - mb: 2 |
468 | | - }} |
469 | | - > |
470 | | - {T.translate("edit_sponsor.add_value")} |
471 | | - </Button> |
| 436 | + <FieldArray name="values"> |
| 437 | + {({ push, remove }) => ( |
| 438 | + <> |
| 439 | + <Grid2 container spacing={2} size={12}> |
| 440 | + <DragAndDropList |
| 441 | + items={formik.values.values.sort( |
| 442 | + (a, b) => a.order - b.order |
| 443 | + )} |
| 444 | + onReorder={handleReorder} |
| 445 | + renderItem={(valueItem, index, provided, snapshot) => |
| 446 | + renderValueItem( |
| 447 | + valueItem, |
| 448 | + index, |
| 449 | + provided, |
| 450 | + snapshot, |
| 451 | + remove |
| 452 | + ) |
| 453 | + } |
| 454 | + idKey="id" |
| 455 | + updateOrderKey="order" |
| 456 | + droppableId="sponsor-extra-question-values" |
| 457 | + /> |
| 458 | + </Grid2> |
| 459 | + <Button |
| 460 | + variant="text" |
| 461 | + startIcon={<AddIcon />} |
| 462 | + onClick={() => |
| 463 | + push({ |
| 464 | + value: "", |
| 465 | + label: "", |
| 466 | + is_default: false, |
| 467 | + _shouldSave: true |
| 468 | + }) |
| 469 | + } |
| 470 | + disabled={areExtraQuestionsIncomplete()} |
| 471 | + sx={{ |
| 472 | + color: "#666", |
| 473 | + textTransform: "none", |
| 474 | + fontSize: "14px", |
| 475 | + fontWeight: 400, |
| 476 | + "&:hover": { |
| 477 | + backgroundColor: "transparent", |
| 478 | + color: "#2196F3" |
| 479 | + }, |
| 480 | + mb: 2 |
| 481 | + }} |
| 482 | + > |
| 483 | + {T.translate("edit_sponsor.add_value")} |
| 484 | + </Button> |
| 485 | + </> |
| 486 | + )} |
| 487 | + </FieldArray> |
472 | 488 | </Grid2> |
473 | 489 | )} |
474 | 490 | <Divider /> |
|
0 commit comments