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 | const [denyReason, setDenyReason] = useState('');
|
---|
18 | const [isDenying, setIsDenying] = useState(false);
|
---|
19 |
|
---|
20 | const mapTypeOptions = ['Hospital', 'Faculty', 'House', 'Other'];
|
---|
21 |
|
---|
22 | const handleChange = (e) => {
|
---|
23 | const { name, value } = e.target;
|
---|
24 | setForm((prev) => ({ ...prev, [name]: value }));
|
---|
25 | setErrors((prev) => ({ ...prev, [name]: '' }));
|
---|
26 | };
|
---|
27 |
|
---|
28 | const validateForm = () => {
|
---|
29 | const newErrors = {};
|
---|
30 | if (!form.name.trim()) newErrors.name = 'Name is required.';
|
---|
31 | if (!form.lastName.trim()) newErrors.lastName = 'Last Name is required.';
|
---|
32 | if (!form.googleMapsUrl.trim()) newErrors.googleMapsUrl = 'Google Maps URL is required.';
|
---|
33 | return newErrors;
|
---|
34 | };
|
---|
35 |
|
---|
36 | const handleSubmit = () => {
|
---|
37 | const formErrors = validateForm();
|
---|
38 | if (Object.keys(formErrors).length > 0) {
|
---|
39 | setErrors(formErrors);
|
---|
40 | return;
|
---|
41 | }
|
---|
42 |
|
---|
43 | if (state === 'writing') {
|
---|
44 | onSubmit(form);
|
---|
45 | }
|
---|
46 | };
|
---|
47 |
|
---|
48 | const handleDeny = () => {
|
---|
49 | if (!denyReason.trim()) {
|
---|
50 | alert('Please provide a reason for denial.');
|
---|
51 | return;
|
---|
52 | }
|
---|
53 | onDeny(form.id, form.mapName, denyReason);
|
---|
54 | setIsDenying(false);
|
---|
55 | };
|
---|
56 |
|
---|
57 | return (
|
---|
58 | <>
|
---|
59 | <div className={styles.modalOverlay}></div>
|
---|
60 | <div className={styles.modal}>
|
---|
61 | <div className={styles.modalContent}>
|
---|
62 | <h2>{state === 'writing' ? 'Publish Map' : 'View Map Details'}</h2>
|
---|
63 | {state === 'writing' ? (
|
---|
64 | <form>
|
---|
65 | <div>
|
---|
66 | <label>Name:</label>
|
---|
67 | <input
|
---|
68 | type="text"
|
---|
69 | name="name"
|
---|
70 | value={form.name}
|
---|
71 | onChange={handleChange}
|
---|
72 | />
|
---|
73 | {errors.name && <p className={styles.error}>{errors.name}</p>}
|
---|
74 | </div>
|
---|
75 | <div>
|
---|
76 | <label>Last Name:</label>
|
---|
77 | <input
|
---|
78 | type="text"
|
---|
79 | name="lastName"
|
---|
80 | value={form.lastName}
|
---|
81 | onChange={handleChange}
|
---|
82 | />
|
---|
83 | {errors.lastName && <p className={styles.error}>{errors.lastName}</p>}
|
---|
84 | </div>
|
---|
85 | <div>
|
---|
86 | <label>Google Maps URL:</label>
|
---|
87 | <input
|
---|
88 | type="url"
|
---|
89 | name="googleMapsUrl"
|
---|
90 | value={form.googleMapsUrl}
|
---|
91 | onChange={handleChange}
|
---|
92 | />
|
---|
93 | {errors.googleMapsUrl && (
|
---|
94 | <p className={styles.error}>{errors.googleMapsUrl}</p>
|
---|
95 | )}
|
---|
96 | </div>
|
---|
97 | <div>
|
---|
98 | <label>Type of Map:</label>
|
---|
99 | <select
|
---|
100 | name="mapType"
|
---|
101 | value={form.mapType}
|
---|
102 | onChange={handleChange}
|
---|
103 | >
|
---|
104 | {mapTypeOptions.map((option) => (
|
---|
105 | <option key={option} value={option}>
|
---|
106 | {option}
|
---|
107 | </option>
|
---|
108 | ))}
|
---|
109 | </select>
|
---|
110 | </div>
|
---|
111 | <div className={styles.buttonGroup}>
|
---|
112 | <button type="button" onClick={handleSubmit}>
|
---|
113 | Submit
|
---|
114 | </button>
|
---|
115 | <button
|
---|
116 | type="button"
|
---|
117 | className={styles.cancelButton}
|
---|
118 | onClick={onCancel}
|
---|
119 | >
|
---|
120 | Cancel
|
---|
121 | </button>
|
---|
122 | </div>
|
---|
123 | </form>
|
---|
124 | ) : (
|
---|
125 | <div>
|
---|
126 | <p><strong>Map Name:</strong> {form.mapName}</p>
|
---|
127 | <p><strong>Name:</strong> {form.name}</p>
|
---|
128 | <p><strong>Last Name:</strong> {form.lastName}</p>
|
---|
129 | <p>
|
---|
130 | <strong>Google Maps URL:</strong>{' '}
|
---|
131 | <a
|
---|
132 | href={form.googleMapsUrl}
|
---|
133 | target="_blank"
|
---|
134 | rel="noopener noreferrer"
|
---|
135 | >
|
---|
136 | {form.googleMapsUrl}
|
---|
137 | </a>
|
---|
138 | </p>
|
---|
139 | <p><strong>Type of Map:</strong> {form.mapType}</p>
|
---|
140 | <div className={styles.buttonGroup}>
|
---|
141 | <button
|
---|
142 | className={styles.cancelButton}
|
---|
143 | onClick={() => {
|
---|
144 | setIsDenying(false);
|
---|
145 | onCancel();
|
---|
146 | }}
|
---|
147 | >
|
---|
148 | Cancel
|
---|
149 | </button>
|
---|
150 | <button
|
---|
151 | className={styles.approveButton}
|
---|
152 | onClick={() => onApprove(form.id, form.mapName)}
|
---|
153 | >
|
---|
154 | Approve
|
---|
155 | </button>
|
---|
156 | <button
|
---|
157 | className={styles.denyButton}
|
---|
158 | onClick={() => setIsDenying(true)}
|
---|
159 | >
|
---|
160 | Deny
|
---|
161 | </button>
|
---|
162 | </div>
|
---|
163 | {isDenying && (
|
---|
164 | <div className={styles.denyReason}>
|
---|
165 | <textarea className={styles.denyReasonTextArea}
|
---|
166 | placeholder="Enter reason for denial..."
|
---|
167 | value={denyReason}
|
---|
168 | onChange={(e) => setDenyReason(e.target.value)}
|
---|
169 | />
|
---|
170 | <div className={styles.buttonGroup}>
|
---|
171 | <button
|
---|
172 | className={styles.denySubmitButton}
|
---|
173 | onClick={handleDeny}
|
---|
174 | >
|
---|
175 | Submit
|
---|
176 | </button>
|
---|
177 | </div>
|
---|
178 | </div>
|
---|
179 | )}
|
---|
180 | </div>
|
---|
181 | )}
|
---|
182 | </div>
|
---|
183 | </div>
|
---|
184 | </>
|
---|
185 | );
|
---|
186 | };
|
---|
187 |
|
---|
188 | export default PublishForm;
|
---|