From c784d18427c48f7bad0a43ed2f33a886342e18d3 Mon Sep 17 00:00:00 2001 From: Pramod Mahajan Date: Wed, 21 May 2025 16:28:44 +0530 Subject: [PATCH] updated cache --- src/components/Directory/NotesDirectory.jsx | 115 +++++++++---- .../Directory/ProfileContactDirectory.jsx | 151 +++++++----------- 2 files changed, 135 insertions(+), 131 deletions(-) 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 (
-
- - {errors.notes && ( -

{errors.note.message}

+
0 + ? "d-flex justify-content-between" + : "d-flex justify-content-end" + }`} + > + {contactProfile?.notes.length > 0 && ( +

Notes :

)} - + setAddNote(!addNote)} + > + + {addNote ? "" : "Add Note"} + {" "} + {" "} + +
+ {addNote && ( +
+ + {errors.notes && ( +

{errors.note.message}

+ )} + + )}
- {isLoading &&

Loading...

} - {!isLoading && contactProfile?.notes?.map((noteItem) => ( - - ))} + {isLoading && ( +
+ {" "} +

Loading...

{" "} +
+ )} + {!isLoading && + [...(contactProfile?.notes || [])] + .reverse() + .map((noteItem) => ( + + ))}
); diff --git a/src/components/Directory/ProfileContactDirectory.jsx b/src/components/Directory/ProfileContactDirectory.jsx index a46c2001..7b7a3ed4 100644 --- a/src/components/Directory/ProfileContactDirectory.jsx +++ b/src/components/Directory/ProfileContactDirectory.jsx @@ -5,7 +5,7 @@ import moment from "moment"; import NotesDirectory from "./NotesDirectory"; const ProfileContactDirectory = ({ contact, setOpen_contact, closeModal }) => { - const { conatProfile ,loading} = useContactProfile(contact?.id); + const { conatProfile, loading } = useContactProfile(contact?.id); const [activeTab, setActiveTab] = useState("profile"); const [profileContact, setProfileContact] = useState(); @@ -14,7 +14,7 @@ const ProfileContactDirectory = ({ contact, setOpen_contact, closeModal }) => { }, [conatProfile]); return (
-
+

Contact Profile

@@ -36,106 +36,65 @@ const ProfileContactDirectory = ({ contact, setOpen_contact, closeModal }) => {
- -
- - {/* Tabs */} - - {loading &&

Loading...

} - {!loading && ( - - )} - - {/* Tab Content */} -
- {activeTab === "notes" && ( - +
+ {conatProfile?.contactEmails?.length > 0 && ( +
+
+

Email

+
+
+
    + {conatProfile.contactEmails.map((email, idx) => ( +
  • + + {email.emailAddress} +
  • + ))} +
+
+
+ )} + {conatProfile?.contactPhones?.length > 0 && ( +
+
+

Phone

+
+
+
    + {conatProfile.contactPhones.map((phone, idx) => ( +
  • + + {phone.phoneNumber} +
  • + ))} +
+
+
)} - {activeTab === "profile" && ( -
-
- {conatProfile?.contactEmails?.length > 0 && ( -
-
-

Email

-
-
-
    - {conatProfile.contactEmails.map((email, idx) => ( -
  • - - {email.emailAddress} -
  • - ))} -
-
-
- )} - {conatProfile?.contactPhones?.length > 0 && ( -
-
-

Phone

-
-
-
    - {conatProfile.contactPhones.map((phone, idx) => ( -
  • - - {phone.phoneNumber} -
  • - ))} -
-
-
- )} - {conatProfile?.createdAt && ( -
-
-

Created

-
-
-
    -
  • - - {moment(conatProfile.createdAt).format( - "MMMM, DD YYYY" - )} -
  • -
-
-
- )} + {conatProfile?.createdAt && ( +
+
+

Created

+
+
+
    +
  • + + {moment(conatProfile.createdAt).format("MMMM, DD YYYY")} +
  • +
)}
+ +
+
);