diff --git a/frontend/src/components/admin/UserModal.jsx b/frontend/src/components/admin/UserModal.jsx index 0219d688..9e28b5df 100644 --- a/frontend/src/components/admin/UserModal.jsx +++ b/frontend/src/components/admin/UserModal.jsx @@ -28,15 +28,30 @@ const UserModal = ({ isOpen, onClose, onSave, initialData = null, clinics = [] } useEffect(() => { if (isOpen) { if (initialData) { - // Om vi redigerar en veterinär, se till att fälten från vetRecord följer med in i formData + const role = (initialData.role || 'OWNER').replace('ROLE_', ''); setFormData({ ...initialData, + role, password: '', - licenseId: initialData.vetRecord?.licenseId || '', - specialization: initialData.vetRecord?.specialization || '', - bookingInfo: initialData.vetRecord?.bookingInfo || '', - clinicId: initialData.vetRecord?.clinicId || '' + clinicId: initialData.clinicId || '', + licenseId: '', + specialization: '', + bookingInfo: '', }); + + if (role === 'VET') { + vetService.getAll().then(res => { + const vetRecord = res.data.find(v => v.userId === initialData.id); + if (vetRecord) { + setFormData(prev => ({ + ...prev, + licenseId: vetRecord.licenseId || '', + specialization: vetRecord.specialization || '', + bookingInfo: vetRecord.bookingInfo || '', + })); + } + }).catch(err => console.error('Kunde inte hämta veterinärdetaljer:', err)); + } } else { setFormData({ name: '', email: '', password: '', @@ -70,12 +85,17 @@ const UserModal = ({ isOpen, onClose, onSave, initialData = null, clinics = [] } bookingInfo: formData.bookingInfo }; - // CodeRabbit Fix: Kolla om det redan finns en veterinärpost (Edit vs Create) - const existingVetId = initialData?.vetRecord?.id; - - if (existingVetId) { - await vetService.update(existingVetId, vetPayload); - } else if (userId) { + if (initialData) { + try { + await vetService.update(userId, vetPayload); + } catch (err) { + if (err.response?.status === 404) { + await vetService.create(vetPayload); + } else { + throw err; + } + } + } else { await vetService.create(vetPayload); } } @@ -211,7 +231,7 @@ const UserModal = ({ isOpen, onClose, onSave, initialData = null, clinics = [] } {/* VETERINÄR-DETALJER */} {isVet && (
-
+