From d22338964a66499eb74a44a7026319237500d199 Mon Sep 17 00:00:00 2001 From: 2004yash <134356616+2004yash@users.noreply.github.com> Date: Sat, 21 Sep 2024 09:11:03 +0530 Subject: [PATCH 1/2] minor changes --- components/leadspage.tsx | 156 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 146 insertions(+), 10 deletions(-) diff --git a/components/leadspage.tsx b/components/leadspage.tsx index 82d5d98..ff28d04 100644 --- a/components/leadspage.tsx +++ b/components/leadspage.tsx @@ -1,4 +1,6 @@ -import React from 'react'; +"use client"; + +import React, { useState } from 'react'; interface Lead { id: string; @@ -10,6 +12,8 @@ interface Lead { } const Leads: React.FC = () => { + const [showForm, setShowForm] = useState(false); + const currentLeads: Lead[] = [ { id: '10', @@ -20,13 +24,13 @@ const Leads: React.FC = () => { 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: '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', @@ -96,8 +100,32 @@ const Leads: React.FC = () => { }, ]; + const toggleForm = () => { + setShowForm(!showForm); + }; + return (
+ + + {showForm && } +
@@ -110,13 +138,13 @@ interface LeadSectionProps { } const LeadSection: React.FC = ({ title, leads }) => ( -
+

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

); +interface LeadFormProps { + closeForm: () => void; +} + +const LeadForm: React.FC = ({ closeForm }) => { + const [formData, setFormData] = useState({ + id: '', + name: '', + position: '', + organization: '', + additionalInfo: '', + imageUrl: '', + }); + + const handleChange = (e: React.ChangeEvent) => { + setFormData({ + ...formData, + [e.target.name]: e.target.value, + }); + }; + + const handleSubmit = (e: React.FormEvent) => { + e.preventDefault(); + // Handle form submission logic here + console.log(formData); + closeForm(); + }; + + return ( +
+

Add a New Lead

+
+ + + + + + + +
+
+ ); +}; + export default Leads; From b6593d2c64f2e7ae9d7c10a14dfa176b38c2d891 Mon Sep 17 00:00:00 2001 From: 2004yash <134356616+2004yash@users.noreply.github.com> Date: Sun, 22 Sep 2024 23:10:14 +0530 Subject: [PATCH 2/2] leadspage completed --- components/leadspage.tsx | 374 ++++++++++++++++++++------------------- 1 file changed, 192 insertions(+), 182 deletions(-) diff --git a/components/leadspage.tsx b/components/leadspage.tsx index ff28d04..90185d6 100644 --- a/components/leadspage.tsx +++ b/components/leadspage.tsx @@ -1,6 +1,11 @@ "use client"; -import React, { useState } from 'react'; +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; @@ -13,92 +18,66 @@ interface Lead { const Leads: React.FC = () => { const [showForm, setShowForm] = useState(false); + const [isAdminLoggedIn, setIsAdminLoggedIn] = useState(false); + const [currentLeads, setCurrentLeads] = useState([]); + const [alumniLeads, setAlumniLeads] = useState([]); - 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', - }, - ]; + useEffect(() => { + // const auth = getAuth(); + // const db = getFirestore(); - 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 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); @@ -106,24 +85,26 @@ const Leads: React.FC = () => { return (
- - + {isAdminLoggedIn && ( + + )} + {showForm && } @@ -194,107 +175,136 @@ interface LeadFormProps { } const LeadForm: React.FC = ({ closeForm }) => { - const [formData, setFormData] = useState({ - id: '', + const [newLead, setNewLead] = useState>({ name: '', - position: '', + position: 'Current', organization: '', additionalInfo: '', - imageUrl: '', }); + const [image, setImage] = useState(null); - const handleChange = (e: React.ChangeEvent) => { - setFormData({ - ...formData, + const handleChange = (e: React.ChangeEvent) => { + setNewLead({ + ...newLead, [e.target.name]: e.target.value, }); }; - const handleSubmit = (e: React.FormEvent) => { + 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(); - // Handle form submission logic here - console.log(formData); - closeForm(); + 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 a New Lead

-
- - +

Add New Lead

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