211 lines
6.5 KiB
JavaScript
211 lines
6.5 KiB
JavaScript
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 ConfirmModal from "../common/ConfirmModal"; // Make sure path is correct
|
|
import "../common/TextEditor/Editor.css";
|
|
|
|
const NoteCardDirectoryEditable = ({
|
|
noteItem,
|
|
contactId,
|
|
onNoteUpdate,
|
|
onNoteDelete,
|
|
}) => {
|
|
const [editing, setEditing] = useState(false);
|
|
const [editorValue, setEditorValue] = useState(noteItem.note);
|
|
const [isLoading, setIsLoading] = useState(false);
|
|
const [isDeleting, setIsDeleting] = useState(false);
|
|
const [isRestoring, setIsRestoring] = useState(false);
|
|
const [isDeleteModalOpen, setIsDeleteModalOpen] = useState(false);
|
|
|
|
const handleUpdateNote = async () => {
|
|
try {
|
|
setIsLoading(true);
|
|
const payload = {
|
|
id: noteItem.id,
|
|
note: editorValue,
|
|
contactId,
|
|
};
|
|
const response = await DirectoryRepository.UpdateNote(noteItem.id, payload);
|
|
|
|
const cachedContactProfile = getCachedData("Contact Profile");
|
|
if (cachedContactProfile?.contactId === contactId) {
|
|
const updatedCache = {
|
|
...cachedContactProfile,
|
|
data: {
|
|
...cachedContactProfile.data,
|
|
notes: cachedContactProfile.data.notes.map((note) =>
|
|
note.id === noteItem.id ? response.data : note
|
|
),
|
|
},
|
|
};
|
|
cacheData("Contact Profile", updatedCache);
|
|
}
|
|
|
|
onNoteUpdate?.(response.data);
|
|
setEditing(false);
|
|
showToast("Note updated successfully", "success");
|
|
} catch (error) {
|
|
showToast("Failed to update note", "error");
|
|
} finally {
|
|
setIsLoading(false);
|
|
}
|
|
};
|
|
|
|
const suspendEmployee = async () => {
|
|
try {
|
|
setIsDeleting(true);
|
|
await DirectoryRepository.DeleteNote(noteItem.id, false);
|
|
onNoteDelete?.(noteItem.id);
|
|
setIsDeleteModalOpen(false);
|
|
showToast("Note deleted successfully", "success");
|
|
} catch (error) {
|
|
showToast("Failed to delete note", "error");
|
|
} finally {
|
|
setIsDeleting(false);
|
|
}
|
|
};
|
|
|
|
const handleRestore = async () => {
|
|
try {
|
|
setIsRestoring(true);
|
|
await DirectoryRepository.DeleteNote(noteItem.id, true);
|
|
onNoteDelete?.(noteItem.id);
|
|
showToast("Note restored successfully", "success");
|
|
} catch (error) {
|
|
showToast("Failed to restore note", "error");
|
|
} finally {
|
|
setIsRestoring(false);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<>
|
|
<div
|
|
className="card p-1 shadow-sm border-1 mb-4 rounded"
|
|
style={{
|
|
width: "100%",
|
|
background: noteItem.isActive ? "#fff" : "#f8f6f6",
|
|
}}
|
|
key={noteItem.id}
|
|
>
|
|
{/* Header */}
|
|
<div className="d-flex justify-content-between align-items-center mb-1">
|
|
<div className="d-flex align-items-center">
|
|
<Avatar
|
|
size="xs"
|
|
firstName={noteItem?.createdBy?.firstName}
|
|
lastName={noteItem?.createdBy?.lastName}
|
|
className="m-0"
|
|
/>
|
|
<div className="d-flex flex-column ms-0">
|
|
<span className="fw-semibold small">
|
|
{noteItem?.createdBy?.firstName} {noteItem?.createdBy?.lastName}
|
|
</span>
|
|
<span className="text-muted" style={{ fontSize: "10px" }}>
|
|
{moment
|
|
.utc(noteItem?.createdAt)
|
|
.add(5, "hours")
|
|
.add(30, "minutes")
|
|
.format("MMMM DD, YYYY [at] hh:mm A")}
|
|
</span>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Action Icons */}
|
|
<div>
|
|
{noteItem.isActive ? (
|
|
<>
|
|
<i
|
|
className="bx bxs-edit bx-sm me-2 text-primary cursor-pointer"
|
|
onClick={() => setEditing(true)}
|
|
title="Edit"
|
|
></i>
|
|
{!isDeleting ? (
|
|
<i
|
|
className="bx bx-trash bx-sm me-2 text-danger cursor-pointer"
|
|
onClick={() => setIsDeleteModalOpen(true)}
|
|
title="Delete"
|
|
></i>
|
|
) : (
|
|
<div className="spinner-border spinner-border-sm text-danger" />
|
|
)}
|
|
</>
|
|
) : isRestoring ? (
|
|
<i className="bx bx-loader-alt bx-spin text-primary"></i>
|
|
) : (
|
|
<i
|
|
className="bx bx-recycle me-2 text-success cursor-pointer"
|
|
onClick={handleRestore}
|
|
title="Restore"
|
|
></i>
|
|
)}
|
|
</div>
|
|
</div>
|
|
|
|
<hr className="mt-0 mb-2" />
|
|
|
|
{/* Editor or Content */}
|
|
{editing ? (
|
|
<>
|
|
<ReactQuill
|
|
value={editorValue}
|
|
onChange={setEditorValue}
|
|
theme="snow"
|
|
className="compact-editor"
|
|
/>
|
|
<div className="d-flex justify-content-end gap-3 mt-2">
|
|
<span
|
|
className="text-secondary cursor-pointer"
|
|
onClick={() => setEditing(false)}
|
|
>
|
|
Cancel
|
|
</span>
|
|
<span
|
|
className="text-primary cursor-pointer"
|
|
onClick={handleUpdateNote}
|
|
>
|
|
{isLoading ? "Saving..." : "Submit"}
|
|
</span>
|
|
</div>
|
|
</>
|
|
) : (
|
|
<div
|
|
className="px-10 pb-2 text-start"
|
|
dangerouslySetInnerHTML={{ __html: noteItem.note }}
|
|
/>
|
|
)}
|
|
</div>
|
|
|
|
{/* Delete Confirm Modal */}
|
|
{isDeleteModalOpen && (
|
|
<div
|
|
className={`modal fade ${isDeleteModalOpen ? "show" : ""}`}
|
|
tabIndex="-1"
|
|
role="dialog"
|
|
style={{
|
|
display: isDeleteModalOpen ? "block" : "none",
|
|
backgroundColor: "rgba(0,0,0,0.5)",
|
|
}}
|
|
aria-hidden="false"
|
|
>
|
|
<ConfirmModal
|
|
type={"delete"}
|
|
header={"Delete Note"}
|
|
message={"Are you sure you want to delete this note?"}
|
|
onSubmit={suspendEmployee}
|
|
onClose={() => setIsDeleteModalOpen(false)}
|
|
loading={isDeleting}
|
|
paramData={noteItem}
|
|
/>
|
|
</div>
|
|
)}
|
|
</>
|
|
);
|
|
};
|
|
|
|
export default NoteCardDirectoryEditable;
|