Skip to content

Commit f5c7d4a

Browse files
committed
refactor+feat: fix incorrect id, save emulator data
- fix incorrect id issue - add emulator save on exit command - update page layout - refactor code
1 parent ca3b9f0 commit f5c7d4a

15 files changed

+12960
-1896
lines changed

database-debug.log

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
16:12:01.519 [NamespaceSystem-akka.actor.default-dispatcher-4] INFO akka.event.slf4j.Slf4jLogger - Slf4jLogger started
2-
16:12:01.609 [main] INFO com.firebase.server.forge.App$ - Listening at 127.0.0.1:9000
1+
20:31:50.718 [NamespaceSystem-akka.actor.default-dispatcher-5] INFO akka.event.slf4j.Slf4jLogger - Slf4jLogger started
2+
20:31:50.800 [main] INFO com.firebase.server.forge.App$ - Listening at 127.0.0.1:9000

firebase-debug.log

+7,796-1,106
Large diffs are not rendered by default.

firestore-debug.log

+5,058-594
Large diffs are not rendered by default.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"dev": "next dev",
77
"build": "next build",
88
"start": "next start",
9-
"lint": "next lint"
9+
"lint": "next lint",
10+
"emulator:start": "firebase emulators:start --import=./src/data/firebase_emulator_data --export-on-exit=./src/data/firebase_emulator_data"
1011
},
1112
"dependencies": {
1213
"@radix-ui/react-dialog": "^1.0.5",

src/app/pages/events.tsx src/app/events/page.tsx

+66-154
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import {
77
doc,
88
updateDoc,
99
setDoc,
10+
deleteDoc,
1011
} from "firebase/firestore";
1112
import firebase_app from "../../lib/firebase/config";
12-
import "./events.css";
1313
import { Event } from "@/models/event/event";
1414
import { DataTable } from "@/components/ui/data-table";
1515
import { ColumnDef } from "@tanstack/react-table";
@@ -34,18 +34,21 @@ import {
3434
DialogHeader,
3535
DialogTitle,
3636
} from "@/components/ui/dialog";
37-
import { DialogClose } from "@radix-ui/react-dialog";
3837
import { Input } from "@/components/ui/input";
3938
import { EventType } from "@/models/event/event-type.d";
4039
import { EventStatus } from "@/models/event/event-status.d";
4140

42-
const Events: React.FC = () => {
41+
import React from "react";
42+
43+
export default function Events() {
4344
const [events, setEvents] = useState<Event[]>([]);
45+
4446
const [error, setError] = useState<string | null>(null);
4547
const [loading, setLoading] = useState<boolean>(true);
48+
4649
const [editMode, setEditMode] = useState<boolean>(false);
47-
const [selectedEvent, setSelectedEvent] = useState<any>(null);
48-
const [editedEvent, setEditedEvent] = useState<any>(null);
50+
const [editedEvent, setEditedEvent] = useState<Event | null>(null);
51+
4952
const [isNewEvent, setIsNewEvent] = useState(false);
5053

5154
useEffect(() => {
@@ -72,7 +75,6 @@ const Events: React.FC = () => {
7275
const handleEdit = (event: any, isNew: boolean) => {
7376
setIsNewEvent(isNew);
7477
setEditMode(true);
75-
setSelectedEvent(event);
7678
setEditedEvent({
7779
...event,
7880
category: Array.isArray(event.category) ? event.category : [],
@@ -103,32 +105,40 @@ const Events: React.FC = () => {
103105
e.preventDefault();
104106
try {
105107
const db = getFirestore(firebase_app);
106-
const newEventId = `event-${selectedEvent.name}-123`;
107-
const { id, ...eventDetails } = selectedEvent;
108-
if (isNewEvent === true)
109-
setSelectedEvent({
110-
...eventDetails,
111-
id: newEventId,
112-
});
108+
const newId = `event-${editedEvent!.name}-123`;
113109

114-
const eventRef = doc(db, "events", selectedEvent.id);
110+
const eventRef = doc(db, "events", isNewEvent ? newId : editedEvent!.id);
115111
// Update event with new data
116112
isNewEvent === true
117-
? await setDoc(eventRef, editedEvent)
118-
: await updateDoc(eventRef, editedEvent);
113+
? await setDoc(eventRef, { ...editedEvent, id: newId })
114+
: await updateDoc(eventRef, { ...editedEvent });
119115
// Fetch updated events
120-
const updatedEvents = events.map((event) =>
121-
event.id === selectedEvent.id ? editedEvent : event
122-
);
116+
const updatedEvents = isNewEvent
117+
? [{ ...editedEvent!, id: newId }, ...events]
118+
: events.map((event) =>
119+
event.id === editedEvent!.id ? editedEvent! : event
120+
);
123121
setEvents(updatedEvents);
124122
setEditMode(false);
125-
setSelectedEvent(null);
126123
setEditedEvent(null);
127124
} catch (error) {
128125
console.error("Error updating event:", error);
129126
}
130127
};
131128

129+
const handleDelete = async (eventId: string) => {
130+
try {
131+
const db = getFirestore(firebase_app);
132+
const eventRef = doc(db, "events", eventId);
133+
134+
await deleteDoc(eventRef);
135+
136+
setEvents(events.filter((event) => event.id != eventId));
137+
} catch (error) {
138+
console.error("Error deleting event:", error);
139+
}
140+
};
141+
132142
function titleCase(str: string) {
133143
return str
134144
.split(" ")
@@ -163,7 +173,6 @@ const Events: React.FC = () => {
163173

164174
const handleCancel = () => {
165175
setEditMode(false);
166-
setSelectedEvent(null);
167176
setEditedEvent(null);
168177
};
169178

@@ -301,6 +310,9 @@ const Events: React.FC = () => {
301310
>
302311
Copy ID
303312
</DropdownMenuItem>
313+
<DropdownMenuItem onClick={() => handleDelete(event.id)}>
314+
Delete
315+
</DropdownMenuItem>
304316

305317
<DropdownMenuSeparator />
306318
</DropdownMenuContent>
@@ -312,14 +324,21 @@ const Events: React.FC = () => {
312324

313325
return (
314326
<div className="container mx-auto mt-8">
315-
<h1 className="text-3xl font-bold mb-4">Events</h1>
316-
<Button onClick={() => handleEdit(createTestEvent(), true)}>
317-
Create Event
318-
</Button>
319-
<DataTable columns={columns} data={events} />
327+
<div className="flex flex-col gap-4">
328+
<div className="flex w-full justify-between">
329+
<h1 className="text-3xl font-bold">Events</h1>
330+
<Button onClick={() => handleEdit(createTestEvent(), true)}>
331+
Create Event
332+
</Button>
333+
</div>
320334

321-
{editMode && selectedEvent && (
322-
<Dialog open={editMode && selectedEvent}>
335+
<div className="bg-white">
336+
<DataTable columns={columns} data={events} />
337+
</div>
338+
</div>
339+
340+
{editMode && editedEvent && (
341+
<Dialog open={editMode && editedEvent != null}>
323342
{/* <DialogTrigger>Open</DialogTrigger> */}
324343

325344
<DialogContent>
@@ -334,7 +353,7 @@ const Events: React.FC = () => {
334353
<Input
335354
type="text"
336355
name="name"
337-
value={editedEvent.name}
356+
value={editedEvent!.name}
338357
onChange={handleChange}
339358
className="form-input mt-1 block w-full border-gray-300 rounded-md focus:border-blue-400 focus:outline-none"
340359
/>
@@ -344,7 +363,7 @@ const Events: React.FC = () => {
344363
<Input
345364
type="text"
346365
name="description"
347-
value={editedEvent.description}
366+
value={editedEvent!.description}
348367
onChange={handleChange}
349368
className="form-input mt-1 block w-full border-gray-300 rounded-md focus:border-blue-400 focus:outline-none"
350369
/>
@@ -354,7 +373,7 @@ const Events: React.FC = () => {
354373
<Input
355374
type="text"
356375
name="description_short"
357-
value={editedEvent.description_short}
376+
value={editedEvent!.description_short}
358377
onChange={handleChange}
359378
className="form-input mt-1 block w-full border-gray-300 rounded-md focus:border-blue-400 focus:outline-none"
360379
/>
@@ -364,7 +383,7 @@ const Events: React.FC = () => {
364383
<Input
365384
type="date"
366385
name="start_date"
367-
defaultValue={new Date(Number(editedEvent.start_date))
386+
defaultValue={new Date(Number(editedEvent?.start_date))
368387
.toLocaleDateString()
369388
.split("/")
370389
.reverse()
@@ -383,15 +402,15 @@ const Events: React.FC = () => {
383402
{
384403
// eslint-disable-next-line @next/next/no-img-element
385404
<img
386-
src={editedEvent.imageURL}
405+
src={editedEvent!.imageURL}
387406
alt="image"
388407
className="w-full h-auto"
389408
/>
390409
}
391410
<input
392411
type="text"
393412
name="imageURL"
394-
value={editedEvent.imageURL}
413+
value={editedEvent!.imageURL}
395414
onChange={handleChange}
396415
className="form-input mt-1 block w-full border-gray-300 rounded-md focus:border-blue-400 focus:outline-none"
397416
/>
@@ -403,9 +422,12 @@ const Events: React.FC = () => {
403422
value,
404423
label: titleCase(value.split("-").join(" ")),
405424
}))}
406-
selectedValue={editedEvent.status}
425+
selectedValue={editedEvent!.status}
407426
onChange={(value) =>
408-
setEditedEvent({ ...editedEvent, status: value })
427+
setEditedEvent({
428+
...editedEvent!,
429+
status: value as EventStatus,
430+
})
409431
}
410432
label="Select Status"
411433
/>
@@ -418,9 +440,12 @@ const Events: React.FC = () => {
418440
value,
419441
label: titleCase(value.split("-").join(" ")),
420442
}))}
421-
selectedValue={editedEvent.type}
443+
selectedValue={editedEvent!.type}
422444
onChange={(value) =>
423-
setEditedEvent({ ...editedEvent, type: value })
445+
setEditedEvent({
446+
...editedEvent!,
447+
type: value as EventType,
448+
})
424449
}
425450
label="Select Type"
426451
/>
@@ -431,8 +456,8 @@ const Events: React.FC = () => {
431456
type="text"
432457
name="category"
433458
value={
434-
Array.isArray(editedEvent.category)
435-
? editedEvent.category.join(", ")
459+
Array.isArray(editedEvent!.category)
460+
? editedEvent!.category.join(", ")
436461
: ""
437462
}
438463
onChange={handleChange}
@@ -456,119 +481,6 @@ const Events: React.FC = () => {
456481
</DialogContent>
457482
</Dialog>
458483
)}
459-
460-
{/* {editMode && selectedEvent && (
461-
<div className="edit-form-overlay fixed top-0 left-0 w-full h-full flex justify-center items-center bg-black bg-opacity-50">
462-
<div className="edit-form bg-white rounded-lg shadow-md p-6">
463-
<h2 className="text-xl font-bold mb-4">Edit Event</h2>
464-
<form onSubmit={handleSubmit}>
465-
<label className="block mb-4">
466-
<span className="font-bold">Name:</span>
467-
<input
468-
type="text"
469-
name="name"
470-
value={editedEvent.name}
471-
onChange={handleChange}
472-
className="form-input mt-1 block w-full border-gray-300 rounded-md focus:border-blue-400 focus:outline-none"
473-
/>
474-
</label>
475-
<label className="block mb-4">
476-
<span className="font-bold">Description:</span>
477-
<input
478-
type="text"
479-
name="description"
480-
value={editedEvent.description}
481-
onChange={handleChange}
482-
className="form-input mt-1 block w-full border-gray-300 rounded-md focus:border-blue-400 focus:outline-none"
483-
/>
484-
</label>
485-
<label className="block mb-4">
486-
<span className="font-bold">Description Short:</span>
487-
<input
488-
type="text"
489-
name="description_short"
490-
value={editedEvent.description_short}
491-
onChange={handleChange}
492-
className="form-input mt-1 block w-full border-gray-300 rounded-md focus:border-blue-400 focus:outline-none"
493-
/>
494-
</label>
495-
<label className="block mb-4">
496-
<span className="font-bold">Start Date:</span>
497-
<input
498-
type="text"
499-
name="start_date"
500-
value={new Date(
501-
Number(editedEvent.start_date)
502-
).toLocaleDateString()}
503-
onChange={handleChange}
504-
className="form-input mt-1 block w-full border-gray-300 rounded-md focus:border-blue-400 focus:outline-none"
505-
/>
506-
</label>
507-
<label className="block mb-4">
508-
<span className="font-bold">Image URL:</span>
509-
<input
510-
type="text"
511-
name="imageURL"
512-
value={editedEvent.imageURL}
513-
onChange={handleChange}
514-
className="form-input mt-1 block w-full border-gray-300 rounded-md focus:border-blue-400 focus:outline-none"
515-
/>
516-
</label>
517-
<label className="block mb-4">
518-
<span className="font-bold">Status:</span>
519-
<input
520-
type="text"
521-
name="status"
522-
value={editedEvent.status}
523-
onChange={handleChange}
524-
className="form-input mt-1 block w-full border-gray-300 rounded-md focus:border-blue-400 focus:outline-none"
525-
/>
526-
</label>
527-
<label className="block mb-4">
528-
<span className="font-bold">Type:</span>
529-
<input
530-
type="text"
531-
name="type"
532-
value={editedEvent.type}
533-
onChange={handleChange}
534-
className="form-input mt-1 block w-full border-gray-300 rounded-md focus:border-blue-400 focus:outline-none"
535-
/>
536-
</label>
537-
<label className="block mb-4">
538-
<span className="font-bold">Categories:</span>
539-
<input
540-
type="text"
541-
name="category"
542-
value={
543-
Array.isArray(editedEvent.category)
544-
? editedEvent.category.join(", ")
545-
: ""
546-
}
547-
onChange={handleChange}
548-
className="form-input mt-1 block w-full border-gray-300 rounded-md focus:border-blue-400 focus:outline-none"
549-
/>
550-
</label>
551-
<div className="flex justify-end">
552-
<button
553-
type="submit"
554-
className="mr-2 bg-blue-500 text-white px-4 py-2 rounded-md hover:bg-blue-600"
555-
>
556-
Save
557-
</button>
558-
<button
559-
type="button"
560-
onClick={handleCancel}
561-
className="bg-gray-300 text-gray-700 px-4 py-2 rounded-md hover:bg-gray-400"
562-
>
563-
Cancel
564-
</button>
565-
</div>
566-
</form>
567-
</div>
568-
</div>
569-
)} */}
570484
</div>
571485
);
572-
};
573-
574-
export default Events;
486+
}

0 commit comments

Comments
 (0)