@@ -2,13 +2,17 @@ import React, { useState, useEffect } from 'react';
2
2
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' ;
3
3
import { faTrashAlt } from '@fortawesome/free-solid-svg-icons' ;
4
4
import axios from 'axios' ;
5
+ import '../index.css' ;
5
6
6
7
const AdminDashboard : React . FC = ( ) => {
7
8
const [ ads , setAds ] = useState < any [ ] > ( [ ] ) ;
9
+ const [ loading , setLoading ] = useState < boolean > ( true ) ; // State for loading spinner
10
+ const [ error , setError ] = useState < string | null > ( null ) ; // State for error handling
8
11
9
12
useEffect ( ( ) => {
10
13
const fetchAds = async ( ) => {
11
14
try {
15
+ setLoading ( true ) ; // Start loading
12
16
const token = localStorage . getItem ( 'token' ) ;
13
17
const response = await axios . get ( 'https://api.contigosanmarcos.com/panel/notifications' , {
14
18
headers : {
@@ -19,17 +23,24 @@ const AdminDashboard: React.FC = () => {
19
23
setAds ( response . data ) ;
20
24
} else {
21
25
console . error ( 'Unexpected data format:' , response . data ) ;
26
+ setError ( 'Unexpected data format received from the server.' ) ;
22
27
}
23
28
} catch ( error ) {
24
29
console . error ( 'Error fetching ads:' , error ) ;
25
- alert ( 'Para gestionar los banners inicia sesión primero' ) ;
30
+ setError ( 'Error fetching ads. Please log in to manage the banners.' ) ;
31
+ } finally {
32
+ setLoading ( false ) ;
26
33
}
27
34
} ;
28
35
29
36
fetchAds ( ) ;
30
37
} , [ ] ) ;
31
38
32
39
const handleDelete = async ( index : number ) => {
40
+ if ( ! window . confirm ( 'Are you sure you want to delete this ad?' ) ) {
41
+ return ;
42
+ }
43
+
33
44
const adToDelete = ads [ index ] ;
34
45
const response = await fetch ( `https://api.contigosanmarcos.com/panel/notifications/${ adToDelete . id } ` , {
35
46
method : 'DELETE' ,
@@ -48,10 +59,15 @@ const AdminDashboard: React.FC = () => {
48
59
49
60
return (
50
61
< div className = "admin-dashboard" >
51
- < div className = "ad-list" >
52
- { ads . length > 0 ? (
53
- ads . map ( ( ad , index ) =>
54
- < div key = { index } className = { `ad ad-item ${ ad . ad_type } ` } >
62
+ { loading ? (
63
+ < p > Loading ads...</ p > // Display loading message
64
+ ) : error ? (
65
+ < p className = "error-message" > { error } </ p > // Display error message if any
66
+ ) : (
67
+ < div className = "ad-list" >
68
+ { ads . length > 0 ? (
69
+ ads . map ( ( ad , index ) => (
70
+ < div key = { ad . id } className = { `ad ad-item ${ ad . ad_type } ` } >
55
71
{ ad . ad_type === 'banner' ? (
56
72
< div className = "ad-banner" >
57
73
{ ad . image_url && < img src = { ad . image_url } alt = { ad . ad_title } /> }
@@ -73,16 +89,21 @@ const AdminDashboard: React.FC = () => {
73
89
</ div >
74
90
) }
75
91
< div className = "ad-controls" >
76
- < button className = "icon-button delete-button" onClick = { ( ) => handleDelete ( index ) } style = { { color : 'red' } } >
77
- < FontAwesomeIcon icon = { faTrashAlt } />
92
+ < button
93
+ className = "icon-button delete-button"
94
+ onClick = { ( ) => handleDelete ( index ) }
95
+ style = { { color : 'red' } }
96
+ >
97
+ < FontAwesomeIcon icon = { faTrashAlt } />
78
98
</ button >
79
99
</ div >
80
100
</ div >
81
- )
82
- ) : (
83
- < p > No ads available.</ p >
84
- ) }
85
- </ div >
101
+ ) )
102
+ ) : (
103
+ < p > No ads available.</ p >
104
+ ) }
105
+ </ div >
106
+ ) }
86
107
</ div >
87
108
) ;
88
109
} ;
0 commit comments