198 lines
6.3 KiB
TypeScript
198 lines
6.3 KiB
TypeScript
"use client"
|
|
|
|
import { useState, useEffect, use } from "react"
|
|
import { useRouter } from "next/navigation"
|
|
|
|
export default function EditWorkerPage({ params }: { params: Promise<{ id: string }> }) {
|
|
const router = useRouter()
|
|
const { id } = use(params)
|
|
const [loading, setLoading] = useState(false)
|
|
const [teams, setTeams] = useState<any[]>([])
|
|
const [formData, setFormData] = useState({
|
|
empNo: "",
|
|
firstName: "",
|
|
surname: "",
|
|
email: "",
|
|
phone: "",
|
|
jobPosition: "",
|
|
teamId: "",
|
|
status: ""
|
|
})
|
|
|
|
useEffect(() => {
|
|
fetch("/api/teams")
|
|
.then(r => r.json())
|
|
.then(setTeams)
|
|
|
|
fetch(`/api/admin/workers/${id}`)
|
|
.then(r => r.json())
|
|
.then(data => setFormData({
|
|
...data,
|
|
teamId: data.teamId || ""
|
|
}))
|
|
}, [id])
|
|
|
|
const handleSubmit = async (e: React.FormEvent) => {
|
|
e.preventDefault()
|
|
setLoading(true)
|
|
|
|
const response = await fetch(`/api/admin/workers/${id}`, {
|
|
method: "PUT",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify(formData)
|
|
})
|
|
|
|
if (response.ok) {
|
|
router.push("/admin/workers")
|
|
} else {
|
|
alert("Error updating worker")
|
|
setLoading(false)
|
|
}
|
|
}
|
|
|
|
const handleDelete = async () => {
|
|
if (!confirm("Are you sure you want to delete this worker?")) return
|
|
|
|
const response = await fetch(`/api/admin/workers/${id}`, {
|
|
method: "DELETE"
|
|
})
|
|
|
|
if (response.ok) {
|
|
router.push("/admin/workers")
|
|
} else {
|
|
alert("Error deleting worker")
|
|
}
|
|
}
|
|
|
|
return (
|
|
<div className="min-h-screen bg-gray-50 p-8">
|
|
<div className="max-w-2xl mx-auto">
|
|
<h1 className="text-3xl font-bold text-gray-800 mb-6">Edit Worker</h1>
|
|
|
|
<form onSubmit={handleSubmit} className="bg-white p-6 rounded-xl shadow-sm border border-gray-200 space-y-4">
|
|
<div>
|
|
<label className="block text-sm font-medium mb-2">Employee Number *</label>
|
|
<input
|
|
type="text"
|
|
value={formData.empNo}
|
|
onChange={(e) => setFormData({...formData, empNo: e.target.value})}
|
|
className="w-full px-4 py-2 border rounded-lg"
|
|
required
|
|
/>
|
|
</div>
|
|
|
|
<div className="grid grid-cols-2 gap-4">
|
|
<div>
|
|
<label className="block text-sm font-medium mb-2">First Name *</label>
|
|
<input
|
|
type="text"
|
|
value={formData.firstName}
|
|
onChange={(e) => setFormData({...formData, firstName: e.target.value})}
|
|
className="w-full px-4 py-2 border rounded-lg"
|
|
required
|
|
/>
|
|
</div>
|
|
<div>
|
|
<label className="block text-sm font-medium mb-2">Surname *</label>
|
|
<input
|
|
type="text"
|
|
value={formData.surname}
|
|
onChange={(e) => setFormData({...formData, surname: e.target.value})}
|
|
className="w-full px-4 py-2 border rounded-lg"
|
|
required
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
<div>
|
|
<label className="block text-sm font-medium mb-2">Email</label>
|
|
<input
|
|
type="email"
|
|
value={formData.email || ""}
|
|
onChange={(e) => setFormData({...formData, email: e.target.value})}
|
|
className="w-full px-4 py-2 border rounded-lg"
|
|
/>
|
|
</div>
|
|
|
|
<div>
|
|
<label className="block text-sm font-medium mb-2">Phone</label>
|
|
<input
|
|
type="text"
|
|
value={formData.phone || ""}
|
|
onChange={(e) => setFormData({...formData, phone: e.target.value})}
|
|
className="w-full px-4 py-2 border rounded-lg"
|
|
/>
|
|
</div>
|
|
|
|
<div>
|
|
<label className="block text-sm font-medium mb-2">Job Position *</label>
|
|
<select
|
|
value={formData.jobPosition}
|
|
onChange={(e) => setFormData({...formData, jobPosition: e.target.value})}
|
|
className="w-full px-4 py-2 border rounded-lg"
|
|
required
|
|
>
|
|
<option value="Blow Moulder Level 1">Blow Moulder Level 1 (Operator)</option>
|
|
<option value="Blow Moulder Level 2">Blow Moulder Level 2 (Supervisor)</option>
|
|
<option value="Engineer">Engineer</option>
|
|
</select>
|
|
</div>
|
|
|
|
<div>
|
|
<label className="block text-sm font-medium mb-2">Team</label>
|
|
<select
|
|
value={formData.teamId || ""}
|
|
onChange={(e) => setFormData({...formData, teamId: e.target.value})}
|
|
className="w-full px-4 py-2 border rounded-lg"
|
|
>
|
|
<option value="">No Team</option>
|
|
{teams.map((team: any) => (
|
|
<option key={team.id} value={team.id}>
|
|
{team.name}
|
|
</option>
|
|
))}
|
|
</select>
|
|
</div>
|
|
|
|
<div>
|
|
<label className="block text-sm font-medium mb-2">Status *</label>
|
|
<select
|
|
value={formData.status}
|
|
onChange={(e) => setFormData({...formData, status: e.target.value})}
|
|
className="w-full px-4 py-2 border rounded-lg"
|
|
required
|
|
>
|
|
<option value="active">Active</option>
|
|
<option value="inactive">Inactive</option>
|
|
</select>
|
|
</div>
|
|
|
|
<div className="flex gap-4 pt-4">
|
|
<button
|
|
type="submit"
|
|
disabled={loading}
|
|
className="flex-1 bg-blue-600 text-white py-3 rounded-lg font-medium hover:bg-blue-700 transition-colors disabled:opacity-50"
|
|
>
|
|
{loading ? "Updating..." : "Update Worker"}
|
|
</button>
|
|
<button
|
|
type="button"
|
|
onClick={() => router.back()}
|
|
className="px-6 py-3 border border-gray-300 rounded-lg hover:bg-gray-50"
|
|
>
|
|
Cancel
|
|
</button>
|
|
<button
|
|
type="button"
|
|
onClick={handleDelete}
|
|
className="px-6 py-3 bg-red-600 text-white rounded-lg hover:bg-red-700"
|
|
>
|
|
Delete
|
|
</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|