Skip to content

Commit 65103c5

Browse files
committed
work pls
1 parent e1503fb commit 65103c5

File tree

2 files changed

+37
-12
lines changed

2 files changed

+37
-12
lines changed

src/components/AdminDashboard.tsx

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@ import React, { useState, useEffect } from 'react';
22
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
33
import { faTrashAlt } from '@fortawesome/free-solid-svg-icons';
44
import axios from 'axios';
5+
import '../index.css';
56

67
const AdminDashboard: React.FC = () => {
78
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
811

912
useEffect(() => {
1013
const fetchAds = async () => {
1114
try {
15+
setLoading(true); // Start loading
1216
const token = localStorage.getItem('token');
1317
const response = await axios.get('https://api.contigosanmarcos.com/panel/notifications', {
1418
headers: {
@@ -19,17 +23,24 @@ const AdminDashboard: React.FC = () => {
1923
setAds(response.data);
2024
} else {
2125
console.error('Unexpected data format:', response.data);
26+
setError('Unexpected data format received from the server.');
2227
}
2328
} catch (error) {
2429
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);
2633
}
2734
};
2835

2936
fetchAds();
3037
}, []);
3138

3239
const handleDelete = async (index: number) => {
40+
if (!window.confirm('Are you sure you want to delete this ad?')) {
41+
return;
42+
}
43+
3344
const adToDelete = ads[index];
3445
const response = await fetch(`https://api.contigosanmarcos.com/panel/notifications/${adToDelete.id}`, {
3546
method: 'DELETE',
@@ -48,10 +59,15 @@ const AdminDashboard: React.FC = () => {
4859

4960
return (
5061
<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}`}>
5571
{ad.ad_type === 'banner' ? (
5672
<div className="ad-banner">
5773
{ad.image_url && <img src={ad.image_url} alt={ad.ad_title} />}
@@ -73,16 +89,21 @@ const AdminDashboard: React.FC = () => {
7389
</div>
7490
)}
7591
<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} />
7898
</button>
7999
</div>
80100
</div>
81-
)
82-
) : (
83-
<p>No ads available.</p>
84-
)}
85-
</div>
101+
))
102+
) : (
103+
<p>No ads available.</p>
104+
)}
105+
</div>
106+
)}
86107
</div>
87108
);
88109
};

src/index.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ h2 {
1818
color: #f8f6f6;
1919
}
2020

21+
.white-text {
22+
color: white;
23+
}
24+
2125
form, .dashboard-container {
2226
width: 100%;
2327
background-color: rgb(0, 0, 0);

0 commit comments

Comments
 (0)