diff options
Diffstat (limited to 'src/components')
-rw-r--r-- | src/components/Dashboard.js | 83 | ||||
-rw-r--r-- | src/components/Login.js | 33 | ||||
-rw-r--r-- | src/components/PrivateRoute.js | 17 |
3 files changed, 0 insertions, 133 deletions
diff --git a/src/components/Dashboard.js b/src/components/Dashboard.js deleted file mode 100644 index cb14e26..0000000 --- a/src/components/Dashboard.js +++ /dev/null @@ -1,83 +0,0 @@ -import React, { useState, useEffect } from 'react'; -import { useHistory } from 'react-router-dom'; -import { auth, firestore } from '../firebase/firebaseConfig'; -import { getStorage, ref, uploadBytes, deleteObject } from "firebase/storage"; -import { collection, getDocs, addDoc, deleteDoc, doc } from "firebase/firestore"; -import { Bar } from 'react-chartjs-2'; - -const Dashboard = () => { - const history = useHistory(); - const [ads, setAds] = useState([]); - const [views, setViews] = useState([]); - const storage = getStorage(); - const adsCollectionRef = collection(firestore, "ads"); - - useEffect(() => { - const fetchAds = async () => { - const adsSnapshot = await getDocs(adsCollectionRef); - setAds(adsSnapshot.docs.map(doc => ({ ...doc.data(), id: doc.id }))); - }; - - const fetchViews = async () => { - // Assume we have a 'views' collection to track ad views - const viewsSnapshot = await getDocs(collection(firestore, "views")); - setViews(viewsSnapshot.docs.map(doc => doc.data())); - }; - - fetchAds(); - fetchViews(); - }, []); - - const handleLogout = () => { - auth.signOut(); - localStorage.removeItem('token'); - history.push('/login'); - }; - - const handleUpload = async (event) => { - const file = event.target.files[0]; - const storageRef = ref(storage, `ads/${file.name}`); - await uploadBytes(storageRef, file); - await addDoc(adsCollectionRef, { name: file.name, url: `ads/${file.name}` }); - setAds([...ads, { name: file.name, url: `ads/${file.name}` }]); - }; - - const handleDelete = async (id, url) => { - const storageRef = ref(storage, url); - await deleteObject(storageRef); - await deleteDoc(doc(firestore, "ads", id)); - setAds(ads.filter(ad => ad.id !== id)); - }; - - const viewData = { - labels: ads.map(ad => ad.name), - datasets: [ - { - label: 'Ad Views', - data: views.map(view => view.count), - backgroundColor: 'rgba(75, 192, 192, 0.6)', - }, - ], - }; - - return ( - <div> - <h2>Dashboard</h2> - <button onClick={handleLogout}>Logout</button> - <input type="file" onChange={handleUpload} /> - <div> - {ads.map(ad => ( - <div key={ad.id}> - <p>{ad.name}</p> - <button onClick={() => handleDelete(ad.id, ad.url)}>Delete</button> - </div> - ))} - </div> - <div> - <Bar data={viewData} /> - </div> - </div> - ); -}; - -export default Dashboard; diff --git a/src/components/Login.js b/src/components/Login.js deleted file mode 100644 index 64539c6..0000000 --- a/src/components/Login.js +++ /dev/null @@ -1,33 +0,0 @@ -import React, { useState } from 'react'; -import axios from 'axios'; -import { useHistory } from 'react-router-dom'; - -const Login = () => { - const [email, setEmail] = useState(''); - const [password, setPassword] = useState(''); - const history = useHistory(); - - const handleSubmit = async (e) => { - e.preventDefault(); - try { - const response = await axios.post('/api/auth/login', { email, password }); - localStorage.setItem('token', response.data.token); - history.push('/dashboard'); - } catch (error) { - console.error('Error logging in:', error); - } - }; - - return ( - <div> - <h2>Login</h2> - <form onSubmit={handleSubmit}> - <input type="email" placeholder="Email" value={email} onChange={(e) => setEmail(e.target.value)} required /> - <input type="password" placeholder="Password" value={password} onChange={(e) => setPassword(e.target.value)} required /> - <button type="submit">Login</button> - </form> - </div> - ); -}; - -export default Login; diff --git a/src/components/PrivateRoute.js b/src/components/PrivateRoute.js deleted file mode 100644 index 1296935..0000000 --- a/src/components/PrivateRoute.js +++ /dev/null @@ -1,17 +0,0 @@ -import React from 'react'; -import { Route, Redirect } from 'react-router-dom'; - -const PrivateRoute = ({ component: Component, ...rest }) => { - const isAuthenticated = !!localStorage.getItem('token'); - - return ( - <Route - {...rest} - render={(props) => - isAuthenticated ? <Component {...props} /> : <Redirect to="/login" /> - } - /> - ); -}; - -export default PrivateRoute; |