diff --git a/src/components/Directory/NotesDirectory.jsx b/src/components/Directory/NotesDirectory.jsx index 41a02097..c92713c5 100644 --- a/src/components/Directory/NotesDirectory.jsx +++ b/src/components/Directory/NotesDirectory.jsx @@ -1,4 +1,4 @@ -import React, {useEffect, useState} from "react"; +import React, { useEffect, useState } from "react"; import Editor from "../common/TextEditor/Editor"; import Avatar from "../common/Avatar"; import { useForm } from "react-hook-form"; @@ -7,7 +7,7 @@ import { zodResolver } from "@hookform/resolvers/zod"; import { showText } from "pdf-lib"; import { DirectoryRepository } from "../../repositories/DirectoryRepository"; import moment from "moment"; -import {getCachedData} from "../../slices/apiDataManager"; +import { cacheData, getCachedData } from "../../slices/apiDataManager"; import NoteCardDirectory from "./NoteCardDirectory"; import showToast from "../../services/toastService"; @@ -15,10 +15,10 @@ const schema = z.object({ note: z.string().min(1, { message: "Note is required" }), }); -const NotesDirectory = ( {isLoading,contactProfile, setProfileContact} ) => -{ - const [ NotesData, setNotesData ] = useState() - const[IsSubmitting,setIsSubmitting] = useState(false) +const NotesDirectory = ({ isLoading, contactProfile, setProfileContact }) => { + const [NotesData, setNotesData] = useState(); + const [IsSubmitting, setIsSubmitting] = useState(false); + const [addNote, setAddNote] = useState(false); const { register, handleSubmit, @@ -40,27 +40,35 @@ const NotesDirectory = ( {isLoading,contactProfile, setProfileContact} ) => const onSubmit = async (data) => { const newNote = { ...data, contactId: contactProfile?.id }; - try - { - setIsSubmitting(true) - const response = await DirectoryRepository.CreateNote( newNote ); + try { + setIsSubmitting(true); + const response = await DirectoryRepository.CreateNote(newNote); - const createdNote = response.data; + 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", ""); - const result = response.data - const cache_notes = getCachedData( "Contact Note" ) - - setIsSubmitting(false) - showToast("Note added successfully!", "success") - } catch ( error ) - { - setIsSubmitting(false) + setIsSubmitting(false); + showToast("Note added successfully!", "success"); + setAddNote(false); + } catch (error) { + setIsSubmitting(false); const msg = error.response.data.message || error.message || @@ -75,26 +83,63 @@ const NotesDirectory = ( {isLoading,contactProfile, setProfileContact} ) => return (
Loading...
Loading...
{" "} +Contact Profile
Loading...
Phone
+Phone
-Created
-Created
+