diff --git a/components/leadspage.tsx b/components/leadspage.tsx index f6a77d4..90185d6 100644 --- a/components/leadspage.tsx +++ b/components/leadspage.tsx @@ -1,4 +1,11 @@ -import React from 'react'; +"use client"; + +import React, { useState, useEffect } from 'react'; +import { getAuth, onAuthStateChanged } from 'firebase/auth'; +import { getFirestore, doc, getDoc, setDoc, collection, getDocs, addDoc } from 'firebase/firestore'; +import { getStorage, ref, uploadBytes, getDownloadURL } from 'firebase/storage'; +import { auth } from "@/Firebase"; +import { db } from "@/Firebase"; interface Lead { id: string; @@ -10,94 +17,96 @@ interface Lead { } const Leads: React.FC = () => { - const currentLeads: Lead[] = [ - { - id: '10', - name: 'Akash Singh', - position: '3rd year', - organization: 'CloudSek', - additionalInfo: 'Gsoc 24', - imageUrl: "https://media.licdn.com/dms/image/v2/D5603AQFeShMi1sbKLg/profile-displayphoto-shrink_800_800/profile-displayphoto-shrink_800_800/0/1718481763562?e=1729123200&v=beta&t=60HO7YQ53F44tUfCHikuDNYHZPojS2SJD0msO1Sm3eY", - }, - { - id: '1', - name: 'Saalim Quadri', - position: '4th year', - organization: 'Raptee', - additionalInfo: 'LFX 23', - imageUrl: 'https://ik.imagekit.io/qx5kklh3ls/WhatsApp%20Image%202023-10-29%20at%2011.12.20%20AM.jpeg?updatedAt=1698558279266', - }, - { - id: '3', - name: 'Pratyush Singh', - position: 'Alumni', - organization: 'Ultrahuman', - additionalInfo: 'Gsoc 23, 24', - imageUrl: 'https://github-production-user-asset-6210df.s3.amazonaws.com/90026952/279694915-c7693363-6623-4bd2-9503-ee092b0e3593.jpeg', - }, - ]; - - const alumniLeads: Lead[] = [ - { - id: '2', - name: 'Prathik Singh', - position: '', - organization: '', - additionalInfo: '', - imageUrl: 'https://ik.imagekit.io/qx5kklh3ls/index.jpeg?updatedAt=1678781533632', - }, - { - id: '4', - name: 'Ashutosh Pandey', - position: '', - organization: '', - additionalInfo: '', - imageUrl: 'https://avatars.githubusercontent.com/u/36353507?v=4', - }, - { - id: '5', - name: 'Bapu Pruthvidhar', - position: '', - organization: '', - additionalInfo: '', - imageUrl: 'https://avatars.githubusercontent.com/u/37359679?v=4', - }, - { - id: '6', - name: 'Anukul Anand', - position: '', - organization: '', - additionalInfo: '', - imageUrl: 'https://avatars.githubusercontent.com/u/64669326?v=4', - }, - { - id: '7', - name: 'Madhur Mehta', - position: '', - organization: '', - additionalInfo: '', - imageUrl: 'https://avatars.githubusercontent.com/u/77354138?v=4', - }, - { - id: '8', - name: 'Debayan Ghosh Dastider', - position: '', - organization: '', - additionalInfo: '', - imageUrl: 'https://avatars.githubusercontent.com/u/77199373?v=4', - }, - { - id: '9', - name: 'Rithik Raj Pandey', - position: '', - organization: '', - additionalInfo: '', - imageUrl: 'https://avatars.githubusercontent.com/u/83706503?s=400&u=2d00114433bc28b8e28252e41bbc919229b9a7f4&v=4', - }, - ]; + const [showForm, setShowForm] = useState(false); + const [isAdminLoggedIn, setIsAdminLoggedIn] = useState(false); + const [currentLeads, setCurrentLeads] = useState([]); + const [alumniLeads, setAlumniLeads] = useState([]); + + useEffect(() => { + // const auth = getAuth(); + // const db = getFirestore(); + + const checkAdmin = async (uid: string) => { + try { + console.log(uid); + const docRef = doc(db, "admin", uid); + const docSnap = await getDoc(docRef); + if (docSnap.exists()) { + console.log("helo") + setIsAdminLoggedIn(true); + console.log("checked") + // isAdminLoggedIn(true); + } else { + setIsAdminLoggedIn(false); + console.log(docRef) + } + } catch (error) { + console.error("Error checking admin status:", error); + setIsAdminLoggedIn(false); + } + }; + + const unsubscribe = onAuthStateChanged(auth, (user) => { + if (user) { + checkAdmin(user.uid); + } else { + setIsAdminLoggedIn(false); + } + }); + + // Fetch leads from Firestore + const fetchLeads = async () => { + const leadsRef = collection(db, "leads"); + const querySnapshot = await getDocs(leadsRef); + const currentLeads: Lead[] = []; + const alumniLeads: Lead[] = []; + + querySnapshot.forEach((doc) => { + const leadData = doc.data() as Lead; + if (leadData.position === "Current") { + currentLeads.push({ ...leadData, id: doc.id }); + } else { + alumniLeads.push({ ...leadData, id: doc.id }); + } + }); + + setCurrentLeads(currentLeads); + setAlumniLeads(alumniLeads); + }; + + fetchLeads(); + + return () => unsubscribe(); + }, []); + + const toggleForm = () => { + setShowForm(!showForm); + }; return (
+ {isAdminLoggedIn && ( + + )} + + {showForm && } +
@@ -110,13 +119,13 @@ interface LeadSectionProps { } const LeadSection: React.FC = ({ title, leads }) => ( -
+

@@ -161,4 +170,141 @@ const LeadSection: React.FC = ({ title, leads }) => (

); -export default Leads; +interface LeadFormProps { + closeForm: () => void; +} + +const LeadForm: React.FC = ({ closeForm }) => { + const [newLead, setNewLead] = useState>({ + name: '', + position: 'Current', + organization: '', + additionalInfo: '', + }); + const [image, setImage] = useState(null); + + const handleChange = (e: React.ChangeEvent) => { + setNewLead({ + ...newLead, + [e.target.name]: e.target.value, + }); + }; + + const handleImageChange = (e: React.ChangeEvent) => { + if (e.target.files && e.target.files[0]) { + setImage(e.target.files[0]); + } + }; + + const handleSubmit = async (e: React.FormEvent) => { + e.preventDefault(); + if (!newLead.name || !image) { + alert("Please provide the lead name and image."); + return; + } + + const db = getFirestore(); + const storage = getStorage(); + + try { + // Upload the image to Firebase Storage + const imageRef = ref(storage, `images/${newLead.name}`); + await uploadBytes(imageRef, image); + const imageUrl = await getDownloadURL(imageRef); + + // Add lead to Firestore + await addDoc(collection(db, 'leads'), { + ...newLead, + imageUrl: imageUrl, + }); + + console.log("New Lead Submitted:", { ...newLead, imageUrl }); + closeForm(); + } catch (error) { + console.error("Error adding new lead:", error); + } + }; + + return ( +
+

Add New Lead

+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+ +
+ ); +}; + +export default Leads; \ No newline at end of file