diff options
author | Biswakalyan Bhuyan <biswa@surgot.in> | 2024-07-18 15:49:21 +0530 |
---|---|---|
committer | Biswakalyan Bhuyan <biswa@surgot.in> | 2024-07-18 15:49:21 +0530 |
commit | cf0421c94b8ca14e819bad45e7db6875b92d547b (patch) | |
tree | 8deb4de15449bc2cf24c88950490ce60b5f6aaa8 /src/components | |
parent | be93c42fc010e7309e9e10d0431418ae5e7dbb93 (diff) | |
download | admin-panel-cf0421c94b8ca14e819bad45e7db6875b92d547b.tar.gz admin-panel-cf0421c94b8ca14e819bad45e7db6875b92d547b.tar.bz2 admin-panel-cf0421c94b8ca14e819bad45e7db6875b92d547b.zip |
added and setup dashboard login auth and many more
Diffstat (limited to 'src/components')
-rw-r--r-- | src/components/Dashboard.js | 83 | ||||
-rw-r--r-- | src/components/Login.js | 39 | ||||
-rw-r--r-- | src/components/PrivateRoute.js | 17 |
3 files changed, 119 insertions, 20 deletions
diff --git a/src/components/Dashboard.js b/src/components/Dashboard.js new file mode 100644 index 0000000..cb14e26 --- /dev/null +++ b/src/components/Dashboard.js @@ -0,0 +1,83 @@ +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 index 9a67051..64539c6 100644 --- a/src/components/Login.js +++ b/src/components/Login.js @@ -1,33 +1,32 @@ import React, { useState } from 'react'; import axios from 'axios'; +import { useHistory } from 'react-router-dom'; -const Login = ({ setAuth }) => { - const [formData, setFormData] = useState({ - username: '', - password: '', - }); +const Login = () => { + const [email, setEmail] = useState(''); + const [password, setPassword] = useState(''); + const history = useHistory(); - const { username, password } = formData; - - const onChange = (e) => setFormData({ ...formData, [e.target.name]: e.target.value }); - - const onSubmit = async (e) => { + const handleSubmit = async (e) => { e.preventDefault(); try { - const res = await axios.post('/api/auth/login', formData); - localStorage.setItem('token', res.data.token); - setAuth(true); - } catch (err) { - console.error(err.response.data); + 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 ( - <form onSubmit={onSubmit}> - <input type="text" name="username" value={username} onChange={onChange} required /> - <input type="password" name="password" value={password} onChange={onChange} required /> - <button type="submit">Login</button> - </form> + <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> ); }; diff --git a/src/components/PrivateRoute.js b/src/components/PrivateRoute.js new file mode 100644 index 0000000..1296935 --- /dev/null +++ b/src/components/PrivateRoute.js @@ -0,0 +1,17 @@ +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; |