-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditEventPage.js
More file actions
168 lines (152 loc) · 5.41 KB
/
EditEventPage.js
File metadata and controls
168 lines (152 loc) · 5.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
function EditEventPage({ eventId }) {
const [event, setEvent] = React.useState(null);
const [title, setTitle] = React.useState("");
const [location, setLocation] = React.useState("");
const [latitude, setLatitude] = React.useState(null);
const [longitude, setLongitude] = React.useState(null);
const [date, setDate] = React.useState("");
const [startTime, setStartTime] = React.useState("");
const [endTime, setEndTime] = React.useState("");
const [diningType, setDiningType] = React.useState("Lunch");
const [isPublic, setIsPublic] = React.useState(true); // default to public
const userId = localStorage.getItem("user_id");
React.useEffect(() => {
fetch(`http://localhost:3001/api/events/${eventId}`)
.then(res => res.json())
.then(data => {
setEvent(data);
setTitle(data.title);
setLocation(data.location);
setDate(data.date.split("T")[0]);
setDiningType(data.dining_type);
const [start, end] = data.time_range.split(" - ");
setStartTime(start);
setEndTime(end);
setLatitude(data.latitude);
setLongitude(data.longitude);
setIsPublic(data.is_public);
});
}, [eventId, userId]);
function handleLocationInput(value) {
setLocation(value);
const match = defaultLocations.find(
loc => loc.name.toLowerCase() === value.toLowerCase()
);
if (match) {
setLatitude(match.lat);
setLongitude(match.lng);
} else {
setLatitude(null);
setLongitude(null);
}
}
function handleSubmit() {
if (!title || !date || !startTime || !endTime) {
alert("Please fill in all required fields.");
return;
}
const updatedEvent = {
title,
location,
latitude,
longitude,
date,
dining_type: diningType,
time_range: `${startTime} - ${endTime}`,
is_public: isPublic,
};
fetch(`http://localhost:3001/api/events/${eventId}`, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(updatedEvent),
})
.then(res => res.json())
.then(data => {
if (data.success) {
alert("Event updated!");
window.location.hash = `#event-detail/${eventId}`;
} else {
alert("Failed to update event.");
}
})
.catch(err => {
console.error("Error updating event:", err);
alert("Error updating event.");
});
}
if (!event) return <div className="p-4 text-white">Loading...</div>;
return (
<div className="flex flex-col min-h-screen pb-16 bg-[#f6fbff] p-6">
<h1 className="text-2xl font-bold mb-4 text-[#3a2e20]">Edit Event</h1>
<div className="space-y-4 max-w-xl w-full mx-auto">
<input
type="text"
value={title}
onChange={(e) => setTitle(e.target.value)}
placeholder="Event Title"
className="w-full px-4 py-2 rounded border bg-white"
/>
<input
type="text"
value={location}
onChange={(e) => handleLocationInput(e.target.value)}
placeholder="Location"
list="location-options"
className="w-full px-4 py-2 rounded border bg-white"
/>
<datalist id="location-options">
{defaultLocations.map((loc, i) => (
<option key={i} value={loc.name} />
))}
</datalist>
<div className="flex gap-2">
<input
type="date"
value={date}
onChange={(e) => setDate(e.target.value)}
className="flex-1 px-4 py-2 rounded border bg-white"
/>
<select
value={diningType}
onChange={(e) => setDiningType(e.target.value)}
className="flex-1 px-4 py-2 rounded border bg-white"
>
<option value="Lunch">Lunch</option>
<option value="Dinner">Dinner</option>
</select>
</div>
<div className="flex gap-2">
<input
type="time"
value={startTime}
onChange={(e) => setStartTime(e.target.value)}
className="flex-1 px-4 py-2 rounded border bg-white"
/>
<input
type="time"
value={endTime}
onChange={(e) => setEndTime(e.target.value)}
className="flex-1 px-4 py-2 rounded border bg-white"
/>
</div>
<div className="flex items-center gap-2">
<label className="text-sm font-medium text-gray-800">Make Event Public</label>
<input
type="checkbox"
checked={isPublic}
onChange={(e) => setIsPublic(e.target.checked)}
className="accent-blue-600"
/>
</div>
<button
onClick={handleSubmit}
className="bg-blue-500 text-white font-semibold px-6 py-2 rounded-xl w-full shadow-sm hover:bg-blue-600 transition"
>
Save Changes
</button>
</div>
<Navigation />
</div>
);
}
window.EditEventPage = EditEventPage;