import React, { useState } from 'react'; import styles from './PublishForm.module.css'; const PublishForm = ({ isAdmin = false, formData, onSubmit, onCancel, onApprove, onDeny }) => { const [state, setState] = useState(isAdmin ? 'viewing' : 'writing'); const [form, setForm] = useState( formData || { id: -1, name: '', lastName: '', mapName: '', mapType: 'Other', googleMapsUrl: '', } ); const [errors, setErrors] = useState({}); const mapTypeOptions = ['Hospital', 'Faculty', 'House', 'Other']; const handleChange = (e) => { const { name, value } = e.target; setForm((prev) => ({ ...prev, [name]: value })); setErrors((prev) => ({ ...prev, [name]: '' })); }; const validateForm = () => { const newErrors = {}; if (!form.name.trim()) newErrors.name = 'Name is required.'; if (!form.lastName.trim()) newErrors.lastName = 'Last Name is required.'; if (!form.googleMapsUrl.trim()) newErrors.googleMapsUrl = 'Google Maps URL is required.'; return newErrors; }; const handleSubmit = () => { const formErrors = validateForm(); if (Object.keys(formErrors).length > 0) { setErrors(formErrors); return; } if (state === 'writing') { onSubmit(form); } }; return ( <>

{state === 'writing' ? 'Publish Map' : 'View Map Details'}

{state === 'writing' ? (
{errors.name &&

{errors.name}

}
{errors.lastName &&

{errors.lastName}

}
{errors.googleMapsUrl && (

{errors.googleMapsUrl}

)}
) : (

Map Name: {form.mapName}

Name: {form.name}

Last Name: {form.lastName}

Google Maps URL:{' '} {form.googleMapsUrl}

Type of Map: {form.mapType}

)}
); }; export default PublishForm;