import React, { useEffect } from "react"; import { useProjectName } from "../../../hooks/useProjects"; import { useForm } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import Label from "../../common/Label"; import { useBranchDetails, useBranchTypes, useCreateBranch, useServiceProjects, useUpdateBranch, } from "../../../hooks/useServiceProject"; import { useAppForm } from "../../../hooks/appHooks/useAppForm"; import { useParams } from "react-router-dom"; import { BranchSchema, defaultBranches } from "../ServiceProjectSchema"; import InputSuggessionField from "../../common/Forms/InputSuggesstionField"; import InputSuggestions from "../../common/InputSuggestion"; const ManageBranch = ({ closeModal, BranchToEdit = null }) => { const { data, isLoading, isError, error: requestError, } = useBranchDetails(BranchToEdit); const { data: branchTypes } = useBranchTypes(); const [contacts, setContacts] = React.useState([ { contactPerson: "", designation: "", contactEmails: [""], contactNumbers: [""], }, ]); const { projectId } = useParams(); const schema = BranchSchema(); const { register, control, watch, handleSubmit, setValue, reset, formState: { errors }, } = useAppForm({ resolver: zodResolver(schema), defaultValues: { ...defaultBranches, projectId: projectId || "", }, }); const handleClose = () => { reset(); closeModal(); }; useEffect(() => { if (BranchToEdit && data) { reset({ branchName: data.branchName || "", projectId: data.project?.id || projectId || "", 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(); }); const { mutate: ServiceBranchUpdate, isPending } = useUpdateBranch(() => handleClose() ); const onSubmit = (formdata) => { let payload = { ...data, ...formdata, projectId, contactInformation: JSON.stringify(contacts), // ← important }; if (BranchToEdit) { ServiceBranchUpdate({ id: data.id, payload }); } else { CreateServiceBranch(payload); } }; return (
{BranchToEdit ? "Update Branch" : "Create Branch"}
{errors.branchName && ( {errors.branchName.message} )}
setValue("branchType", val, { shouldValidate: true }) } error={errors.branchType?.message} />
{errors.googleMapUrl && ( {errors.googleMapUrl.message} )}
{contacts.map((item, index) => (
{" "}
setContacts(contacts.filter((_, i) => i !== index)) } >
{ const list = [...contacts]; list[index].contactPerson = e.target.value; setContacts(list); }} />
{ const list = [...contacts]; list[index].designation = e.target.value; setContacts(list); }} />
{/* 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); }} > )}
))}
))}