Skip to content

Commit 7d4287e

Browse files
authored
Merge pull request #5 from druvkotwani/dataRetrieval/Updation
Data retrieval/updation
2 parents f39e715 + f203f69 commit 7d4287e

File tree

5 files changed

+101
-75
lines changed

5 files changed

+101
-75
lines changed

client/src/App.jsx

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,33 @@ import Navbar from "./components/Navbar";
22
import Stat from "./components/Stat";
33
import StatsGenerator from "./components/StatsGenerator";
44
import { useEffect, useState } from "react";
5+
import { collection, getDocs } from "firebase/firestore";
6+
import { firestore } from "./firebase";
57

68
import "./App.css";
79

810
export default function App() {
911
const [showStats, setShowStats] = useState(false);
1012

13+
const [data, setData] = useState([]);
14+
15+
useEffect(() => {
16+
const getContacts = async () => {
17+
try {
18+
const dataCollection = collection(firestore, "users_stats");
19+
const dataSnapshot = await getDocs(dataCollection);
20+
const dataList = dataSnapshot.docs.map((doc) => {
21+
return doc.data()
22+
})
23+
setData(dataList);
24+
} catch (error) {
25+
console.log(error);
26+
}
27+
};
28+
29+
getContacts();
30+
}, []);
31+
1132
useEffect(() => {
1233
// Set an interval to toggle the animation class every 5 seconds
1334
const interval = setInterval(() => {
@@ -35,11 +56,11 @@ export default function App() {
3556
<div className="flex flex-col mx-auto max-w-screen-xl px-2 sm:px-6 lg:px-8">
3657
<div className="grid grid-cols-1 gap-4 py-4 ">
3758
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2 lg:grid-cols-3">
38-
<Stat />
39-
<Stat />
40-
<Stat />
41-
<Stat />
42-
59+
{
60+
data.map(data => (
61+
<Stat key={data.username} data={data} />
62+
))
63+
}
4364
</div>
4465
</div>
4566
</div>

client/src/components/About.jsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,34 @@ const About = ({ result }) => {
88
<span className='text-sm text-[#BDBEC3] '>India</span>
99
</p> */}
1010
{
11-
result.website.link && (<p className="flex">
11+
result?.website?.link && (<p className="flex">
1212
<iconify-icon icon="streamline-emojis:globe-showing-americas" style={{ color: 'white', marginRight: '1px' }} width="19" height="19"></iconify-icon>
1313
<span className='text-sm text-[#BDBEC3] '>
14-
<a href={result.website.link} target="_blank" rel='noreferrer' className="">{result.website.text}</a>
14+
<a href={result?.website?.link} target="_blank" rel='noreferrer' className="">{result?.website?.text}</a>
1515
</span>
1616
</p>)
1717
}
1818
{
19-
result.github.link && (<p className="flex">
19+
result?.github?.link && (<p className="flex">
2020
<iconify-icon icon="mdi:github" style={{ color: 'white', marginRight: '5px' }} width="17" height="19"></iconify-icon>
2121
<span className='text-sm text-[#BDBEC3] '>
22-
<a href={result.github.link} target="_blank" rel='noreferrer' className="">{result.github.text}</a>
22+
<a href={result?.github?.link} target="_blank" rel='noreferrer' className="">{result?.github?.text}</a>
2323
</span>
2424
</p>)
2525
}
2626
{
27-
result.twitter.link && (<p className="flex">
27+
result?.twitter?.link && (<p className="flex">
2828
<iconify-icon icon="fa6-brands:square-x-twitter" style={{ color: 'white', marginRight: '5px' }} width="17" height="19"></iconify-icon>
2929
<span className='text-sm text-[#BDBEC3] '>
30-
<a href={result.twitter.link} target="_blank" rel='noreferrer' className="">{result.twitter.text}</a>
30+
<a href={result?.twitter?.link} target="_blank" rel='noreferrer' className="">{result?.twitter?.text}</a>
3131
</span>
3232
</p>)
3333
}
3434
{
35-
result.linkedin.link && (<p className="flex">
35+
result?.linkedin?.link && (<p className="flex">
3636
<iconify-icon icon="devicon:linkedin" style={{ color: 'white', marginRight: '5px' }} width="17" height="19 "></iconify-icon>
3737
<span className='text-sm text-[#BDBEC3] '>
38-
<a href={result.linkedin.link} target="_blank" rel='noreferrer' className="">{result.linkedin.text}</a>
38+
<a href={result?.linkedin?.link} target="_blank" rel='noreferrer' className="">{result?.linkedin?.text}</a>
3939
</span>
4040
</p>
4141
)

client/src/components/Profile.jsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,23 @@ const Profile = ({ userData }) => {
66
<div className="flex justify-center gap-2">
77
{/* Image */}
88
<div className="relative flex h-20 w-[70px] shrink-0 flex-nowrap">
9-
<img src={userData.profileData.image} alt="Avatar" className="h-[70px] w-[70px] rounded-lg object-cover" />
9+
<img src={userData?.image} alt="Avatar" className="h-[70px] w-[70px] rounded-lg object-cover" />
1010
</div>
1111

1212
{/* Name/username/rank*/}
1313
<div className="flex flex-col">
1414
<div className="flex items-center" >
15-
<p className=" break-all text-sm font-semibold text-[#FFFEFE]">{userData.profileData.fullName}</p>
15+
<p className=" break-all text-sm font-semibold text-[#FFFEFE]">{userData?.fullName}</p>
1616
</div>
1717
<div className="flex items-center flex-nowrap" >
18-
<p className="text-xs text-[#9FA1A4]">{userData.profileData.username}</p>
19-
{userData.profileData.badgeImg && <div className="ml-1">
20-
<img src={userData.profileData.badgeImg} className="h-[14px] w-[14px]" />
21-
</div>}
18+
<p className="text-xs text-[#9FA1A4]">{userData?.username}</p>
19+
{userData?.badgeImg && (<div className="ml-1">
20+
<img src={userData.badgeImg} className="h-[14px] w-[14px]" />
21+
</div>)}
2222
</div>
2323
<p className="flex mt-3 items-end space-x-[5px] text-base">
2424
<span className="text-sm text-[#9FA1A4] ">Rank</span>
25-
<span className=" text-sm font-medium text-[#FFFFF8]">{userData.profileData.rank}</span>
25+
<span className=" text-sm font-medium text-[#FFFFF8]">{userData?.rank}</span>
2626
</p>
2727
</div>
2828
</div>

client/src/components/Stat.jsx

Lines changed: 56 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,88 @@
11
import Questions from "./Questions";
22
import Circle from "./Circle";
3-
import { useState } from "react";
3+
import { useState, useEffect } from "react";
44
import Profile from "./Profile";
55
import About from "./About";
66

7-
export default function Stat() {
7+
export default function Stat({ data }) {
8+
const [userData, setUserData] = useState({});
89

9-
const defaultProfileData = {
10-
image: '/assets/profile.png',
11-
fullName: 'Dhruv Kotwani',
12-
username: 'druv_kotwani',
13-
badgeImg: '/assets/badge.png',
14-
rank: '234, 542',
15-
};
10+
const setDataManually = () => {
11+
const newProfileData = {
12+
fullName: data.fullName,
13+
image: data.image,
14+
badgeImg: data.badgeImg,
15+
username: data.username,
16+
rank: data.rank,
17+
};
1618

17-
let defaultAboutUs = {
18-
linkedin: { link: "https://linkedin.com/in/dhruv-kotwani", text: "dhruv-kotwani" },
19-
twitter: { link: "https://twitter.com/druv_kotwani", text: "druv_kotwani" },
20-
github: { link: "https://github.com/druvkotwani", text: "druvkotwani" },
21-
website: { link: "https://dhruvkotwani.me", text: "dhruvkotwani.me" }
22-
};
23-
const defaultQuestionData = {
24-
easySolved: 165,
25-
easyTotal: 745,
26-
easyBeats: '96.9%',
27-
mediumSolved: 92,
28-
mediumTotal: 1547,
29-
mediumBeats: '84.8%',
30-
hardSolved: 20,
31-
hardTotal: 644,
32-
hardBeats: '77.6%',
33-
totalSolved: 277,
34-
totalQuestions: 2940,
35-
};
19+
const newQuestionData = {
20+
easySolved: data.easySolved,
21+
easyTotal: data.easyTotal,
22+
easyBeats: data.easyBeats,
23+
mediumSolved: data.mediumSolved,
24+
mediumTotal: data.mediumTotal,
25+
mediumBeats: data.mediumBeats,
26+
hardSolved: data.hardSolved,
27+
hardTotal: data.hardTotal,
28+
hardBeats: data.hardBeats,
29+
totalSolved: data.totalSolved,
30+
totalQuestions: data.totalQuestions,
31+
};
32+
33+
const newAboutData = {
34+
linkedin: data.linkedin,
35+
twitter: data.twitter,
36+
github: data.github,
37+
website: data.website,
38+
// Add other about data fields as needed
39+
}
40+
setUserData({
41+
profileData: newProfileData,
42+
questionData: newQuestionData,
43+
aboutData: newAboutData,
44+
});
45+
}
46+
useEffect(() => {
47+
if (data) {
48+
setDataManually(data);
49+
}
50+
}, [data]);
3651

37-
const [userData, setUserData] = useState({
38-
profileData: defaultProfileData,
39-
questionData: defaultQuestionData,
40-
aboutData: defaultAboutUs,
41-
});
4252

4353

4454
return (
4555
<div className="flex justify-center flex-col items-center rounded-lg h-[280px] bg-[#292829]">
4656
<div>
47-
<div className="flex gap-1 xl:gap-4 items-center justify-center ">
48-
<Profile userData={userData} />
49-
<About result={userData.aboutData} />
57+
<div className="flex gap-1 xl:gap-4 items-center justify-between ">
58+
<Profile userData={userData?.profileData} />
59+
<About result={userData?.aboutData} />
5060
</div>
5161
<div style={{ height: '0.5px', backgroundColor: '#E0E0E0' }} className="h-[0.5px] bg-white" />
5262
<div className=" flex lg:flex-row gap-5 items-center justify-center mt-4">
53-
<Circle total={userData?.questionData?.totalSolved} />
63+
<Circle total={data?.totalSolved} />
5464
<div className="flex flex-col gap-3">
5565
<Questions
5666
type={'Easy'}
57-
solved={userData?.questionData?.easySolved}
58-
total={userData?.questionData?.easyTotal}
59-
beats={userData?.questionData?.easyBeats}
67+
solved={data?.easySolved}
68+
total={data?.easyTotal}
69+
beats={data?.easyBeats}
6070
line='bg-[#2db55d26]'
6171
line2='bg-[#01B8A2]'
6272
/>
6373
<Questions
6474
type={'Medium'}
65-
solved={userData?.questionData?.mediumSolved}
66-
total={userData?.questionData?.mediumTotal}
67-
beats={userData?.questionData?.mediumBeats}
75+
solved={data?.mediumSolved}
76+
total={data?.mediumTotal}
77+
beats={data?.mediumBeats}
6878
line='bg-[#ffb80026]'
6979
line2='bg-[#FFC11F]'
7080
/>
7181
<Questions
7282
type={'Hard'}
73-
solved={userData?.questionData?.hardSolved}
74-
total={userData?.questionData?.hardTotal}
75-
beats={userData?.questionData?.hardBeats}
83+
solved={data?.hardSolved}
84+
total={data?.hardTotal}
85+
beats={data?.hardBeats}
7686
line='bg-[#ef474326]'
7787
line2='bg-[#EF4642]'
7888
/>

client/src/components/StatsGenerator.jsx

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export default function StatsGenerator({ setShowStats }) {
5353
fullName: userData.profileData.fullName,
5454
image: userData.profileData.image,
5555
username: userData.profileData.username,
56+
badgeImg: userData.profileData.badgeImg || '',
5657
rank: userData.profileData.rank,
5758
easySolved: userData.questionData.easySolved,
5859
easyTotal: userData.questionData.easyTotal,
@@ -74,14 +75,6 @@ export default function StatsGenerator({ setShowStats }) {
7475
};
7576

7677
const addData = async () => {
77-
if (userName.trim() !== '') {
78-
// Proceed with the action since the input is not empty
79-
// Your logic here
80-
} else {
81-
// Input is empty, show an error message or prevent the action
82-
// For now, let's just console log a message
83-
console.log('Please enter a username.');
84-
}
8578
try {
8679
const transformedUserData = transformUserData(userData);
8780
const dataCollection = collection(firestore, "users_stats")
@@ -102,6 +95,8 @@ export default function StatsGenerator({ setShowStats }) {
10295
catch (err) {
10396
console.log(err);
10497
}
98+
setShowStats(false)
99+
105100
}
106101

107102
function handleInputChange(e) {
@@ -140,7 +135,7 @@ export default function StatsGenerator({ setShowStats }) {
140135
{loading ? (<Skeleton />) : (<div className="rounded-lg w-[95%] sm:w-[65%] md:w-[50%] lg:w-[35%] xl:w-[30%] h-[270px] bg-[#292829] mb-5">
141136

142137
<div className="flex items-center justify-around ">
143-
<Profile userData={userData} />
138+
<Profile userData={userData.profileData} />
144139
<About result={userData.aboutData} />
145140
</div>
146141

0 commit comments

Comments
 (0)