| 1 | import React, { useState, useEffect } from 'react';
|
|---|
| 2 | import { useNavigate, useParams } from 'react-router-dom';
|
|---|
| 3 | import { doctorService } from '../../services/doctorService';
|
|---|
| 4 | import ErrorAlert from '../../components/ErrorAlert';
|
|---|
| 5 | import SuccessAlert from '../../components/SuccessAlert';
|
|---|
| 6 |
|
|---|
| 7 | function DoctorForm() {
|
|---|
| 8 | const { id } = useParams();
|
|---|
| 9 | const navigate = useNavigate();
|
|---|
| 10 | const [formData, setFormData] = useState({
|
|---|
| 11 | firstName: '',
|
|---|
| 12 | lastName: '',
|
|---|
| 13 | emailAddress: '',
|
|---|
| 14 | levelId: '',
|
|---|
| 15 | specializationId: '',
|
|---|
| 16 | departmentId: '',
|
|---|
| 17 | });
|
|---|
| 18 | const [error, setError] = useState(null);
|
|---|
| 19 | const [success, setSuccess] = useState(null);
|
|---|
| 20 | const [loading, setLoading] = useState(false);
|
|---|
| 21 |
|
|---|
| 22 | useEffect(() => {
|
|---|
| 23 | if (id) {
|
|---|
| 24 | fetchDoctor();
|
|---|
| 25 | }
|
|---|
| 26 | }, [id]);
|
|---|
| 27 |
|
|---|
| 28 | const fetchDoctor = async () => {
|
|---|
| 29 | try {
|
|---|
| 30 | setLoading(true);
|
|---|
| 31 | const response = await doctorService.getDoctorById(id);
|
|---|
| 32 | setFormData({
|
|---|
| 33 | firstName: response.data.firstName,
|
|---|
| 34 | lastName: response.data.lastName,
|
|---|
| 35 | emailAddress: response.data.emailAddress,
|
|---|
| 36 | levelId: response.data.level?.levelId || '',
|
|---|
| 37 | specializationId: response.data.specialization?.specializationId || '',
|
|---|
| 38 | departmentId: response.data.department?.departmentId || '',
|
|---|
| 39 | });
|
|---|
| 40 | } catch (err) {
|
|---|
| 41 | setError('Failed to fetch doctor');
|
|---|
| 42 | } finally {
|
|---|
| 43 | setLoading(false);
|
|---|
| 44 | }
|
|---|
| 45 | };
|
|---|
| 46 |
|
|---|
| 47 | const handleChange = (e) => {
|
|---|
| 48 | const { name, value } = e.target;
|
|---|
| 49 | setFormData(prev => ({
|
|---|
| 50 | ...prev,
|
|---|
| 51 | [name]: value
|
|---|
| 52 | }));
|
|---|
| 53 | };
|
|---|
| 54 |
|
|---|
| 55 | const handleSubmit = async (e) => {
|
|---|
| 56 | e.preventDefault();
|
|---|
| 57 |
|
|---|
| 58 | if (!formData.firstName || !formData.lastName || !formData.emailAddress) {
|
|---|
| 59 | setError('Please fill in all required fields');
|
|---|
| 60 | return;
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | try {
|
|---|
| 64 | setLoading(true);
|
|---|
| 65 | if (id) {
|
|---|
| 66 | await doctorService.updateDoctor(id, formData);
|
|---|
| 67 | setSuccess('Doctor updated successfully!');
|
|---|
| 68 | } else {
|
|---|
| 69 | await doctorService.createDoctor(formData);
|
|---|
| 70 | setSuccess('Doctor created successfully!');
|
|---|
| 71 | }
|
|---|
| 72 | setTimeout(() => navigate('/doctors'), 1500);
|
|---|
| 73 | } catch (err) {
|
|---|
| 74 | setError(err.response?.data?.error || 'Failed to save doctor');
|
|---|
| 75 | } finally {
|
|---|
| 76 | setLoading(false);
|
|---|
| 77 | }
|
|---|
| 78 | };
|
|---|
| 79 |
|
|---|
| 80 | return (
|
|---|
| 81 | <div className="max-w-2xl mx-auto">
|
|---|
| 82 | <h1 className="text-3xl font-bold mb-6">
|
|---|
| 83 | {id ? 'Edit Doctor' : 'Add New Doctor'}
|
|---|
| 84 | </h1>
|
|---|
| 85 |
|
|---|
| 86 | {error && <ErrorAlert message={error} onClose={() => setError(null)} />}
|
|---|
| 87 | {success && <SuccessAlert message={success} onClose={() => setSuccess(null)} />}
|
|---|
| 88 |
|
|---|
| 89 | <form onSubmit={handleSubmit} className="bg-white rounded-lg shadow p-6 space-y-4">
|
|---|
| 90 | <div className="grid grid-cols-2 gap-4">
|
|---|
| 91 | <div>
|
|---|
| 92 | <label className="block text-sm font-semibold mb-2">First Name *</label>
|
|---|
| 93 | <input
|
|---|
| 94 | type="text"
|
|---|
| 95 | name="firstName"
|
|---|
| 96 | value={formData.firstName}
|
|---|
| 97 | onChange={handleChange}
|
|---|
| 98 | className="w-full px-4 py-2 border rounded-lg"
|
|---|
| 99 | required
|
|---|
| 100 | />
|
|---|
| 101 | </div>
|
|---|
| 102 | <div>
|
|---|
| 103 | <label className="block text-sm font-semibold mb-2">Last Name *</label>
|
|---|
| 104 | <input
|
|---|
| 105 | type="text"
|
|---|
| 106 | name="lastName"
|
|---|
| 107 | value={formData.lastName}
|
|---|
| 108 | onChange={handleChange}
|
|---|
| 109 | className="w-full px-4 py-2 border rounded-lg"
|
|---|
| 110 | required
|
|---|
| 111 | />
|
|---|
| 112 | </div>
|
|---|
| 113 | </div>
|
|---|
| 114 |
|
|---|
| 115 | <div>
|
|---|
| 116 | <label className="block text-sm font-semibold mb-2">Email *</label>
|
|---|
| 117 | <input
|
|---|
| 118 | type="email"
|
|---|
| 119 | name="emailAddress"
|
|---|
| 120 | value={formData.emailAddress}
|
|---|
| 121 | onChange={handleChange}
|
|---|
| 122 | className="w-full px-4 py-2 border rounded-lg"
|
|---|
| 123 | required
|
|---|
| 124 | />
|
|---|
| 125 | </div>
|
|---|
| 126 |
|
|---|
| 127 | <div className="grid grid-cols-3 gap-4">
|
|---|
| 128 | <div>
|
|---|
| 129 | <label className="block text-sm font-semibold mb-2">Level</label>
|
|---|
| 130 | <input
|
|---|
| 131 | type="number"
|
|---|
| 132 | name="levelId"
|
|---|
| 133 | value={formData.levelId}
|
|---|
| 134 | onChange={handleChange}
|
|---|
| 135 | placeholder="Level ID"
|
|---|
| 136 | className="w-full px-4 py-2 border rounded-lg"
|
|---|
| 137 | />
|
|---|
| 138 | </div>
|
|---|
| 139 | <div>
|
|---|
| 140 | <label className="block text-sm font-semibold mb-2">Specialization</label>
|
|---|
| 141 | <input
|
|---|
| 142 | type="number"
|
|---|
| 143 | name="specializationId"
|
|---|
| 144 | value={formData.specializationId}
|
|---|
| 145 | onChange={handleChange}
|
|---|
| 146 | placeholder="Spec. ID"
|
|---|
| 147 | className="w-full px-4 py-2 border rounded-lg"
|
|---|
| 148 | />
|
|---|
| 149 | </div>
|
|---|
| 150 | <div>
|
|---|
| 151 | <label className="block text-sm font-semibold mb-2">Department</label>
|
|---|
| 152 | <input
|
|---|
| 153 | type="number"
|
|---|
| 154 | name="departmentId"
|
|---|
| 155 | value={formData.departmentId}
|
|---|
| 156 | onChange={handleChange}
|
|---|
| 157 | placeholder="Dept. ID"
|
|---|
| 158 | className="w-full px-4 py-2 border rounded-lg"
|
|---|
| 159 | />
|
|---|
| 160 | </div>
|
|---|
| 161 | </div>
|
|---|
| 162 |
|
|---|
| 163 | <div className="flex gap-4 pt-4">
|
|---|
| 164 | <button
|
|---|
| 165 | type="submit"
|
|---|
| 166 | disabled={loading}
|
|---|
| 167 | className="bg-purple-600 text-white px-6 py-2 rounded hover:bg-purple-700 disabled:opacity-50"
|
|---|
| 168 | >
|
|---|
| 169 | {loading ? 'Saving...' : 'Save Doctor'}
|
|---|
| 170 | </button>
|
|---|
| 171 | <button
|
|---|
| 172 | type="button"
|
|---|
| 173 | onClick={() => navigate('/doctors')}
|
|---|
| 174 | className="bg-gray-300 text-gray-700 px-6 py-2 rounded hover:bg-gray-400"
|
|---|
| 175 | >
|
|---|
| 176 | Cancel
|
|---|
| 177 | </button>
|
|---|
| 178 | </div>
|
|---|
| 179 | </form>
|
|---|
| 180 | </div>
|
|---|
| 181 | );
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | export default DoctorForm;
|
|---|