-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditNotePage.js
More file actions
128 lines (114 loc) · 4.58 KB
/
EditNotePage.js
File metadata and controls
128 lines (114 loc) · 4.58 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import React, { useState, useEffect } from 'react';
import { useParams, useNavigate } from 'react-router-dom';
import { getNoteById, updateNote, getCategories, assignCategoryToNote, removeCategoryFromNote } from '../api';
const EditNotePage = () => {
const { noteId } = useParams();
const navigate = useNavigate();
const [title, setTitle] = useState('');
const [content, setContent] = useState('');
const [categories, setCategories] = useState([]);
const [selectedCategoryIds, setSelectedCategoryIds] = useState([]);
const [currentCategoryIds, setCurrentCategoryIds] = useState([]);
useEffect(() => {
const fetchNoteAndCategories = async () => {
try {
const note = await getNoteById(noteId);
setTitle(note.title);
setContent(note.content);
setSelectedCategoryIds(note.categories ? note.categories.map(cat => cat.id) : []);
setCurrentCategoryIds(note.categories ? note.categories.map(cat => cat.id) : []);
const fetchedCategories = await getCategories();
setCategories(fetchedCategories);
} catch (error) {
console.error('Failed to fetch note or categories:', error);
}
};
fetchNoteAndCategories();
}, [noteId]);
const handleUpdateNote = async (e) => {
e.preventDefault();
const updatedNote = {
id: noteId,
title,
content,
modificationDate: new Date().toISOString(),
archived: false,
user: {
id: JSON.parse(localStorage.getItem('user')).id,
username: JSON.parse(localStorage.getItem('user')).username,
},
};
try {
await updateNote(updatedNote);
// Update categories
const newCategoryIds = selectedCategoryIds;
// Remove categories that are no longer selected
const categoriesToRemove = currentCategoryIds.filter(id => !newCategoryIds.includes(id));
await Promise.all(categoriesToRemove.map(categoryId => removeCategoryFromNote(noteId, categoryId)));
// Add newly selected categories
const categoriesToAdd = newCategoryIds.filter(id => !currentCategoryIds.includes(id));
await Promise.all(categoriesToAdd.map(categoryId => assignCategoryToNote(noteId, categoryId)));
navigate('/');
} catch (error) {
console.error('Error updating note:', error);
alert('Failed to update note.');
}
};
const toggleCategory = (categoryId) => {
setSelectedCategoryIds(prev => {
if (prev.includes(categoryId)) {
return prev.filter(id => id !== categoryId);
} else {
return [...prev, categoryId];
}
});
};
return (
<div className="container mx-auto p-4">
<h2 className="text-2xl font-bold mb-4 text-indigo-900">Edit Note</h2>
<form onSubmit={handleUpdateNote}>
<div className="mb-4">
<label htmlFor="title" className="block text-gray-700 text-sm font-bold mb-2">Title:</label>
<input
type="text"
id="title"
className="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline"
value={title}
onChange={(e) => setTitle(e.target.value)}
required
/>
</div>
<div className="mb-4">
<label htmlFor="content" className="block text-gray-700 text-sm font-bold mb-2">Content:</label>
<textarea
id="content"
className="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline"
rows="4"
value={content}
onChange={(e) => setContent(e.target.value)}
required
/>
</div>
<div className="mb-4">
<label className="block text-gray-700 text-sm font-bold mb-2">Categories:</label>
<div>
{categories.map(category => (
<button
key={category.id}
type="button"
onClick={() => toggleCategory(category.id)}
className={`inline-block rounded-full px-3 py-1 text-sm font-semibold text-gray-700 mr-2 mb-2 ${selectedCategoryIds.includes(category.id) ? 'bg-gradient-to-br from-purple-600 to-blue-500 text-white' : 'bg-gray-200'}`}
>
{category.name}
</button>
))}
</div>
</div>
<button type="submit" className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline">
Update Note
</button>
</form>
</div>
);
};
export default EditNotePage;