-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArchivedNotes.js
More file actions
70 lines (63 loc) · 2.09 KB
/
ArchivedNotes.js
File metadata and controls
70 lines (63 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// ArchivedNotesPage.js
import React, { useEffect, useState } from 'react';
import { getArchivedNotes, deleteNote, unarchiveNote, getNoteCategory } from '../api';
import NoteCard from '../Components/NoteCard';
const ArchivedNotesPage = () => {
const [notes, setNotes] = useState([]);
const user = JSON.parse(localStorage.getItem('user'));
const username = user ? user.username : null;
useEffect(() => {
const fetchArchivedNotes = async () => {
try {
const archivedNotes = await getArchivedNotes(username);
const notesWithCategories = await Promise.all(
archivedNotes.map(async (note) => {
const categories = await getNoteCategory(note.id);
return { ...note, categories };
})
);
setNotes(notesWithCategories);
} catch (error) {
console.error('Error fetching archived notes:', error);
}
};
fetchArchivedNotes();
}, [username]);
const handleDelete = async (noteId) => {
try {
await deleteNote(noteId);
setNotes((prevNotes) => prevNotes.filter((note) => note.id !== noteId));
} catch (error) {
console.error('Failed to delete note:', error);
}
};
const handleUnarchive = async (note) => {
try {
await unarchiveNote(note.id);
setNotes((prevNotes) => prevNotes.filter((n) => n.id !== note.id));
} catch (error) {
console.error('Failed to unarchive note:', error);
}
};
return (
<div className="container mx-auto p-4">
<h1 className="text-3xl font-bold my-4 text-center text-indigo-900">Archived Notes</h1>
<div className="flex flex-wrap justify-start">
{notes.length > 0 ? (
notes.map((note) => (
<NoteCard
key={note.id}
note={note}
onDelete={handleDelete}
onUnarchive={handleUnarchive}
isArchived={true}
/>
))
) : (
<p className='text-xl font-bold my-4 text-center text-indigo-900'>No archived notes found.</p>
)}
</div>
</div>
);
};
export default ArchivedNotesPage;