diff --git a/src/components/Directory/NotesDirectory.jsx b/src/components/Directory/NotesDirectory.jsx index 61908853..f310bfcc 100644 --- a/src/components/Directory/NotesDirectory.jsx +++ b/src/components/Directory/NotesDirectory.jsx @@ -4,7 +4,6 @@ import Avatar from "../common/Avatar"; import { useForm } from "react-hook-form"; import { z } from "zod"; import { zodResolver } from "@hookform/resolvers/zod"; -import { showText } from "pdf-lib"; import { DirectoryRepository } from "../../repositories/DirectoryRepository"; import moment from "moment"; import { cacheData, getCachedData } from "../../slices/apiDataManager"; @@ -19,15 +18,14 @@ const schema = z.object({ const NotesDirectory = ({ refetchProfile, isLoading, - contactProfile, + contactProfile, // This contactProfile now reliably includes firstName, middleName, lastName, and fullName setProfileContact, }) => { const [IsActive, setIsActive] = useState(true); - const { contactNotes, refetch } = useContactNotes(contactProfile?.id, true); + const { contactNotes, refetch } = useContactNotes(contactProfile?.id, IsActive); - const [NotesData, setNotesData] = useState(); const [IsSubmitting, setIsSubmitting] = useState(false); - const [addNote, setAddNote] = useState(true); + const [showEditor, setShowEditor] = useState(false); const { register, handleSubmit, @@ -67,102 +65,115 @@ const NotesDirectory = ({ ) { const updatedProfile = { ...cached_contactProfile.data, - notes: [...(cached_contactProfile.notes || []), createdNote], + notes: [...(cached_contactProfile.data.notes || []), createdNote], }; - cacheData("Contact Profile", updatedProfile); + cacheData("Contact Profile", { contactId: contactProfile?.id, data: updatedProfile }); } setValue("note", ""); setIsSubmitting(false); showToast("Note added successfully!", "success"); - setAddNote(true); + setShowEditor(false); setIsActive(true); + refetch(contactProfile?.id, true); } catch (error) { setIsSubmitting(false); const msg = - error.response.data.message || + error.response?.data?.message || error.message || - "Error occured during API calling"; + "Error occurred during API calling"; showToast(msg, "error"); } }; const onCancel = () => { - setValue( "note", "" ); - + setValue("note", ""); + setShowEditor(false); }; + const handleSwitch = () => { - setIsActive(!IsActive); - if (IsActive) { - refetch(contactProfile?.id, false); - } + setIsActive((prevIsActive) => { + const newState = !prevIsActive; + refetch(contactProfile?.id, newState); + return newState; + }); }; + // Use the fullName from contactProfile, which now includes middle and last names if available + const contactName = contactProfile?.fullName || contactProfile?.firstName || "Contact"; + const noNotesMessage = `Be the first to share your insights! ${contactName} currently has no notes.`; + + const notesToDisplay = IsActive ? (contactProfile?.notes || []) : (contactNotes || []); + return ( -
+
-

Notes :

+

Notes :

-
- {contactNotes?.length > 0 ? ( - - ) : ( -
- +
+ {(contactProfile?.notes?.length > 0 || contactNotes?.length > 0) && ( + + )} + {!(contactProfile?.notes?.length > 0 || contactNotes?.length > 0) && ( +
+ +
+ )} +
+ + {!showEditor && ( +
+ +
+ )} +
- )} -
-
- setAddNote(!addNote)} - > - {addNote ? "Hide Editor" : "Add a Note"} - -
-
- - - {addNote && ( -
+ {showEditor && ( +
- {/*
- setAddNote(!addNote)} - > - {addNote ? "Hide Editor" : "Add Note"} - -
*/}
- {errors.notes && ( + {errors.note && (

{errors.note.message}

)}
)} -
+
{isLoading && (
{" "}

Loading...

{" "}
)} - {!isLoading && - [...(IsActive ? contactProfile?.notes || [] : contactNotes || [])] + {!isLoading && notesToDisplay.length > 0 ? ( + notesToDisplay + .slice() .reverse() .map((noteItem) => ( - ))} - - {IsActive && ( -
- {!isLoading && contactProfile?.notes.length == 0 && !addNote && ( -
No Notes Found
- )} -
- )} - {!IsActive && ( -
- {!isLoading && contactNotes.length == 0 && !addNote && ( -
No Notes Found
- )} -
+ )) + ) : ( + !isLoading && !showEditor && ( +
+ {noNotesMessage} +
+ ) )}
); }; -export default NotesDirectory; +export default NotesDirectory; \ No newline at end of file diff --git a/src/components/Directory/ProfileContactDirectory.jsx b/src/components/Directory/ProfileContactDirectory.jsx index 0509db50..3a518272 100644 --- a/src/components/Directory/ProfileContactDirectory.jsx +++ b/src/components/Directory/ProfileContactDirectory.jsx @@ -8,9 +8,10 @@ const ProfileContactDirectory = ({ contact, setOpen_contact, closeModal }) => { const { contactProfile, loading, refetch } = useContactProfile(contact?.id); const [copiedIndex, setCopiedIndex] = useState(null); - const [profileContact, setProfileContact] = useState(); + const [profileContactState, setProfileContactState] = useState(null); const [expanded, setExpanded] = useState(false); - const description = contactProfile?.description || ""; + + const description = profileContactState?.description || ""; const limit = 500; const toggleReadMore = () => setExpanded(!expanded); @@ -19,14 +20,51 @@ const ProfileContactDirectory = ({ contact, setOpen_contact, closeModal }) => { const displayText = expanded ? description : description.slice(0, limit) + (isLong ? "..." : ""); + useEffect(() => { - setProfileContact(contactProfile); - }, [contactProfile]); + if (contactProfile) { + const names = (contact?.name || "").trim().split(" "); + let firstName = ""; + let middleName = ""; + let lastName = ""; + let fullName = contact?.name || ""; + + // Logic to determine first, middle, and last names + if (names.length === 1) { + firstName = names[0]; + } else if (names.length === 2) { + firstName = names[0]; + lastName = names[1]; + } else if (names.length >= 3) { + firstName = names[0]; + middleName = names[1]; // This was an error in the original prompt, corrected to names[1] + lastName = names[names.length - 1]; + // Reconstruct full name to be precise with spacing + fullName = `${firstName} ${middleName ? middleName + ' ' : ''}${lastName}`; + } else { + // Fallback if no names or empty string + firstName = "Contact"; + fullName = "Contact"; + } + + + setProfileContactState({ + ...contactProfile, + firstName: contactProfile.firstName || firstName, + // Adding middleName and lastName to the state for potential future use or more granular access + middleName: contactProfile.middleName || middleName, + lastName: contactProfile.lastName || lastName, + fullName: contactProfile.fullName || fullName, // Prioritize fetched fullName, fallback to derived + }); + } + }, [contactProfile, contact?.name]); + const handleCopy = (email, index) => { navigator.clipboard.writeText(email); setCopiedIndex(index); - setTimeout(() => setCopiedIndex(null), 2000); // Reset after 2 seconds + setTimeout(() => setCopiedIndex(null), 2000); }; + return (
@@ -47,36 +85,34 @@ const ProfileContactDirectory = ({ contact, setOpen_contact, closeModal }) => {
{contact?.name} - {contactProfile?.designation} + {profileContactState?.designation}
-
+
- {contactProfile?.contactEmails?.length > 0 && ( -
-
- -

Email :

-
- -

Email :

+ {profileContactState?.contactEmails?.length > 0 && ( +
+
+ + + Email + + :
+
    - {contactProfile.contactEmails.map((email, idx) => ( + {profileContactState.contactEmails.map((email, idx) => (
  • - + {email.emailAddress} {
)} - {contactProfile?.contactPhones?.length > 0 && ( -
-
- -

Phone :

+ {profileContactState?.contactPhones?.length > 0 && ( +
+
+ + + Phone + + :
+
    - {contactProfile.contactPhones.map((phone, idx) => ( + {profileContactState.contactPhones.map((phone, idx) => (
  • {phone.phoneNumber} - {idx < contactProfile.contactPhones.length - 1 && ","} + {idx < profileContactState.contactPhones.length - 1 && ","}
  • ))}
@@ -111,89 +148,92 @@ const ProfileContactDirectory = ({ contact, setOpen_contact, closeModal }) => {
)} - - {contactProfile?.createdAt && ( -
-
- -

Created :

-
- -

Created :

+ {profileContactState?.createdAt && ( +
+
+ + + Created + + :
+
-
  • - {moment(contactProfile.createdAt).format("MMMM, DD YYYY")} -
  • + + {moment(profileContactState.createdAt).format("MMMM DD, YYYY")} +
    )} - {contactProfile?.address && ( -
    -
    - -

    Location :

    -
    - -

    Location :

    -
    -
    - - {contactProfile.address} + + {profileContactState?.address && ( +
    +
    + + + Location + : +
    +
    + {profileContactState.address}
    )}
    - {contactProfile?.organization && ( -
    -
    - -

    Organization :

    -
    - -

    Organization :

    + {profileContactState?.organization && ( +
    +
    + + + Organization + + :
    +
    - {contactProfile.organization} + {profileContactState.organization}
    )} - {contactProfile?.contactCategory && ( -
    -
    - -

    Category :

    -
    - -

    Category :

    + + {profileContactState?.contactCategory && ( +
    +
    + + + Category + + :
    +
    • - {contactProfile.contactCategory.name} + {profileContactState.contactCategory.name}
    )} - {contactProfile?.tags?.length > 0 && ( - {contactProfile?.tags?.length > 0 && ( -
    -
    - -

    Tags :

    -
    - -

    Tags :

    + + {profileContactState?.tags?.length > 0 && ( +
    +
    + + + Tags + + :
    +
      - {contactProfile.tags.map((tag, index) => ( + {profileContactState.tags.map((tag, index) => (
    • {tag.name}
    • @@ -203,84 +243,76 @@ const ProfileContactDirectory = ({ contact, setOpen_contact, closeModal }) => {
    )} - {contactProfile?.buckets?.length > 0 && ( -
    -
    - {contactProfile?.contactEmails?.length > 0 && ( -
    -
    -

    - Buckets : -

    -
    -

    - Buckets : -

    -
    -
    -
      - {contactProfile.buckets.map((bucket) => ( -
    • - - {bucket.name} - -
    • - ))} -
    -
    -
    - )} + {profileContactState?.buckets?.length > 0 && ( +
    +
    + + + Buckets + + : +
    + +
    +
      + {profileContactState.buckets.map((bucket) => ( +
    • + + {bucket.name} + +
    • + ))} +
    +
    )} - -
    + + {profileContactState?.projects?.length > 0 && ( +
    +
    + + + Projects + + : +
    + +
    +
      + {profileContactState.projects.map((project, index) => ( +
    • + {project.name} + {index < profileContactState.projects.length - 1 && ","} +
    • + ))} +
    +
    +
    + )}
    - {contactProfile?.projects?.length > 0 && ( -
    -
    -

    - Projects : -

    -
    -

    - Projects : -

    -
    -
    -
      - {contactProfile.projects.map((project, index) => ( -
    • - {project.name} - {index < contactProfile.projects.length - 1 && ","} -
    • - ))} -
    -
    + +
    +
    + + + Description + + :
    - )} - - -
    -
    -

    - Description : -

    -
    -

    - Description : -

    -
    {displayText} {isLong && ( - - {expanded ? "Read less" : "Read more"} - + <> +
    + + {expanded ? "Read less" : "Read more"} + + )}
    @@ -291,12 +323,12 @@ const ProfileContactDirectory = ({ contact, setOpen_contact, closeModal }) => {
    ); }; -export default ProfileContactDirectory; +export default ProfileContactDirectory; \ No newline at end of file