import React, { useState } from "react"; import ReactQuill from "react-quill"; import moment from "moment"; import Avatar from "../common/Avatar"; import { DirectoryRepository } from "../../repositories/DirectoryRepository"; import showToast from "../../services/toastService"; import { cacheData, getCachedData } from "../../slices/apiDataManager"; import "../common/TextEditor/Editor.css"; import { useActiveInActiveNote } from "../../hooks/useDirectory"; const NoteCardDirectory = ({ noteItem, contactId }) => { const [editing, setEditing] = useState(false); const [editorValue, setEditorValue] = useState(noteItem.note); const [isLoading, setIsLoading] = useState(false); const [isDeleting, setIsDeleting] = useState(false); const [isActivProcess, setActiveProcessing] = useState(false); const [isHovered, setIsHovered] = useState(false); const handleUpdateNote = async () => { try { setIsLoading(true); const payload = { id: noteItem.id, note: editorValue, contactId: contactId, }; const response = await DirectoryRepository.UpdateNote( noteItem.id, payload ); 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 (activeStatue) => { try { activeStatue ? setActiveProcessing(true) : setIsDeleting(true); const resp = await DirectoryRepository.DeleteNote( noteItem.id, activeStatue ); 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); setActiveProcessing(false); showToast( `Note ${activeStatue ? "Restored" : "Deleted"} Successfully`, "success" ); } catch (error) { setIsDeleting(false); const msg = error.response?.data?.message || error.message || "Error occured during API calling"; showToast(msg, "error"); } }; const { mutate: handleActiveInActive, isPending } = useActiveInActiveNote( () => {} ); return (
setIsHovered(true)} // Set hover state to true on mouse enter onMouseLeave={() => setIsHovered(false)} // Set hover state to false on mouse leave >
{noteItem?.createdBy?.firstName} {noteItem?.createdBy?.lastName} {moment .utc(noteItem?.createdAt) .add(5, "hours") .add(30, "minutes") .format("MMMM DD, YYYY [at] hh:mm A")}
{noteItem.isActive ? ( <> setEditing(true)} > {!isPending ? ( handleActiveInActive({ noteId: noteItem?.id, noteStatus: !noteItem?.isActive, }) } > ) : (
Loading...
)} ) : isPending ? ( ) : ( handleActiveInActive({ noteId: noteItem?.id, noteStatus: !noteItem?.isActive, }) } title="Restore" > )}

{editing ? ( <>
setEditing(false)} > Cancel {isLoading ? "Please Wait..." : "Submit"}
) : (
)}
); }; export default NoteCardDirectory;