diff --git a/src/components/Directory/NoteCardDirectory.jsx b/src/components/Directory/NoteCardDirectory.jsx index 7e145136..a7020732 100644 --- a/src/components/Directory/NoteCardDirectory.jsx +++ b/src/components/Directory/NoteCardDirectory.jsx @@ -4,43 +4,109 @@ import moment from "moment"; import Avatar from "../common/Avatar"; import { DirectoryRepository } from "../../repositories/DirectoryRepository"; import showToast from "../../services/toastService"; -import {getCachedData} from "../../slices/apiDataManager"; -import "../common/TextEditor/Editor.css" +import { cacheData, getCachedData } from "../../slices/apiDataManager"; +import "../common/TextEditor/Editor.css"; -const NoteCardDirectory = ({ noteItem, contactId,setProfileContact }) => { +const NoteCardDirectory = ({ noteItem, contactId, setProfileContact }) => { const [editing, setEditing] = useState(false); - const [ editorValue, setEditorValue ] = useState( noteItem.note ); - const [isLoading,setIsLoading] = useState(false) - const handleUpdateNote= async () => { - try - { - console.log(editorValue) - setIsLoading(true) - const payload ={id:noteItem.id,note:editorValue,contactId:contactId} - - const response = await DirectoryRepository.UpdateNote(noteItem.id, payload); - setProfileContact((prev) => ({ - ...prev, - notes: prev.notes.map((note) => - note.id === noteItem.id ? response?.data : note - ), + const [editorValue, setEditorValue] = useState(noteItem.note); + const [isLoading, setIsLoading] = useState(false); + const [isDeleting, setIsDeleting] = useState(false); + const handleUpdateNote = async () => { + try { + setIsLoading(true); + const payload = { + id: noteItem.id, + note: editorValue, + contactId: contactId, + }; - })); - setEditing( false ); - setIsLoading(false) - showToast("Note Updated successfully", "success") - } catch ( error ) - { - setIsLoading(false) - const msg = error.reponse.data.message || error.message || "Error occured during API calling." - showToast("Failed to update note", "error"); + const response = await DirectoryRepository.UpdateNote( + noteItem.id, + payload + ); + setProfileContact((prev) => ({ + ...prev, + notes: prev.notes.map((note) => + note.id === noteItem.id ? response?.data : note + ), + })); + + const cached_contactProfile = getCachedData("Contact Profile"); + + if ( + cached_contactProfile && + cached_contactProfile.contactId === contactId + ) { + const updatedProfile = { + ...cached_contactProfile, + data: { + ...cached_contactProfile?.data, + notes: cached_contactProfile?.data?.notes.map((note) => + note.id === noteItem.id ? response?.data : note + ), + }, + }; + cacheData("Contact Profile", updatedProfile); + } + setEditing(false); + setIsLoading(false); + showToast("Note Updated successfully", "success"); + } catch (error) { + setIsLoading(false); + const msg = + error.reponse.data.message || + error.message || + "Error occured during API calling."; + showToast("Failed to update note", "error"); } }; + const handleDeleteNote = async () => { + try { + setIsDeleting(true); + const resp = await DirectoryRepository.DeleteNote(noteItem.id); + setProfileContact((prev) => ({ + ...prev, + notes: prev.notes.filter((note) => note.id !== noteItem.id), + })); + const cachedContactProfile = getCachedData("Contact Profile"); + + if ( + cachedContactProfile && + cachedContactProfile.contactId === contactId + ) { + const updatedCache = { + ...cachedContactProfile, + data: { + ...cachedContactProfile?.data, + notes: (cachedContactProfile?.data?.notes || []).filter( + (note) => note.id !== noteItem.id + ), + }, + }; + + cacheData("Contact Profile", updatedCache); + } + setIsDeleting(false); + showToast("Note Deleted Successfully", "success"); + } catch (error) { + setIsDeleting(false); + const msg = + error.response?.data?.message || + error.message || + "Error occured during API calling"; + showToast(msg, "error"); + } + }; return ( -
+
{
- -
+
- -
    -
  • - -
  • -
  • - -
  • -
+ {!isDeleting && ( + + )} + {isDeleting && ( +
+ Loading... +
+ )}
@@ -102,14 +155,21 @@ const NoteCardDirectory = ({ noteItem, contactId,setProfileContact }) => { onChange={setEditorValue} theme="snow" className="compact-editor" - />
- setEditing(false)}> + setEditing(false)} + > Cancel - - {isLoading ? "Please Wait...":"Submit"} + + {isLoading ? "Please Wait..." : "Submit"}