import React, { useEffect, useState } from "react"; import Editor from "../common/TextEditor/Editor"; 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"; import NoteCardDirectory from "./NoteCardDirectory"; import showToast from "../../services/toastService"; import {useContactNotes} from "../../hooks/useDirectory"; const schema = z.object({ note: z.string().min(1, { message: "Note is required" }), }); const NotesDirectory = ( {isLoading, contactProfile, setProfileContact} ) => { const [ IsActive, setIsActive ] = useState( true ) const {contactNotes} = useContactNotes(contactProfile?.id,!IsActive) const [NotesData, setNotesData] = useState(); const [IsSubmitting, setIsSubmitting] = useState(false); const [addNote, setAddNote] = useState(false); const { register, handleSubmit, setValue, watch, formState: { errors }, } = useForm({ resolver: zodResolver(schema), defaultValues: { note: "", }, }); const noteValue = watch("note"); const handleEditorChange = (value) => { setValue("note", value, { shouldValidate: true }); }; const onSubmit = async (data) => { const newNote = { ...data, contactId: contactProfile?.id }; try { setIsSubmitting(true); const response = await DirectoryRepository.CreateNote(newNote); const createdNote = response.data; setProfileContact((prev) => ({ ...prev, notes: [...(prev.notes || []), createdNote], })); const cached_contactProfile = getCachedData("Contact Profile"); if ( cached_contactProfile && cached_contactProfile.contactId === contactProfile?.id ) { const updatedProfile = { ...cached_contactProfile.data, notes: [...(cached_contactProfile.notes || []), createdNote], }; cacheData("Contact Profile", updatedProfile); } setValue("note", ""); setIsSubmitting(false); showToast("Note added successfully!", "success"); setAddNote( false ); setIsActive(true) } catch (error) { setIsSubmitting(false); const msg = error.response.data.message || error.message || "Error occured during API calling"; showToast(msg, "error"); } }; const onCancel = () => { setValue("note", ""); }; return (

Notes :

setAddNote( !addNote )} > {/* */} {addNote ? "close" : "Add Note"}
{addNote && (
{errors.notes && (

{errors.note.message}

)} )}
{isLoading && (
{" "}

Loading...

{" "}
)} {!isLoading && [...(IsActive ? contactProfile?.notes || [] : contactNotes || [])] .reverse() .map((noteItem) => ( ) )} {IsActive && (

{!isLoading && contactProfile?.notes.length == 0 && !addNote && (

No Notes Found

)}

)} {!IsActive && (

{!isLoading && contactNotes.length == 0 && !addNote && (

No Notes Found

) }

)}
); }; export default NotesDirectory;