|
1 | 1 | "use client"; |
2 | 2 |
|
3 | 3 | import { useState, useEffect, Suspense } from "react"; |
4 | | -import { FaTrash } from "react-icons/fa"; |
| 4 | +import { FaTrash, FaEdit, FaPlus } from "react-icons/fa"; // FaEdit, FaPlusを追加 |
5 | 5 | import { CategoryItem, CosmosItem } from "@/models/models"; |
6 | 6 | import CategoryDropdown from "../../../components/documents/CategoryDropdown"; |
7 | 7 | import DocumentForm from "../../../components/documents/DocumentForm"; |
8 | 8 | import DocumentList from "../../../components/documents/DocumentList"; |
9 | 9 | import CategoryModal from "@/components/documents/CategoryModal"; |
| 10 | +import Head from "next/head"; // Headを追加 |
10 | 11 |
|
11 | 12 | export default function DocumentsPage() { |
12 | 13 | const [selectedCategory, setSelectedCategory] = useState(""); |
@@ -113,109 +114,149 @@ export default function DocumentsPage() { |
113 | 114 | }; |
114 | 115 |
|
115 | 116 | return ( |
116 | | - <main className="flex flex-col text-gray-800 w-full h-full overflow-y-auto"> |
117 | | - <h1 className="text-xl font-bold text-center">ドキュメント管理</h1> |
118 | | - <div className="flex justify-center items-center p-4 space-x-2"> |
119 | | - <Suspense fallback={<p>カテゴリを取得中...</p>}> |
120 | | - <CategoryDropdown |
121 | | - categories={categories} |
122 | | - onCategoryChange={handleCategoryChange} |
123 | | - onAddCategory={() => { |
124 | | - setEditingCategory(null); |
125 | | - setIsCategoryModalOpen(true); |
126 | | - }} |
127 | | - value={selectedCategory} // ドロップダウンの選択状態をバインド |
128 | | - /> |
129 | | - </Suspense> |
| 117 | + <> |
| 118 | + <Head> |
| 119 | + <title>ドキュメント管理</title> {/* タイトルを設定 */} |
| 120 | + </Head> |
| 121 | + <main className="flex flex-col text-gray-800 w-full h-full overflow-y-auto"> |
| 122 | + <h1 className="text-xl font-bold text-center">ドキュメント管理</h1> |
| 123 | + <div className="flex justify-center items-center p-4 space-x-2"> |
| 124 | + <Suspense fallback={<p>カテゴリを取得中...</p>}> |
| 125 | + <CategoryDropdown |
| 126 | + categories={categories} |
| 127 | + onCategoryChange={handleCategoryChange} |
| 128 | + onAddCategory={() => { |
| 129 | + setEditingCategory(null); |
| 130 | + setIsCategoryModalOpen(true); |
| 131 | + }} |
| 132 | + value={selectedCategory} // ドロップダウンの選択状態をバインド |
| 133 | + /> |
| 134 | + </Suspense> |
| 135 | + {selectedCategory && ( |
| 136 | + <div className="flex items-center space-x-2"> |
| 137 | + <button |
| 138 | + className="bg-yellow-500 text-white p-2 rounded-full" |
| 139 | + onClick={() => { |
| 140 | + const categoryToEdit = categories.find((cat) => cat.id === selectedCategory); |
| 141 | + setEditingCategory(categoryToEdit || null); |
| 142 | + setIsCategoryModalOpen(true); |
| 143 | + }} |
| 144 | + > |
| 145 | + <FaEdit /> |
| 146 | + </button> |
| 147 | + <button |
| 148 | + className="bg-blue-500 text-white p-2 rounded-full" |
| 149 | + onClick={() => { |
| 150 | + setEditingCategory(null); |
| 151 | + setIsCategoryModalOpen(true); |
| 152 | + }} |
| 153 | + > |
| 154 | + <FaPlus /> |
| 155 | + </button> |
| 156 | + <button |
| 157 | + className="bg-red-500 text-white p-2 rounded-full" |
| 158 | + onClick={() => handleDeleteCategory(selectedCategory)} |
| 159 | + > |
| 160 | + <FaTrash /> |
| 161 | + </button> |
| 162 | + </div> |
| 163 | + )} |
| 164 | + </div> |
130 | 165 | {selectedCategory && ( |
131 | | - <button |
132 | | - className="bg-red-500 text-white p-2 rounded-full" |
133 | | - onClick={() => handleDeleteCategory(selectedCategory)} |
134 | | - > |
135 | | - <FaTrash /> |
136 | | - </button> |
137 | | - )} |
138 | | - </div> |
139 | | - {selectedCategory && ( |
140 | | - <div className="flex flex-col md:flex-row flex-1"> |
141 | | - <DocumentForm |
142 | | - text={text} |
143 | | - setText={setText} |
144 | | - isSubmitting={isSubmitting} |
145 | | - onSubmit={async () => { |
146 | | - if (!text.trim()) return alert("テキストを入力してください。"); |
147 | | - if (!selectedCategory) return alert("カテゴリを選択してください。"); |
148 | | - |
149 | | - setIsSubmitting(true); |
150 | | - try { |
151 | | - const method = editingDocument ? "PATCH" : "POST"; |
152 | | - const url = `/api/document`; |
153 | | - |
154 | | - const payload = { |
155 | | - category_id: selectedCategory, |
156 | | - content: text, |
157 | | - }; |
158 | | - |
159 | | - const response = await fetch(url, { |
160 | | - method, |
161 | | - headers: { "Content-Type": "application/json" }, |
162 | | - body: JSON.stringify(payload), |
163 | | - }); |
164 | | - |
165 | | - if (response.ok) { |
166 | | - alert(editingDocument ? "更新が成功しました!" : "登録が成功しました!"); |
167 | | - setText(""); |
168 | | - setEditingDocument(null); |
169 | | - fetchDocuments(selectedCategory); |
170 | | - } else { |
171 | | - alert(editingDocument ? "更新に失敗しました。" : "登録に失敗しました。"); |
| 166 | + <div className="flex flex-col md:flex-row flex-1"> |
| 167 | + <DocumentForm |
| 168 | + text={text} |
| 169 | + setText={setText} |
| 170 | + isSubmitting={isSubmitting} |
| 171 | + onSubmit={async () => { |
| 172 | + if (!text.trim()) { |
| 173 | + alert("テキストを入力してください。"); |
| 174 | + return; |
172 | 175 | } |
173 | | - } catch (error) { |
174 | | - console.error("エラー:", error); |
175 | | - alert("エラーが発生しました。"); |
176 | | - } finally { |
177 | | - setIsSubmitting(false); |
178 | | - } |
179 | | - }} |
180 | | - editingDocument={editingDocument} |
181 | | - /> |
182 | | - <DocumentList |
183 | | - documents={documents} |
184 | | - onEdit={(doc) => { |
185 | | - setEditingDocument(doc); |
186 | | - setText(doc.content); |
187 | | - }} |
188 | | - onDelete={async (docId) => { |
189 | | - if (!confirm("本当にこのドキュメントを削除しますか?")) return; |
190 | | - |
191 | | - try { |
192 | | - const response = await fetch(`/api/document`, { |
193 | | - method: "DELETE", |
194 | | - headers: { "Content-Type": "application/json" }, |
195 | | - body: JSON.stringify({ id: docId }), |
196 | | - }); |
197 | | - |
198 | | - if (response.ok) { |
199 | | - alert("削除が成功しました!"); |
200 | | - fetchDocuments(selectedCategory); |
201 | | - } else { |
202 | | - const errorData = await response.json(); |
203 | | - alert(`削除に失敗しました: ${errorData.message}`); |
| 176 | + if (!selectedCategory) { |
| 177 | + alert("カテゴリを選択してください。"); |
| 178 | + return; |
204 | 179 | } |
205 | | - } catch (error) { |
206 | | - console.error("エラー:", error); |
207 | | - alert("エラーが発生しました。"); |
208 | | - } |
209 | | - }} |
210 | | - /> |
211 | | - </div> |
212 | | - )} |
213 | | - <CategoryModal |
214 | | - isOpen={isCategoryModalOpen} |
215 | | - onClose={() => setIsCategoryModalOpen(false)} |
216 | | - onSubmit={handleCategorySubmit} |
217 | | - initialCategory={editingCategory?.category} |
218 | | - /> |
219 | | - </main> |
| 180 | + |
| 181 | + setIsSubmitting(true); |
| 182 | + try { |
| 183 | + const method = editingDocument ? "PATCH" : "POST"; |
| 184 | + const url = `/api/document`; |
| 185 | + |
| 186 | + const payload = { |
| 187 | + id: editingDocument?.id, // 更新時はIDを含める |
| 188 | + category_id: selectedCategory, |
| 189 | + content: text, |
| 190 | + }; |
| 191 | + |
| 192 | + const response = await fetch(url, { |
| 193 | + method, |
| 194 | + headers: { "Content-Type": "application/json" }, |
| 195 | + body: JSON.stringify(payload), |
| 196 | + }); |
| 197 | + |
| 198 | + if (response.ok) { |
| 199 | + const message = editingDocument ? "更新が成功しました!" : "登録が成功しました!"; |
| 200 | + alert(message); |
| 201 | + setText(""); |
| 202 | + setEditingDocument(null); |
| 203 | + fetchDocuments(selectedCategory); |
| 204 | + } else { |
| 205 | + const errorData = await response.json(); |
| 206 | + const message = editingDocument ? "更新に失敗しました。" : "登録に失敗しました。"; |
| 207 | + alert(`${message} エラー: ${errorData.message}`); |
| 208 | + } |
| 209 | + } catch (error) { |
| 210 | + console.error("エラー:", error); |
| 211 | + alert("エラーが発生しました。"); |
| 212 | + } finally { |
| 213 | + setIsSubmitting(false); |
| 214 | + } |
| 215 | + }} |
| 216 | + editingDocument={editingDocument} |
| 217 | + /> |
| 218 | + <DocumentList |
| 219 | + documents={documents} |
| 220 | + onEdit={(doc) => { |
| 221 | + setEditingDocument(doc); |
| 222 | + setText(doc.content); |
| 223 | + }} |
| 224 | + onDelete={async (docId) => { |
| 225 | + if (!confirm("本当にこのドキュメントを削除しますか?")) return; |
| 226 | + |
| 227 | + try { |
| 228 | + const response = await fetch(`/api/document`, { |
| 229 | + method: "DELETE", |
| 230 | + headers: { "Content-Type": "application/json" }, |
| 231 | + body: JSON.stringify({ id: docId }), |
| 232 | + }); |
| 233 | + |
| 234 | + if (response.ok) { |
| 235 | + alert("削除が成功しました!"); |
| 236 | + fetchDocuments(selectedCategory); |
| 237 | + } else { |
| 238 | + const errorData = await response.json(); |
| 239 | + alert(`削除に失敗しました: ${errorData.message}`); |
| 240 | + } |
| 241 | + } catch (error) { |
| 242 | + console.error("エラー:", error); |
| 243 | + alert("エラーが発生しました。"); |
| 244 | + } |
| 245 | + }} |
| 246 | + onAdd={() => { |
| 247 | + setEditingDocument(null); // 新規登録モードに切り替え |
| 248 | + setText(""); // フォームをクリア |
| 249 | + }} |
| 250 | + /> |
| 251 | + </div> |
| 252 | + )} |
| 253 | + <CategoryModal |
| 254 | + isOpen={isCategoryModalOpen} |
| 255 | + onClose={() => setIsCategoryModalOpen(false)} |
| 256 | + onSubmit={handleCategorySubmit} |
| 257 | + initialCategory={editingCategory?.category} |
| 258 | + /> |
| 259 | + </main> |
| 260 | + </> |
220 | 261 | ); |
221 | 262 | } |
0 commit comments