diff --git a/src/components/Directory/ManageDirectory.jsx b/src/components/Directory/ManageDirectory.jsx index 5eae91af..2198c023 100644 --- a/src/components/Directory/ManageDirectory.jsx +++ b/src/components/Directory/ManageDirectory.jsx @@ -1,40 +1,82 @@ -import React, { useEffect } from "react"; -import { useForm, useFieldArray, FormProvider } from "react-hook-form"; +import React, { useEffect, useState } from "react"; +import { + useForm, + useFieldArray, + FormProvider, + useFormContext, +} from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import TagInput from "../common/TagInput"; import { z } from "zod"; +import IconButton from "../common/IconButton"; +import useMaster from "../../hooks/masterHook/useMaster"; +import { useDispatch, useSelector } from "react-redux"; +import { changeMaster } from "../../slices/localVariablesSlice"; +import { useBuckets } from "../../hooks/useDirectory"; +import {useProjects} from "../../hooks/useProjects"; -export const directorySchema = z.object({ - firstName: z.string().min(1, "First Name is required"), - lastName: z.string().min(1, "Last Name is required"), +export const ContactSchema = z.object({ + Name: z.string().min(1, "Name is required"), organization: z.string().min(1, "Organization name is required"), - type: z.string().min(1, "Type is required"), + ContactCategoryId: z.string().optional(), address: z.string().optional(), - description: z.string().min(1, { message: "Description is required" }), - email: z - .array(z.string().email("Invalid email")) - .nonempty("At least one email required"), - phone: z - .array(z.string().regex(/^\d{10}$/, "Phone must be 10 digits")) - .nonempty("At least one phone number is required"), - tags: z.array(z.string()).optional(), + description: z.string().min( 1, {message: "Description is required"} ), + ProjectId :z.string().optional(), + ContactEmails: z + .array( + z.object({ + label: z.string(), + emailAddress: z.string().email("Invalid email"), + }) + ) + .optional() + .default([]), + ContactPhones: z + .array( + z.object({ + label: z.string(), + phoneNumber: z.string().regex(/^\d{10}$/, "Phone must be 10 digits"), + }) + ) + .optional() + .default([]), + + tags: z + .array( + z.object({ + id: z.string().nullable(), + name: z.string(), + }) + ) + .optional(), + BucketIds: z.array(z.string()).optional(), }); +const ManageDirectory = ({submitContact,onCLosed}) => { + const selectedMaster = useSelector( + (store) => store.localVariables.selectedMaster + ); + const [categoryData, setCategoryData] = useState([]); + const [TagsData, setTagsData] = useState([]); + const { data, loading } = useMaster(); + const {buckets, loading: bucketsLoaging} = useBuckets(); + const {projects, loading: projectLoading} = useProjects(); + const [IsSubmitting,setSubmitting] = useState(false) + const dispatch = useDispatch(); - -const ManageDirectory = () => { const methods = useForm({ - resolver: zodResolver(directorySchema), + resolver: zodResolver(ContactSchema), defaultValues: { - firstName: "", - lastName: "", + Name: "", organization: "", - type: "", + ContactCategoryId: null, address: "", description: "", - email: [""], - phone: [""], + ProjectId:null, + ContactEmails: [], + ContactPhones: [], tags: [], + BucketIds: [], }, }); @@ -44,6 +86,9 @@ const ManageDirectory = () => { control, getValues, trigger, + setValue, + watch, + reset, formState: { errors }, } = methods; @@ -51,160 +96,357 @@ const ManageDirectory = () => { fields: emailFields, append: appendEmail, remove: removeEmail, - } = useFieldArray({ control, name: "email" }); + } = useFieldArray({ control, name: "ContactEmails" }); const { fields: phoneFields, append: appendPhone, remove: removePhone, - } = useFieldArray({ control, name: "phone" }); + } = useFieldArray({ control, name: "ContactPhones" }); useEffect(() => { - if (emailFields.length === 0) appendEmail(""); - if (phoneFields.length === 0) appendPhone(""); + if (emailFields.length === 0) appendEmail(""); + if (phoneFields.length === 0) appendPhone(""); }, [emailFields.length, phoneFields.length]); - const onSubmit = (data) => { - // console.log("Submitted:\n" + JSON.stringify(data, null, 2)); - }; + const handleAddEmail = async () => { - const emails = getValues("email"); + const emails = getValues("ContactEmails"); const lastIndex = emails.length - 1; - const valid = await trigger(`email.${lastIndex}`); - if (valid) appendEmail(""); + const valid = await trigger(`ContactEmails.${lastIndex}.emailAddress`); + if (valid) { + appendEmail({ label: "Work", emailAddress: "" }); + } }; const handleAddPhone = async () => { - const phones = getValues("phone"); + const phones = getValues("ContactPhones"); const lastIndex = phones.length - 1; - const valid = await trigger(`phone.${lastIndex}`); - if (valid) appendPhone(""); + const valid = await trigger(`ContactPhones.${lastIndex}.phoneNumber`); + if (valid) { + appendPhone({ label: "Office", phoneNumber: "" }); + } }; + useEffect(() => { + if (selectedMaster === "Contact Category") { + setCategoryData(data); + } else { + setTagsData(data); + } + }, [selectedMaster, data]); + + const watchBucketIds = watch("BucketIds"); + + const toggleBucketId = (id) => { + const updated = watchBucketIds?.includes(id) + ? watchBucketIds.filter((val) => val !== id) + : [...watchBucketIds, id]; + + setValue("BucketIds", updated, { shouldValidate: true }); + }; + const handleCheckboxChange = (id) => { + const updated = watchBucketIds.includes(id) + ? watchBucketIds.filter((i) => i !== id) + : [...watchBucketIds, id]; + + setValue("BucketIds", updated, { shouldValidate: true }); + }; + + + + + const onSubmit = ( data ) => + { + setSubmitting(true) + submitContact( data, reset, setSubmitting ) + + }; return (
+
+ {" "} +
Create New Contact
+
- - - {errors.firstName && {errors.firstName.message}} + + + {errors.Name && ( + {errors.Name.message} + )}
- - - {errors.lastName && {errors.lastName.message}} + + + {errors.organization && ( + + {errors.organization.message} + + )}
- -
- - - {errors.organization && {errors.organization.message}} -
- -
+
- - {emailFields.map((field, index) => (<> -
- - {index === emailFields.length - 1 ? ( - + ) : ( + + )} +
+ {errors.ContactEmails?.[index]?.emailAddress && ( + + {errors.ContactEmails[index].emailAddress.message} + + )} +
- {errors.email?.[index] && ( - - {errors.email[index]?.message} - - )} - ))} - -
- - {phoneFields.map((field, index) => (<> -
- - {index === phoneFields.length - 1 ? ( - + ) : ( + + )} +
+ {errors.ContactPhones?.[index]?.phoneNumber && ( + + {errors.ContactPhones[index].phoneNumber.message} + + )} +
- {errors.phone?.[ index ] && {errors.phone[ index ]?.message}} - ))} -
- - - {errors.type && {errors.type.message}} + + + {errors.ContactCategoryId && ( + {errors.ContactCategoryId.message} + )}
- + +
+
+
+
+ + +
+ {bucketsLoaging &&

Loading...

} + {buckets?.map((item) => ( +
+
+ handleCheckboxChange(item.id)} + /> + +
+
+ ))} +
+ + {errors.BucketIds && ( + {errors.BucketIds.message} + )} +
+ +
+ + + {errors.category && ( + {errors.category.message} + )}
-