Skip to content

Commit e095495

Browse files
fix: handle invalid email validation in quick send
1 parent 8eef178 commit e095495

File tree

1 file changed

+74
-58
lines changed

1 file changed

+74
-58
lines changed

apps/OpenSign/src/components/BulkSendUi.js

Lines changed: 74 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Loader from "../primitives/Loader";
55
import { useTranslation } from "react-i18next";
66
import Parse from "parse";
77
import ModalUi from "../primitives/ModalUi";
8-
import { isEnableSubscription } from "../constant/const";
8+
import { emailRegex, isEnableSubscription } from "../constant/const";
99
import { fetchSubscription } from "../constant/Utils";
1010
import { useNavigate } from "react-router-dom";
1111
const BulkSendUi = (props) => {
@@ -202,70 +202,86 @@ const BulkSendUi = (props) => {
202202
const updatedForms = forms.filter((_, i) => i !== index);
203203
setForms(updatedForms);
204204
};
205+
206+
function validateEmails(data) {
207+
for (const item of data) {
208+
for (const field of item.fields) {
209+
if (!emailRegex.test(field.email)) {
210+
alert(`Invalid email found: ${field.email}`);
211+
return false;
212+
}
213+
}
214+
}
215+
216+
return true;
217+
}
205218
const handleSubmit = async (e) => {
206219
e.preventDefault();
207220
e.stopPropagation();
208221
setIsSubmit(true);
209-
210-
// Create a copy of Placeholders array from props.item
211-
let Placeholders = [...props.Placeholders];
212-
// Initialize an empty array to store updated documents
213-
let Documents = [];
214-
// Loop through each form
215-
forms.forEach((form) => {
216-
//checking if user enter email which already exist as a signer then add user in a signers array
217-
let existSigner = [];
218-
form.fields.map((data) => {
219-
if (data.signer) {
220-
existSigner.push(data.signer);
221-
}
222-
});
223-
// Map through the copied Placeholders array to update email values
224-
const updatedPlaceholders = Placeholders.map((placeholder) => {
225-
// Find the field in the current form that matches the placeholder Id
226-
const field = form.fields.find(
227-
(element) => parseInt(element.fieldId) === placeholder.Id
228-
);
229-
// If a matching field is found, update the email value in the placeholder
230-
const signer = field?.signer?.objectId ? field.signer : "";
231-
if (field) {
232-
if (signer) {
233-
return {
234-
...placeholder,
235-
signerObjId: field?.signer?.objectId || "",
236-
signerPtr: signer
237-
};
238-
} else {
239-
return {
240-
...placeholder,
241-
email: field.email,
242-
signerObjId: field?.signer?.objectId || "",
243-
signerPtr: signer
244-
};
222+
if (validateEmails(forms)) {
223+
// Create a copy of Placeholders array from props.item
224+
let Placeholders = [...props.Placeholders];
225+
// Initialize an empty array to store updated documents
226+
let Documents = [];
227+
// Loop through each form
228+
forms.forEach((form) => {
229+
//checking if user enter email which already exist as a signer then add user in a signers array
230+
let existSigner = [];
231+
form.fields.map((data) => {
232+
if (data.signer) {
233+
existSigner.push(data.signer);
245234
}
246-
}
247-
// If no matching field is found, keep the placeholder as is
248-
return placeholder;
249-
});
250-
251-
// Push a new document object with updated Placeholders into the Documents array
252-
if (existSigner?.length > 0) {
253-
Documents.push({
254-
...props.item,
255-
Placeholders: updatedPlaceholders,
256-
Signers: props.item.Signers
257-
? [...props.item.Signers, ...existSigner]
258-
: [...existSigner]
259235
});
260-
} else {
261-
Documents.push({
262-
...props.item,
263-
Placeholders: updatedPlaceholders,
264-
SignatureType: props.signatureType
236+
// Map through the copied Placeholders array to update email values
237+
const updatedPlaceholders = Placeholders.map((placeholder) => {
238+
// Find the field in the current form that matches the placeholder Id
239+
const field = form.fields.find(
240+
(element) => parseInt(element.fieldId) === placeholder.Id
241+
);
242+
// If a matching field is found, update the email value in the placeholder
243+
const signer = field?.signer?.objectId ? field.signer : "";
244+
if (field) {
245+
if (signer) {
246+
return {
247+
...placeholder,
248+
signerObjId: field?.signer?.objectId || "",
249+
signerPtr: signer
250+
};
251+
} else {
252+
return {
253+
...placeholder,
254+
email: field.email,
255+
signerObjId: field?.signer?.objectId || "",
256+
signerPtr: signer
257+
};
258+
}
259+
}
260+
// If no matching field is found, keep the placeholder as is
261+
return placeholder;
265262
});
266-
}
267-
});
268-
await batchQuery(Documents);
263+
264+
// Push a new document object with updated Placeholders into the Documents array
265+
if (existSigner?.length > 0) {
266+
Documents.push({
267+
...props.item,
268+
Placeholders: updatedPlaceholders,
269+
Signers: props.item.Signers
270+
? [...props.item.Signers, ...existSigner]
271+
: [...existSigner]
272+
});
273+
} else {
274+
Documents.push({
275+
...props.item,
276+
Placeholders: updatedPlaceholders,
277+
SignatureType: props.signatureType
278+
});
279+
}
280+
});
281+
await batchQuery(Documents);
282+
} else {
283+
setIsSubmit(false);
284+
}
269285
};
270286

271287
const batchQuery = async (Documents) => {

0 commit comments

Comments
 (0)