diff --git a/src/components/ServiceProject/ServiceProjectBranch/ManageBranch.jsx b/src/components/ServiceProject/ServiceProjectBranch/ManageBranch.jsx index 11bcdafd..720fa08f 100644 --- a/src/components/ServiceProject/ServiceProjectBranch/ManageBranch.jsx +++ b/src/components/ServiceProject/ServiceProjectBranch/ManageBranch.jsx @@ -21,6 +21,15 @@ const ManageBranch = ({ closeModal, BranchToEdit = null }) => { error: requestError, } = useBranchDetails(BranchToEdit); + const [contacts, setContacts] = React.useState([ + { + contactPerson: "", + designation: "", + contactEmails: [""], + contactNumbers: [""] + } + ]); + const { projectId } = useParams(); const schema = BranchSchema(); const { @@ -49,14 +58,22 @@ const ManageBranch = ({ closeModal, BranchToEdit = null }) => { reset({ branchName: data.branchName || "", projectId: data.project?.id || projectId || "", - contactInformation: data.contactInformation || "", address: data.address || "", branchType: data.branchType || "", googleMapUrl: data.googleMapUrl || "", }); + + if (data.contactInformation) { + try { + setContacts(JSON.parse(data.contactInformation)); + } catch { + setContacts([]); + } + } } }, [data, reset]); + const { mutate: CreateServiceBranch, isPending: createPending } = useCreateBranch(() => { handleClose(); @@ -65,19 +82,23 @@ const ManageBranch = ({ closeModal, BranchToEdit = null }) => { handleClose() ); - const onSubmit = (fromdata) => { + const onSubmit = (formdata) => { let payload = { - ...fromdata, + ...data, + ...formdata, projectId, + contactInformation: JSON.stringify(contacts), // ← important }; + if (BranchToEdit) { - const editPayload = { ...payload, id: data.id }; - ServiceBranchUpdate({ id: data.id, payload: editPayload }); + ServiceBranchUpdate({ id: data.id, payload }); } else { CreateServiceBranch(payload); } }; + + return (
@@ -100,26 +121,6 @@ const ManageBranch = ({ closeModal, BranchToEdit = null }) => { {errors.branchName.message} )}
-
- - - {errors.contactInformation && ( - - {errors.contactInformation.message} - - )} -
- - -
-
+
+ +
+ +
-
+
+
+ + + {contacts.map((item, index) => ( +
+ + {/* Contact Person + Designation */} +
+
+ { + const list = [...contacts]; + list[index].contactPerson = e.target.value; + setContacts(list); + }} + /> +
+ +
+ { + const list = [...contacts]; + list[index].designation = e.target.value; + setContacts(list); + }} + /> +
+ + {/* Remove entire contact */} +
+ + setContacts(contacts.filter((_, i) => i !== index)) + } + > +
+
+ + {/* Numbers Section */} + + + {item.contactNumbers.map((num, numIndex) => ( +
+ + { + const value = e.target.value.replace(/\D/g, ""); // remove non-digit characters + const list = [...contacts]; + list[index].contactNumbers[numIndex] = value; + setContacts(list); + }} + /> + + {/* Show PLUS only on last row */} + {numIndex === item.contactNumbers.length - 1 ? ( + { + const list = [...contacts]; + list[index].contactNumbers.push(""); + setContacts(list); + }} + > + ) : ( + { + const list = [...contacts]; + list[index].contactNumbers.splice(numIndex, 1); + setContacts(list); + }} + > + )} + +
+ ))} + +
+ + {/* Emails Section */} + + + {item.contactEmails.map((email, emailIndex) => ( +
+ + { + const list = [...contacts]; + list[index].contactEmails[emailIndex] = e.target.value; + setContacts(list); + }} + /> + + {/* Show PLUS only on the last row */} + {emailIndex === item.contactEmails.length - 1 ? ( + { + const list = [...contacts]; + list[index].contactEmails.push(""); + setContacts(list); + }} + > + ) : ( + { + const list = [...contacts]; + list[index].contactEmails.splice(emailIndex, 1); + setContacts(list); + }} + > + )} + +
+ ))} + + +
+ ))} + + + + +
+
+ +
diff --git a/src/components/ServiceProject/ServiceProjectSchema.jsx b/src/components/ServiceProject/ServiceProjectSchema.jsx index dc8a6713..589f1497 100644 --- a/src/components/ServiceProject/ServiceProjectSchema.jsx +++ b/src/components/ServiceProject/ServiceProjectSchema.jsx @@ -138,11 +138,7 @@ export const BranchSchema = () => .trim() .min(1, { message: "Branch Name is required" }), - contactInformation: z - .string() - .trim() - .min(1, { message: "Contact Information is required" }), - + contactInformation: z.string().optional(), address: z .string() .trim() @@ -155,8 +151,6 @@ export const BranchSchema = () => googleMapUrl: z .string() - .trim() - .url({ message: "Enter a valid Google Map URL" }), }); export const defaultBranches = {