7
7
doc ,
8
8
updateDoc ,
9
9
setDoc ,
10
+ deleteDoc ,
10
11
} from "firebase/firestore" ;
11
12
import firebase_app from "../../lib/firebase/config" ;
12
- import "./events.css" ;
13
13
import { Event } from "@/models/event/event" ;
14
14
import { DataTable } from "@/components/ui/data-table" ;
15
15
import { ColumnDef } from "@tanstack/react-table" ;
@@ -34,18 +34,21 @@ import {
34
34
DialogHeader ,
35
35
DialogTitle ,
36
36
} from "@/components/ui/dialog" ;
37
- import { DialogClose } from "@radix-ui/react-dialog" ;
38
37
import { Input } from "@/components/ui/input" ;
39
38
import { EventType } from "@/models/event/event-type.d" ;
40
39
import { EventStatus } from "@/models/event/event-status.d" ;
41
40
42
- const Events : React . FC = ( ) => {
41
+ import React from "react" ;
42
+
43
+ export default function Events ( ) {
43
44
const [ events , setEvents ] = useState < Event [ ] > ( [ ] ) ;
45
+
44
46
const [ error , setError ] = useState < string | null > ( null ) ;
45
47
const [ loading , setLoading ] = useState < boolean > ( true ) ;
48
+
46
49
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
+
49
52
const [ isNewEvent , setIsNewEvent ] = useState ( false ) ;
50
53
51
54
useEffect ( ( ) => {
@@ -72,7 +75,6 @@ const Events: React.FC = () => {
72
75
const handleEdit = ( event : any , isNew : boolean ) => {
73
76
setIsNewEvent ( isNew ) ;
74
77
setEditMode ( true ) ;
75
- setSelectedEvent ( event ) ;
76
78
setEditedEvent ( {
77
79
...event ,
78
80
category : Array . isArray ( event . category ) ? event . category : [ ] ,
@@ -103,32 +105,40 @@ const Events: React.FC = () => {
103
105
e . preventDefault ( ) ;
104
106
try {
105
107
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` ;
113
109
114
- const eventRef = doc ( db , "events" , selectedEvent . id ) ;
110
+ const eventRef = doc ( db , "events" , isNewEvent ? newId : editedEvent ! . id ) ;
115
111
// Update event with new data
116
112
isNewEvent === true
117
- ? await setDoc ( eventRef , editedEvent )
118
- : await updateDoc ( eventRef , editedEvent ) ;
113
+ ? await setDoc ( eventRef , { ... editedEvent , id : newId } )
114
+ : await updateDoc ( eventRef , { ... editedEvent } ) ;
119
115
// 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
+ ) ;
123
121
setEvents ( updatedEvents ) ;
124
122
setEditMode ( false ) ;
125
- setSelectedEvent ( null ) ;
126
123
setEditedEvent ( null ) ;
127
124
} catch ( error ) {
128
125
console . error ( "Error updating event:" , error ) ;
129
126
}
130
127
} ;
131
128
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
+
132
142
function titleCase ( str : string ) {
133
143
return str
134
144
. split ( " " )
@@ -163,7 +173,6 @@ const Events: React.FC = () => {
163
173
164
174
const handleCancel = ( ) => {
165
175
setEditMode ( false ) ;
166
- setSelectedEvent ( null ) ;
167
176
setEditedEvent ( null ) ;
168
177
} ;
169
178
@@ -301,6 +310,9 @@ const Events: React.FC = () => {
301
310
>
302
311
Copy ID
303
312
</ DropdownMenuItem >
313
+ < DropdownMenuItem onClick = { ( ) => handleDelete ( event . id ) } >
314
+ Delete
315
+ </ DropdownMenuItem >
304
316
305
317
< DropdownMenuSeparator />
306
318
</ DropdownMenuContent >
@@ -312,14 +324,21 @@ const Events: React.FC = () => {
312
324
313
325
return (
314
326
< 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 >
320
334
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 } >
323
342
{ /* <DialogTrigger>Open</DialogTrigger> */ }
324
343
325
344
< DialogContent >
@@ -334,7 +353,7 @@ const Events: React.FC = () => {
334
353
< Input
335
354
type = "text"
336
355
name = "name"
337
- value = { editedEvent . name }
356
+ value = { editedEvent ! . name }
338
357
onChange = { handleChange }
339
358
className = "form-input mt-1 block w-full border-gray-300 rounded-md focus:border-blue-400 focus:outline-none"
340
359
/>
@@ -344,7 +363,7 @@ const Events: React.FC = () => {
344
363
< Input
345
364
type = "text"
346
365
name = "description"
347
- value = { editedEvent . description }
366
+ value = { editedEvent ! . description }
348
367
onChange = { handleChange }
349
368
className = "form-input mt-1 block w-full border-gray-300 rounded-md focus:border-blue-400 focus:outline-none"
350
369
/>
@@ -354,7 +373,7 @@ const Events: React.FC = () => {
354
373
< Input
355
374
type = "text"
356
375
name = "description_short"
357
- value = { editedEvent . description_short }
376
+ value = { editedEvent ! . description_short }
358
377
onChange = { handleChange }
359
378
className = "form-input mt-1 block w-full border-gray-300 rounded-md focus:border-blue-400 focus:outline-none"
360
379
/>
@@ -364,7 +383,7 @@ const Events: React.FC = () => {
364
383
< Input
365
384
type = "date"
366
385
name = "start_date"
367
- defaultValue = { new Date ( Number ( editedEvent . start_date ) )
386
+ defaultValue = { new Date ( Number ( editedEvent ? .start_date ) )
368
387
. toLocaleDateString ( )
369
388
. split ( "/" )
370
389
. reverse ( )
@@ -383,15 +402,15 @@ const Events: React.FC = () => {
383
402
{
384
403
// eslint-disable-next-line @next/next/no-img-element
385
404
< img
386
- src = { editedEvent . imageURL }
405
+ src = { editedEvent ! . imageURL }
387
406
alt = "image"
388
407
className = "w-full h-auto"
389
408
/>
390
409
}
391
410
< input
392
411
type = "text"
393
412
name = "imageURL"
394
- value = { editedEvent . imageURL }
413
+ value = { editedEvent ! . imageURL }
395
414
onChange = { handleChange }
396
415
className = "form-input mt-1 block w-full border-gray-300 rounded-md focus:border-blue-400 focus:outline-none"
397
416
/>
@@ -403,9 +422,12 @@ const Events: React.FC = () => {
403
422
value,
404
423
label : titleCase ( value . split ( "-" ) . join ( " " ) ) ,
405
424
} ) ) }
406
- selectedValue = { editedEvent . status }
425
+ selectedValue = { editedEvent ! . status }
407
426
onChange = { ( value ) =>
408
- setEditedEvent ( { ...editedEvent , status : value } )
427
+ setEditedEvent ( {
428
+ ...editedEvent ! ,
429
+ status : value as EventStatus ,
430
+ } )
409
431
}
410
432
label = "Select Status"
411
433
/>
@@ -418,9 +440,12 @@ const Events: React.FC = () => {
418
440
value,
419
441
label : titleCase ( value . split ( "-" ) . join ( " " ) ) ,
420
442
} ) ) }
421
- selectedValue = { editedEvent . type }
443
+ selectedValue = { editedEvent ! . type }
422
444
onChange = { ( value ) =>
423
- setEditedEvent ( { ...editedEvent , type : value } )
445
+ setEditedEvent ( {
446
+ ...editedEvent ! ,
447
+ type : value as EventType ,
448
+ } )
424
449
}
425
450
label = "Select Type"
426
451
/>
@@ -431,8 +456,8 @@ const Events: React.FC = () => {
431
456
type = "text"
432
457
name = "category"
433
458
value = {
434
- Array . isArray ( editedEvent . category )
435
- ? editedEvent . category . join ( ", " )
459
+ Array . isArray ( editedEvent ! . category )
460
+ ? editedEvent ! . category . join ( ", " )
436
461
: ""
437
462
}
438
463
onChange = { handleChange }
@@ -456,119 +481,6 @@ const Events: React.FC = () => {
456
481
</ DialogContent >
457
482
</ Dialog >
458
483
) }
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
- )} */ }
570
484
</ div >
571
485
) ;
572
- } ;
573
-
574
- export default Events ;
486
+ }
0 commit comments