[0c6b92a] | 1 | import React, { useState } from 'react';
|
---|
| 2 | import styles from './PublishForm.module.css';
|
---|
| 3 |
|
---|
| 4 | const PublishForm = ({ isAdmin = false, formData, onSubmit, onCancel, onApprove, onDeny }) => {
|
---|
| 5 | const [state, setState] = useState(isAdmin ? 'viewing' : 'writing');
|
---|
| 6 | const [form, setForm] = useState(
|
---|
| 7 | formData || {
|
---|
| 8 | id: -1,
|
---|
| 9 | name: '',
|
---|
| 10 | lastName: '',
|
---|
| 11 | mapName: '',
|
---|
| 12 | mapType: 'Other',
|
---|
| 13 | googleMapsUrl: '',
|
---|
| 14 | }
|
---|
| 15 | );
|
---|
| 16 | const [errors, setErrors] = useState({});
|
---|
| 17 |
|
---|
| 18 | const mapTypeOptions = ['Hospital', 'Faculty', 'House', 'Other'];
|
---|
| 19 |
|
---|
| 20 | const handleChange = (e) => {
|
---|
| 21 | const { name, value } = e.target;
|
---|
| 22 | setForm((prev) => ({ ...prev, [name]: value }));
|
---|
| 23 | setErrors((prev) => ({ ...prev, [name]: '' }));
|
---|
| 24 | };
|
---|
| 25 |
|
---|
| 26 | const validateForm = () => {
|
---|
| 27 | const newErrors = {};
|
---|
| 28 | if (!form.name.trim()) newErrors.name = 'Name is required.';
|
---|
| 29 | if (!form.lastName.trim()) newErrors.lastName = 'Last Name is required.';
|
---|
| 30 | if (!form.googleMapsUrl.trim()) newErrors.googleMapsUrl = 'Google Maps URL is required.';
|
---|
| 31 | return newErrors;
|
---|
| 32 | };
|
---|
| 33 |
|
---|
| 34 | const handleSubmit = () => {
|
---|
| 35 | const formErrors = validateForm();
|
---|
| 36 | if (Object.keys(formErrors).length > 0) {
|
---|
| 37 | setErrors(formErrors);
|
---|
| 38 | return;
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | if (state === 'writing') {
|
---|
| 42 | onSubmit(form);
|
---|
| 43 | }
|
---|
| 44 | };
|
---|
| 45 |
|
---|
| 46 | return (
|
---|
| 47 | <>
|
---|
| 48 | <div className={styles.modalOverlay}></div>
|
---|
| 49 | <div className={styles.modal}>
|
---|
| 50 | <div className={styles.modalContent}>
|
---|
| 51 | <h2>{state === 'writing' ? 'Publish Map' : 'View Map Details'}</h2>
|
---|
| 52 | {state === 'writing' ? (
|
---|
| 53 | <form>
|
---|
| 54 | <div>
|
---|
| 55 | <label>Name:</label>
|
---|
| 56 | <input
|
---|
| 57 | type="text"
|
---|
| 58 | name="name"
|
---|
| 59 | value={form.name}
|
---|
| 60 | onChange={handleChange}
|
---|
| 61 | />
|
---|
| 62 | {errors.name && <p className={styles.error}>{errors.name}</p>}
|
---|
| 63 | </div>
|
---|
| 64 | <div>
|
---|
| 65 | <label>Last Name:</label>
|
---|
| 66 | <input
|
---|
| 67 | type="text"
|
---|
| 68 | name="lastName"
|
---|
| 69 | value={form.lastName}
|
---|
| 70 | onChange={handleChange}
|
---|
| 71 | />
|
---|
| 72 | {errors.lastName && <p className={styles.error}>{errors.lastName}</p>}
|
---|
| 73 | </div>
|
---|
| 74 | <div>
|
---|
| 75 | <label>Google Maps URL:</label>
|
---|
| 76 | <input
|
---|
| 77 | type="url"
|
---|
| 78 | name="googleMapsUrl"
|
---|
| 79 | value={form.googleMapsUrl}
|
---|
| 80 | onChange={handleChange}
|
---|
| 81 | />
|
---|
| 82 | {errors.googleMapsUrl && (
|
---|
| 83 | <p className={styles.error}>{errors.googleMapsUrl}</p>
|
---|
| 84 | )}
|
---|
| 85 | </div>
|
---|
| 86 | <div>
|
---|
| 87 | <label>Type of Map:</label>
|
---|
| 88 | <select
|
---|
| 89 | name="mapType"
|
---|
| 90 | value={form.mapType}
|
---|
| 91 | onChange={handleChange}
|
---|
| 92 | >
|
---|
| 93 | {mapTypeOptions.map((option) => (
|
---|
| 94 | <option key={option} value={option}>
|
---|
| 95 | {option}
|
---|
| 96 | </option>
|
---|
| 97 | ))}
|
---|
| 98 | </select>
|
---|
| 99 | </div>
|
---|
| 100 | <div className={styles.buttonGroup}>
|
---|
| 101 | <button type="button" onClick={handleSubmit}>
|
---|
| 102 | Submit
|
---|
| 103 | </button>
|
---|
| 104 | <button
|
---|
| 105 | type="button"
|
---|
| 106 | className={styles.cancelButton}
|
---|
| 107 | onClick={onCancel}
|
---|
| 108 | >
|
---|
| 109 | Cancel
|
---|
| 110 | </button>
|
---|
| 111 | </div>
|
---|
| 112 | </form>
|
---|
| 113 | ) : (
|
---|
| 114 | <div>
|
---|
| 115 | <p><strong>Map Name:</strong> {form.mapName}</p>
|
---|
| 116 | <p><strong>Name:</strong> {form.name}</p>
|
---|
| 117 | <p><strong>Last Name:</strong> {form.lastName}</p>
|
---|
| 118 | <p>
|
---|
| 119 | <strong>Google Maps URL:</strong>{' '}
|
---|
| 120 | <a
|
---|
| 121 | href={form.googleMapsUrl}
|
---|
| 122 | target="_blank"
|
---|
| 123 | rel="noopener noreferrer"
|
---|
| 124 | >
|
---|
| 125 | {form.googleMapsUrl}
|
---|
| 126 | </a>
|
---|
| 127 | </p>
|
---|
| 128 | <p><strong>Type of Map:</strong> {form.mapType}</p>
|
---|
| 129 | <div className={styles.buttonGroup}>
|
---|
| 130 | <button
|
---|
| 131 | className={styles.cancelButton}
|
---|
| 132 | onClick={onCancel}
|
---|
| 133 | >
|
---|
| 134 | Cancel
|
---|
| 135 | </button>
|
---|
| 136 | <button
|
---|
| 137 | className={styles.approveButton}
|
---|
| 138 | onClick={() => onApprove(form.id,form.mapName)}
|
---|
| 139 | >
|
---|
| 140 | Approve
|
---|
| 141 | </button>
|
---|
| 142 | <button
|
---|
| 143 | className={styles.denyButton}
|
---|
| 144 | onClick={() => onDeny(form.id,form.mapName,"Reason")}
|
---|
| 145 | >
|
---|
| 146 | Deny
|
---|
| 147 | </button>
|
---|
| 148 | </div>
|
---|
| 149 | </div>
|
---|
| 150 | )}
|
---|
| 151 | </div>
|
---|
| 152 | </div>
|
---|
| 153 | </>
|
---|
| 154 | );
|
---|
| 155 | };
|
---|
| 156 |
|
---|
| 157 | export default PublishForm;
|
---|