Day 9 完了
実装内容:
1. backend/apps/fields/views.py - FieldViewSetをModelViewSetに変更(書き込み可能)
2. frontend/src/components/Navbar.tsx - 圃場管理リンク追加
3. frontend/src/app/fields/page.tsx - 圃場一覧画面
4. frontend/src/app/fields/new/page.tsx - 新規作成画面
5. frontend/src/app/fields/[id]/page.tsx - 編集画面
API CRUDテスト結果:
- POST /api/fields/ → 201 Created
- GET /api/fields/ → 200 OK
- PATCH /api/fields/{id}/ → 200 OK
- DELETE /api/fields/{id}/ → 204 No Content
ブラウザで http://localhost:3000/fields から圃場のCRUD操作が可能です。
次の工程に移りますか?
This commit is contained in:
240
frontend/src/app/fields/[id]/page.tsx
Normal file
240
frontend/src/app/fields/[id]/page.tsx
Normal file
@@ -0,0 +1,240 @@
|
||||
'use client';
|
||||
|
||||
import { useState, useEffect } from 'react';
|
||||
import { useRouter, useParams } from 'next/navigation';
|
||||
import { api } from '@/lib/api';
|
||||
import { Field } from '@/types';
|
||||
import Navbar from '@/components/Navbar';
|
||||
import { ArrowLeft, Save } from 'lucide-react';
|
||||
|
||||
export default function EditFieldPage() {
|
||||
const router = useRouter();
|
||||
const params = useParams();
|
||||
const fieldId = parseInt(params.id as string);
|
||||
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [saving, setSaving] = useState(false);
|
||||
const [error, setError] = useState('');
|
||||
const [notFound, setNotFound] = useState(false);
|
||||
|
||||
const [formData, setFormData] = useState({
|
||||
name: '',
|
||||
address: '',
|
||||
area_tan: '',
|
||||
area_m2: '',
|
||||
owner_name: '',
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
fetchField();
|
||||
}, [fieldId]);
|
||||
|
||||
const fetchField = async () => {
|
||||
try {
|
||||
const response = await api.get(`/fields/${fieldId}/`);
|
||||
const field: Field = response.data;
|
||||
setFormData({
|
||||
name: field.name || '',
|
||||
address: field.address || '',
|
||||
area_tan: field.area_tan?.toString() || '',
|
||||
area_m2: field.area_m2?.toString() || '',
|
||||
owner_name: field.owner_name || '',
|
||||
});
|
||||
} catch (err: unknown) {
|
||||
console.error('Failed to fetch field:', err);
|
||||
const axiosError = err as { response?: { status?: number } };
|
||||
if (axiosError.response?.status === 404) {
|
||||
setNotFound(true);
|
||||
}
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
const { name, value } = e.target;
|
||||
setFormData((prev) => ({ ...prev, [name]: value }));
|
||||
};
|
||||
|
||||
const handleSubmit = async (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
setError('');
|
||||
setSaving(true);
|
||||
|
||||
try {
|
||||
const data = {
|
||||
name: formData.name,
|
||||
address: formData.address || null,
|
||||
area_tan: formData.area_tan ? parseFloat(formData.area_tan) : null,
|
||||
area_m2: formData.area_m2 ? parseInt(formData.area_m2) : null,
|
||||
owner_name: formData.owner_name || null,
|
||||
};
|
||||
|
||||
await api.patch(`/fields/${fieldId}/`, data);
|
||||
router.push('/fields');
|
||||
} catch (err: unknown) {
|
||||
console.error('Failed to update field:', err);
|
||||
setError('保存に失敗しました');
|
||||
} finally {
|
||||
setSaving(false);
|
||||
}
|
||||
};
|
||||
|
||||
if (loading) {
|
||||
return (
|
||||
<div className="min-h-screen bg-gray-50">
|
||||
<Navbar />
|
||||
<div className="flex items-center justify-center h-64">
|
||||
<div className="text-gray-500">読み込み中...</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
if (notFound) {
|
||||
return (
|
||||
<div className="min-h-screen bg-gray-50">
|
||||
<Navbar />
|
||||
<div className="max-w-2xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
|
||||
<div className="bg-white rounded-lg shadow p-8 text-center">
|
||||
<p className="text-gray-500">圃場が見つかりません。</p>
|
||||
<button
|
||||
onClick={() => router.push('/fields')}
|
||||
className="mt-4 text-green-600 hover:text-green-700"
|
||||
>
|
||||
一覧に戻る
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="min-h-screen bg-gray-50">
|
||||
<Navbar />
|
||||
<div className="max-w-2xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
|
||||
<div className="mb-6">
|
||||
<button
|
||||
onClick={() => router.push('/fields')}
|
||||
className="flex items-center text-gray-600 hover:text-gray-900"
|
||||
>
|
||||
<ArrowLeft className="h-4 w-4 mr-2" />
|
||||
一覧に戻る
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div className="bg-white rounded-lg shadow p-6">
|
||||
<h1 className="text-2xl font-bold text-gray-900 mb-6">圃場編集</h1>
|
||||
|
||||
{error && (
|
||||
<div className="mb-4 p-3 text-sm text-red-600 bg-red-50 rounded-md">
|
||||
{error}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<form onSubmit={handleSubmit} className="space-y-4">
|
||||
<div>
|
||||
<label htmlFor="name" className="block text-sm font-medium text-gray-700 mb-1">
|
||||
圃場名 <span className="text-red-500">*</span>
|
||||
</label>
|
||||
<input
|
||||
type="text"
|
||||
id="name"
|
||||
name="name"
|
||||
value={formData.name}
|
||||
onChange={handleChange}
|
||||
required
|
||||
className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-green-500"
|
||||
placeholder="例:A-1圃場"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label htmlFor="address" className="block text-sm font-medium text-gray-700 mb-1">
|
||||
住所
|
||||
</label>
|
||||
<input
|
||||
type="text"
|
||||
id="address"
|
||||
name="address"
|
||||
value={formData.address}
|
||||
onChange={handleChange}
|
||||
className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-green-500"
|
||||
placeholder="例:山形県鶴岡市..."
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-2 gap-4">
|
||||
<div>
|
||||
<label htmlFor="area_tan" className="block text-sm font-medium text-gray-700 mb-1">
|
||||
面積(反)
|
||||
</label>
|
||||
<input
|
||||
type="number"
|
||||
step="0.0001"
|
||||
id="area_tan"
|
||||
name="area_tan"
|
||||
value={formData.area_tan}
|
||||
onChange={handleChange}
|
||||
className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-green-500"
|
||||
placeholder="例:1.5"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label htmlFor="area_m2" className="block text-sm font-medium text-gray-700 mb-1">
|
||||
面積(m2)
|
||||
</label>
|
||||
<input
|
||||
type="number"
|
||||
id="area_m2"
|
||||
name="area_m2"
|
||||
value={formData.area_m2}
|
||||
onChange={handleChange}
|
||||
className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-green-500"
|
||||
placeholder="例:1500"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label htmlFor="owner_name" className="block text-sm font-medium text-gray-700 mb-1">
|
||||
所有者名
|
||||
</label>
|
||||
<input
|
||||
type="text"
|
||||
id="owner_name"
|
||||
name="owner_name"
|
||||
value={formData.owner_name}
|
||||
onChange={handleChange}
|
||||
className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-green-500"
|
||||
placeholder="例:山田太郎"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="pt-4">
|
||||
<button
|
||||
type="submit"
|
||||
disabled={saving}
|
||||
className="w-full flex items-center justify-center px-4 py-2 bg-green-600 text-white rounded-md hover:bg-green-700 disabled:opacity-50 disabled:cursor-not-allowed transition-colors"
|
||||
>
|
||||
{saving ? (
|
||||
<>
|
||||
<div className="animate-spin h-4 w-4 border-2 border-white border-t-transparent rounded-full mr-2" />
|
||||
保存中...
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<Save className="h-4 w-4 mr-2" />
|
||||
保存
|
||||
</>
|
||||
)}
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user