Add restore feature for deleted notes

This commit is contained in:
Pramod Mahajan 2025-05-29 20:08:31 +05:30 committed by pramod.mahajan
parent 36e7755b7d
commit a88a8d3df9
2 changed files with 83 additions and 51 deletions

View File

@ -7,11 +7,12 @@ import showToast from "../../services/toastService";
import { cacheData, getCachedData } from "../../slices/apiDataManager"; import { cacheData, getCachedData } from "../../slices/apiDataManager";
import "../common/TextEditor/Editor.css"; import "../common/TextEditor/Editor.css";
const NoteCardDirectory = ({ IsActive,noteItem, contactId, setProfileContact }) => { const NoteCardDirectory = ({refetchProfile,refetchNotes, noteItem, contactId, setProfileContact, }) => {
const [editing, setEditing] = useState(false); const [editing, setEditing] = useState(false);
const [editorValue, setEditorValue] = useState(noteItem.note); const [editorValue, setEditorValue] = useState(noteItem.note);
const [isLoading, setIsLoading] = useState(false); const [isLoading, setIsLoading] = useState(false);
const [ isDeleting, setIsDeleting ] = useState( false ); const [ isDeleting, setIsDeleting ] = useState( false );
const [isActivProcess,setActiveProcessing]= useState(false)
const handleUpdateNote = async () => { const handleUpdateNote = async () => {
try { try {
setIsLoading(true); setIsLoading(true);
@ -63,10 +64,10 @@ const NoteCardDirectory = ({ IsActive,noteItem, contactId, setProfileContact })
} }
}; };
const handleDeleteNote = async () => { const handleDeleteNote = async (activeStatue) => {
try { try {
setIsDeleting(true); activeStatue ? setActiveProcessing(true) : setIsDeleting(true)
const resp = await DirectoryRepository.DeleteNote(noteItem.id); const resp = await DirectoryRepository.DeleteNote(noteItem.id,activeStatue);
setProfileContact((prev) => ({ setProfileContact((prev) => ({
...prev, ...prev,
notes: prev.notes.filter((note) => note.id !== noteItem.id), notes: prev.notes.filter((note) => note.id !== noteItem.id),
@ -91,7 +92,10 @@ const NoteCardDirectory = ({ IsActive,noteItem, contactId, setProfileContact })
cacheData("Contact Profile", updatedCache); cacheData("Contact Profile", updatedCache);
} }
setIsDeleting( false ); setIsDeleting( false );
showToast("Note Deleted Successfully", "success"); setActiveProcessing( false )
refetchNotes( contactId, false )
refetchProfile(contactId)
showToast(`Note ${activeStatue ? "Restored":"Deleted"} Successfully`, "success");
} catch (error) { } catch (error) {
setIsDeleting(false); setIsDeleting(false);
const msg = const msg =
@ -101,13 +105,13 @@ const NoteCardDirectory = ({ IsActive,noteItem, contactId, setProfileContact })
showToast(msg, "error"); showToast(msg, "error");
} }
}; };
return ( return (
<div <div
className="card p-1 shadow-sm border-1 mb-2 conntactNote" className="card p-1 shadow-sm border-1 mb-2 conntactNote"
style={{ width: "100%", minWidth: "300px", borderRadius: "0px", background: `${!IsActive ? "#f8f6f6" : ""}` }} style={{ width: "100%", minWidth: "300px", borderRadius: "0px", background: `${noteItem.isActive ? "": "#f8f6f6"}` }}
key={noteItem.id} key={noteItem.id}
> >
<div className="d-flex justify-content-between align-items-center mb-1"> <div className="d-flex justify-content-between align-items-center mb-1">
<div className="d-flex align-items-center"> <div className="d-flex align-items-center">
<Avatar <Avatar
@ -129,26 +133,39 @@ const NoteCardDirectory = ({ IsActive,noteItem, contactId, setProfileContact })
</span> </span>
</div> </div>
</div> </div>
<div className={`${IsActive ? " ":"d-none"}`}> <div>
{noteItem.isActive ? (
<>
<i <i
className="bx bxs-edit bx-sm me-1 text-primary cursor-pointer" className="bx bxs-edit bx-sm me-1 text-primary cursor-pointer"
onClick={() => setEditing(true)} onClick={() => setEditing(true)}
></i> ></i>
{!isDeleting && (
{!isDeleting ? (
<i <i
className="bx bx-trash bx-sm me-1 text-secondary cursor-pointer" className="bx bx-trash bx-sm me-1 text-secondary cursor-pointer"
onClick={handleDeleteNote} onClick={() => handleDeleteNote(!noteItem.isActive)}
></i> ></i>
)} ) : (
{isDeleting && (
<div <div
class="spinner-border spinner-border-sm text-secondary" className="spinner-border spinner-border-sm text-secondary"
role="status" role="status"
> >
<span class="visually-hidden">Loading...</span> <span className="visually-hidden">Loading...</span>
</div> </div>
)} )}
</>
) : isActivProcess ? (
< i className='bx bx-refresh text-primary bx-spin' ></i>
) : (
<i
className="bx bx-history me-1 text-primary cursor-pointer"
onClick={() => handleDeleteNote(!noteItem.isActive)}
title="Restore"
></i>
)}
</div> </div>
</div> </div>
<hr className="mt-0" /> <hr className="mt-0" />

View File

@ -16,9 +16,9 @@ const schema = z.object({
note: z.string().min(1, { message: "Note is required" }), note: z.string().min(1, { message: "Note is required" }),
}); });
const NotesDirectory = ({ isLoading, contactProfile, setProfileContact }) => { const NotesDirectory = ({refetchProfile, isLoading, contactProfile, setProfileContact }) => {
const [IsActive, setIsActive] = useState(true); const [IsActive, setIsActive] = useState(true);
const { contactNotes } = useContactNotes(contactProfile?.id, !IsActive); const {contactNotes,refetch} = useContactNotes( contactProfile?.id, true );
const [NotesData, setNotesData] = useState(); const [NotesData, setNotesData] = useState();
const [IsSubmitting, setIsSubmitting] = useState(false); const [IsSubmitting, setIsSubmitting] = useState(false);
@ -85,6 +85,14 @@ const NotesDirectory = ({ isLoading, contactProfile, setProfileContact }) => {
const onCancel = () => { const onCancel = () => {
setValue("note", ""); setValue("note", "");
}; };
const handleSwitch = () =>
{
setIsActive( !IsActive )
if ( IsActive )
{
refetch(contactProfile?.id, false)
}
}
return ( return (
<div className="text-start"> <div className="text-start">
@ -95,7 +103,7 @@ const NotesDirectory = ({ isLoading, contactProfile, setProfileContact }) => {
<input <input
type="checkbox" type="checkbox"
className="switch-input" className="switch-input"
onChange={() => setIsActive(!IsActive)} onChange={()=>handleSwitch(!IsActive)}
value={IsActive} value={IsActive}
/> />
<span className="switch-toggle-slider"> <span className="switch-toggle-slider">
@ -106,8 +114,12 @@ const NotesDirectory = ({ isLoading, contactProfile, setProfileContact }) => {
{/* <i class="icon-base bx bx-x"></i> */} {/* <i class="icon-base bx bx-x"></i> */}
</span> </span>
</span> </span>
<span className="switch-label small-text">Show Inactive Notes</span> <span className="switch-label small-text">Show Including Inactive Notes</span>
</label> </label>
</div>
</div>
<div className="d-flex justify-content-end px-2">
<span <span
className={`btn btn-xs ${addNote ? "btn-danger" : "btn-primary"}`} className={`btn btn-xs ${addNote ? "btn-danger" : "btn-primary"}`}
onClick={() => setAddNote(!addNote)} onClick={() => setAddNote(!addNote)}
@ -120,7 +132,6 @@ const NotesDirectory = ({ isLoading, contactProfile, setProfileContact }) => {
{addNote ? "close" : "Add Note"} {addNote ? "close" : "Add Note"}
</span> </span>
</div> </div>
</div>
{addNote && ( {addNote && (
<form onSubmit={handleSubmit(onSubmit)}> <form onSubmit={handleSubmit(onSubmit)}>
<Editor <Editor
@ -147,26 +158,30 @@ const NotesDirectory = ({ isLoading, contactProfile, setProfileContact }) => {
.reverse() .reverse()
.map((noteItem) => ( .map((noteItem) => (
<NoteCardDirectory <NoteCardDirectory
IsActive={IsActive} refetchProfile={refetchProfile}
refetchNotes={refetch}
refetchContact ={refetch}
noteItem={noteItem} noteItem={noteItem}
contactId={contactProfile?.id} contactId={contactProfile?.id}
setProfileContact={setProfileContact} setProfileContact={setProfileContact}
key={noteItem.id} key={noteItem.id}
/> />
))} ))}
{IsActive && ( {IsActive && (
<p> <div>
{!isLoading && contactProfile?.notes.length == 0 && !addNote && ( {!isLoading && contactProfile?.notes.length == 0 && !addNote && (
<p className="text-center">No Notes Found</p> <div className="text-center">No Notes Found</div>
)} )}
</p> </div>
)} )}
{!IsActive && ( {!IsActive && (
<p> <div>
{!isLoading && contactNotes.length == 0 && !addNote && ( {!isLoading && contactNotes.length == 0 && !addNote && (
<p className="text-center">No Notes Found</p> <div className="text-center">No Notes Found</div>
)} )}
</p> </div>
)} )}
</div> </div>
</div> </div>