import React, {useState, useEffect} from "react"; import { useNavigate, useParams } from "react-router-dom"; import { AuthService, ResourceService } from "../../services"; import { Archive } from "react-bootstrap-icons"; import { Spinner, Breadcrumb, BreadcrumbItem, Tabs, Tab, Modal, Button } from "react-bootstrap"; import { RESOURCE_TYPE_OPTIONS, RESOURCE_SPECIALTY_OPTIONS } from "../../shared/constants"; const UpdateResource = () => { const navigate = useNavigate(); const urlParams = useParams(); const [currentResource, setCurrentResource] = useState(undefined); // Basic Information const [name, setName] = useState(''); // Provider name const [officeName, setOfficeName] = useState(''); const [specialty, setSpecialty] = useState(''); const [type, setType] = useState(''); const [typeOther, setTypeOther] = useState(''); // Contact Information const [phone, setPhone] = useState(''); // Office Phone Number const [contact, setContact] = useState(''); // Secondary Phone Number const [fax, setFax] = useState(''); // Fax Number const [email, setEmail] = useState(''); // Address (split fields) const [addressLine1, setAddressLine1] = useState(''); const [addressLine2, setAddressLine2] = useState(''); const [city, setCity] = useState(''); const [state, setState] = useState(''); const [zipcode, setZipcode] = useState(''); // Additional Information const [note, setNote] = useState(''); // Modal const [showDeleteModal, setShowDeleteModal] = useState(false); const redirectTo = () => { navigate(`/medical/resources/list`); } const redirectToView = () => { navigate(`/medical/resources/${urlParams.id}`); } const validateResource = () => { const errors = []; // Required fields validation if (!name || name.trim() === '') { errors.push('Provider'); } if (!phone || phone.trim() === '') { errors.push('Office Phone Number'); } if (type === 'other' && (!typeOther || typeOther.trim() === '')) { errors.push('Other-please specify'); } if (errors.length > 0) { window.alert(`Please fill in the following required fields:\n${errors.join('\n')}`); return false; } return true; }; const saveResource = () => { if (!validateResource()) { return; } const combinedAddress = `${addressLine1}${addressLine2 ? ', ' + addressLine2 : ''}, ${city}, ${state} ${zipcode}`.trim(); const updatedResource = { ...currentResource, data: currentResource?.data || {}, // Basic Information name, // Provider office_name: officeName, name_original: officeName, // Legacy field specialty, type, type_other: type === 'other' ? typeOther : '', // Contact Information phone, // Office Phone Number contact, // Secondary Phone Number fax, // Fax Number email, // Address (split fields) address_line_1: addressLine1, address_line_2: addressLine2, city, state, zipcode, address: combinedAddress, // Legacy field // Additional Information note, // System fields edit_by: localStorage.getItem('user') && JSON.parse(localStorage.getItem('user'))?.name, edit_date: new Date(), edit_history: currentResource?.edit_history ? [...currentResource.edit_history, { employee: localStorage.getItem('user') && JSON.parse(localStorage.getItem('user'))?.name, date: new Date() }] : [{ employee: localStorage.getItem('user') && JSON.parse(localStorage.getItem('user'))?.name, date: new Date() }] }; ResourceService.updateResource(urlParams.id, updatedResource).then(() => redirectToView()); }; const triggerShowDeleteModal = () => { setShowDeleteModal(true); } const closeDeleteModal = () => { setShowDeleteModal(false); } const deactivateResource = () => { ResourceService.disableResource(urlParams.id, { status: 'inactive', edit_by: localStorage.getItem('user') && JSON.parse(localStorage.getItem('user'))?.name, edit_date: new Date() }).then(() => { setShowDeleteModal(false); redirectTo(); }); } useEffect(() => { if (!AuthService.canViewMedicalSection()) { window.alert('You haven\'t login yet OR this user does not have access to this page. Please change an admin account to login.') AuthService.logout(); navigate(`/login`); } ResourceService.getResource(urlParams.id).then(resourceData => { setCurrentResource(resourceData.data); }); }, []); useEffect(() => { if (currentResource) { // Basic Information setName(currentResource?.name || ''); setOfficeName(currentResource?.office_name || currentResource?.name_original || ''); setSpecialty(currentResource?.specialty || ''); setType(currentResource?.type || ''); setTypeOther(currentResource?.type_other || ''); // Contact Information setPhone(currentResource?.phone || ''); setContact(currentResource?.contact || ''); setFax(currentResource?.fax || ''); setEmail(currentResource?.email || ''); // Address - try new fields first, then fall back to legacy setAddressLine1(currentResource?.address_line_1 || currentResource?.address || ''); setAddressLine2(currentResource?.address_line_2 || ''); setCity(currentResource?.city || ''); setState(currentResource?.state || ''); setZipcode(currentResource?.zipcode || ''); // Additional Information - merge description into note if note is empty setNote(currentResource?.note || currentResource?.description || ''); } }, [currentResource]); return ( <>
Medical Provider Information Update Provider Information

Update Provider Information

Basic Information
Provider *
setName(e.target.value)}/>
Office Name
setOfficeName(e.target.value)}/>
Type
{type === 'other' &&
Other-please specify
setTypeOther(e.target.value)} />
}
Specialty
Contact Information
Office Phone Number *
setPhone(e.target.value)}/>
Secondary Phone Number
setContact(e.target.value)}/>
Fax Number
setFax(e.target.value)}/>
Email
setEmail(e.target.value)}/>
Provider Address
Address Line 1 *
setAddressLine1(e.target.value)}/>
Address Line 2
setAddressLine2(e.target.value)}/>
City *
setCity(e.target.value)}/>
State *
setState(e.target.value)}/>
Zip Code *
setZipcode(e.target.value)}/>
Additional Information
Note