|
| 1 | +import React, { useState, useEffect, useRef } from "react"; |
| 2 | +import axios from "axios"; |
| 3 | +import SuggestionInput from "./shared/fields/SuggestionInput"; |
| 4 | + |
| 5 | +const BulkSendUi = (props) => { |
| 6 | + const [forms, setForms] = useState([]); |
| 7 | + const [formId, setFormId] = useState(2); |
| 8 | + const formRef = useRef(null); |
| 9 | + const [scrollOnNextUpdate, setScrollOnNextUpdate] = useState(false); |
| 10 | + const [isSubmit, setIsSubmit] = useState(false); |
| 11 | + const [allowedForm, setAllowedForm] = useState(0); |
| 12 | + const allowedSigners = 50; |
| 13 | + useEffect(() => { |
| 14 | + if (scrollOnNextUpdate && formRef.current) { |
| 15 | + formRef.current.scrollIntoView({ |
| 16 | + behavior: "smooth", |
| 17 | + block: "end", |
| 18 | + inline: "nearest" |
| 19 | + }); |
| 20 | + setScrollOnNextUpdate(false); |
| 21 | + } |
| 22 | + }, [forms, scrollOnNextUpdate]); |
| 23 | + |
| 24 | + useEffect(() => { |
| 25 | + (() => { |
| 26 | + if (props?.Placeholders?.length > 0) { |
| 27 | + let users = []; |
| 28 | + props?.Placeholders?.forEach((element) => { |
| 29 | + if (!element.signerObjId) { |
| 30 | + users = [ |
| 31 | + ...users, |
| 32 | + { |
| 33 | + fieldId: element.Id, |
| 34 | + email: "", |
| 35 | + label: element.Role, |
| 36 | + signer: {} |
| 37 | + } |
| 38 | + ]; |
| 39 | + } |
| 40 | + }); |
| 41 | + setForms((prevForms) => [...prevForms, { Id: 1, fields: users }]); |
| 42 | + const totalForms = Math.floor(allowedSigners / users?.length); |
| 43 | + setAllowedForm(totalForms); |
| 44 | + } |
| 45 | + })(); |
| 46 | + // eslint-disable-next-line |
| 47 | + }, []); |
| 48 | + const handleInputChange = (index, signer, fieldIndex) => { |
| 49 | + const newForms = [...forms]; |
| 50 | + newForms[index].fields[fieldIndex].email = signer?.Email |
| 51 | + ? signer?.Email |
| 52 | + : signer || ""; |
| 53 | + newForms[index].fields[fieldIndex].signer = signer?.objectId ? signer : ""; |
| 54 | + setForms(newForms); |
| 55 | + }; |
| 56 | + |
| 57 | + const handleAddForm = (e) => { |
| 58 | + e.preventDefault(); |
| 59 | + // Check if the quick send limit has been reached |
| 60 | + if (forms?.length < allowedForm) { |
| 61 | + if (props?.Placeholders.length > 0) { |
| 62 | + let newForm = []; |
| 63 | + props?.Placeholders?.forEach((element) => { |
| 64 | + if (!element.signerObjId) { |
| 65 | + newForm = [ |
| 66 | + ...newForm, |
| 67 | + { |
| 68 | + fieldId: element.Id, |
| 69 | + email: "", |
| 70 | + label: element.Role, |
| 71 | + signer: {} |
| 72 | + } |
| 73 | + ]; |
| 74 | + } |
| 75 | + }); |
| 76 | + setForms([...forms, { Id: formId, fields: newForm }]); |
| 77 | + } |
| 78 | + setFormId(formId + 1); |
| 79 | + setScrollOnNextUpdate(true); |
| 80 | + } else { |
| 81 | + // If the limit has been reached, throw an error with the appropriate message |
| 82 | + alert("Quick send reached limit."); |
| 83 | + } |
| 84 | + }; |
| 85 | + |
| 86 | + const handleRemoveForm = (index) => { |
| 87 | + const updatedForms = forms.filter((_, i) => i !== index); |
| 88 | + setForms(updatedForms); |
| 89 | + }; |
| 90 | + const handleSubmit = async (e) => { |
| 91 | + e.preventDefault(); |
| 92 | + e.stopPropagation(); |
| 93 | + setIsSubmit(true); |
| 94 | + |
| 95 | + // Create a copy of Placeholders array from props.item |
| 96 | + let Placeholders = [...props.item.Placeholders]; |
| 97 | + // Initialize an empty array to store updated documents |
| 98 | + let Documents = []; |
| 99 | + |
| 100 | + // Loop through each form |
| 101 | + forms.forEach((form) => { |
| 102 | + // Map through the copied Placeholders array to update email values |
| 103 | + const updatedPlaceholders = Placeholders.map((placeholder) => { |
| 104 | + // Find the field in the current form that matches the placeholder Id |
| 105 | + const field = form.fields.find( |
| 106 | + (element) => parseInt(element.fieldId) === placeholder.Id |
| 107 | + ); |
| 108 | + // If a matching field is found, update the email value in the placeholder |
| 109 | + const signer = field?.signer?.objectId ? field.signer : {}; |
| 110 | + if (field) { |
| 111 | + return { |
| 112 | + ...placeholder, |
| 113 | + email: field.email, |
| 114 | + signerObjId: field?.signer?.objectId || "", |
| 115 | + signerPtr: signer |
| 116 | + }; |
| 117 | + } |
| 118 | + // If no matching field is found, keep the placeholder as is |
| 119 | + return placeholder; |
| 120 | + }); |
| 121 | + |
| 122 | + // Push a new document object with updated Placeholders into the Documents array |
| 123 | + Documents.push({ ...props.item, Placeholders: updatedPlaceholders }); |
| 124 | + }); |
| 125 | + // console.log("Documents ", Documents); |
| 126 | + await batchQuery(Documents); |
| 127 | + }; |
| 128 | + |
| 129 | + const batchQuery = async (Documents) => { |
| 130 | + const serverUrl = localStorage.getItem("baseUrl"); |
| 131 | + const functionsUrl = `${serverUrl}functions/batchdocuments`; |
| 132 | + const headers = { |
| 133 | + "Content-Type": "application/json", |
| 134 | + "X-Parse-Application-Id": localStorage.getItem("parseAppId"), |
| 135 | + sessionToken: localStorage.getItem("accesstoken") |
| 136 | + }; |
| 137 | + const params = { |
| 138 | + Documents: JSON.stringify(Documents) |
| 139 | + }; |
| 140 | + try { |
| 141 | + const res = await axios.post(functionsUrl, params, { headers: headers }); |
| 142 | + // console.log("res ", res); |
| 143 | + if (res.data && res.data.result) { |
| 144 | + props.handleClose("success", Documents?.length); |
| 145 | + } |
| 146 | + } catch (err) { |
| 147 | + console.log("Err ", err); |
| 148 | + props.handleClose("error", 0); |
| 149 | + } finally { |
| 150 | + setIsSubmit(false); |
| 151 | + } |
| 152 | + }; |
| 153 | + |
| 154 | + return ( |
| 155 | + <> |
| 156 | + {isSubmit && ( |
| 157 | + <div className="absolute z-[999] h-full w-full flex justify-center items-center bg-black bg-opacity-40"> |
| 158 | + <div |
| 159 | + style={{ |
| 160 | + fontSize: "45px", |
| 161 | + color: "#3dd3e0" |
| 162 | + }} |
| 163 | + className="loader-37 " |
| 164 | + ></div> |
| 165 | + </div> |
| 166 | + )} |
| 167 | + <form onSubmit={handleSubmit}> |
| 168 | + <div className=" min-h-max max-h-[250px] overflow-y-auto"> |
| 169 | + {forms?.map((form, index) => ( |
| 170 | + <div |
| 171 | + key={form.Id} |
| 172 | + className="p-3 rounded-xl border-[1px] border-gray-400 m-4 bg-white text-black grid grid-cols-1 md:grid-cols-2 gap-2 relative" |
| 173 | + > |
| 174 | + {form?.fields?.map((field, fieldIndex) => ( |
| 175 | + <div className="flex flex-col " key={field.fieldId}> |
| 176 | + <label>{field.label}</label> |
| 177 | + <SuggestionInput |
| 178 | + required |
| 179 | + type="email" |
| 180 | + value={field.value} |
| 181 | + index={fieldIndex} |
| 182 | + onChange={(signer) => |
| 183 | + handleInputChange(index, signer, fieldIndex) |
| 184 | + } |
| 185 | + /> |
| 186 | + </div> |
| 187 | + ))} |
| 188 | + {index > 0 && ( |
| 189 | + <button |
| 190 | + onClick={() => handleRemoveForm(index)} |
| 191 | + className="absolute right-3 top-1 border border-gray-300 rounded-lg px-2 py-1" |
| 192 | + > |
| 193 | + <i className="fa-solid fa-trash"></i> |
| 194 | + </button> |
| 195 | + )} |
| 196 | + <div ref={formRef}></div> |
| 197 | + </div> |
| 198 | + ))} |
| 199 | + </div> |
| 200 | + <div className="flex flex-col mx-4 mb-4 gap-3"> |
| 201 | + <button |
| 202 | + onClick={handleAddForm} |
| 203 | + className="bg-[#32a3ac] p-2 text-white w-full rounded-full focus:outline-none" |
| 204 | + > |
| 205 | + <i className="fa-solid fa-plus"></i> <span>Add new</span> |
| 206 | + </button> |
| 207 | + <button |
| 208 | + type="submit" |
| 209 | + className="bg-[#32a3ac] p-2 text-white w-full rounded-full focus:outline-none" |
| 210 | + > |
| 211 | + <i className="fa-solid fa-paper-plane"></i> <span>Send</span> |
| 212 | + </button> |
| 213 | + </div> |
| 214 | + </form> |
| 215 | + </> |
| 216 | + ); |
| 217 | +}; |
| 218 | + |
| 219 | +export default BulkSendUi; |
0 commit comments