From 51e75830de5923708d917ec86fa347731e4cc61c Mon Sep 17 00:00:00 2001 From: SRV30 <142303159+SRV30@users.noreply.github.com> Date: Tue, 29 Oct 2024 01:03:18 +0530 Subject: [PATCH] User can change or modify their github account --- src/pages/Profile.jsx | 483 +++++++++++++++++++----------------------- 1 file changed, 219 insertions(+), 264 deletions(-) diff --git a/src/pages/Profile.jsx b/src/pages/Profile.jsx index 966b754..fca940d 100644 --- a/src/pages/Profile.jsx +++ b/src/pages/Profile.jsx @@ -7,35 +7,49 @@ import firebase from "firebase/compat/app"; import { useNavigate } from "react-router-dom"; import RepoInfo from "../Components/RepoInfo"; import Post from "../Components/post/Post"; -import db from "../firebase"; +import db from "../firebase"; // Ensure you have the correct import for your Firebase configuration import { FiLink } from "react-icons/fi"; -import Github from "../Components/Github"; export default function Profile() { const navigate = useNavigate(); - const [userData, setUserData] = useState([]); + const [userData, setUserData] = useState({}); const [repo, setRepo] = useState([]); const [content, setContent] = useState("repo"); const [post, setPost] = useState([]); const [loading, setLoading] = useState(false); - const cookie = document.cookie; + const cookie = document.cookie.split("=")[1]; // Extracting the GitHub username from cookies - function githubSignout() { + // Function to sign out from GitHub + const githubSignout = async () => { document.cookie = ""; - firebase - .auth() - .signOut() - .then( - function () { - console.log("Signout successful!"); - navigate("/"); - }, - function (error) { - console.log("Signout failed"); - } - ); - } + try { + await firebase.auth().signOut(); + console.log("Signout successful!"); + navigate("/"); + } catch (error) { + console.error("Signout failed", error); + } + }; + + // Function to handle GitHub sign-in + const githubSignIn = async () => { + const provider = new firebase.auth.GithubAuthProvider(); + + try { + const result = await firebase.auth().signInWithPopup(provider); + const user = result.user; + + console.log("GitHub sign-in successful:", user); + document.cookie = `username=${user.displayName}`; // Set the GitHub username in cookies + await fetchData(); + await fetchDataRepo(); + } catch (error) { + console.error("Error signing in with GitHub:", error.message); + } + }; + + // Function to fetch user data from GitHub const fetchData = async () => { try { const accessToken = process.env.REACT_APP_GITHUB_TOKEN; @@ -46,46 +60,14 @@ export default function Profile() { }, }); const data = await response.json(); - console.log(data) - return setUserData(data); + console.log(data); + setUserData(data); } catch (error) { - if (error.response && error.response.status === 403) { - const resetTime = parseInt( - error.response.headers["x-ratelimit-reset"], - 10 - ); - const currentTime = Math.floor(Date.now() / 1000); - const sleepTime = resetTime - currentTime; - console.log(`Rate limit exceeded. Sleeping for ${sleepTime} seconds.`); - await new Promise((resolve) => setTimeout(resolve, sleepTime * 1000)); - await fetchData(); - } else { - console.log(error.message); - } + console.error("Error fetching user data:", error.message); } }; - useEffect(() => { - fetchData(); - fetchDataRepo(); - setLoading(true); - setTimeout(() => { - setLoading(false); - }, 2000); - }, []); - - useEffect(() => { - const unsubscribe = db.collection('posts').where('usernames', '==', cookie).orderBy("timestamp", "desc").onSnapshot( - snapshot => { - setPost(snapshot.docs.map((doc) => ({id: doc.id, data: doc.data()}))); - }, - error => { - console.error("Error fetching posts:", error); - } - ); - - return () => unsubscribe(); - }, []); + // Function to fetch user repositories from GitHub const fetchDataRepo = async () => { try { const accessToken = process.env.REACT_APP_GITHUB_TOKEN; @@ -96,242 +78,215 @@ export default function Profile() { }, }); const data = await response.json(); - return setRepo(data); - } catch (error) { - if (error.response && error.response.status === 403) { - const resetTime = parseInt( - error.response.headers["x-ratelimit-reset"], - 10 - ); - const currentTime = Math.floor(Date.now() / 1000); - const sleepTime = resetTime - currentTime; - console.log(`Rate limit exceeded. Sleeping for ${sleepTime} seconds.`); - await new Promise((resolve) => setTimeout(resolve, sleepTime * 1000)); - await fetchDataRepo(); + console.log(data); + // Ensure that data is an array before setting it + if (Array.isArray(data)) { + setRepo(data); } else { - console.log(error.message); + console.error("Expected an array, but got:", data); + setRepo([]); // Reset repo to an empty array if data is not valid } + } catch (error) { + console.error("Error fetching repositories:", error.message); } }; + + useEffect(() => { + setLoading(true); + fetchData(); + fetchDataRepo(); + const timer = setTimeout(() => { + setLoading(false); + }, 2000); + return () => clearTimeout(timer); // Cleanup timeout on unmount + }, []); + + useEffect(() => { + const unsubscribe = db + .collection("posts") + .where("usernames", "==", cookie) + .orderBy("timestamp", "desc") + .onSnapshot( + (snapshot) => { + setPost( + snapshot.docs.map((doc) => ({ id: doc.id, data: doc.data() })) + ); + }, + (error) => { + console.error("Error fetching posts:", error); + } + ); + + return () => unsubscribe(); + }, [cookie]); + const onDeletePost = (postId) => { console.log("Deleting post with id:", postId); - setPost(prevPosts => prevPosts.filter(post => post.id !== postId)); + setPost((prevPosts) => prevPosts.filter((post) => post.id !== postId)); }; - let link = `${userData.blog}`; - if (link.substring(0, 8) !== "https://") { + + let link = userData.blog || ""; + if (link && link.substring(0, 8) !== "https://") { link = "https://" + link; } + return ( - <> -
- -
-

- Profile -

- {loading ? ( -
- -
- ) : ( - <> -
-
-
- Avatar not available. +
+ +
+

+ Profile +

+ {loading ? ( +
+ +
+ ) : ( + <> +
+
+
+ Avatar not available. + + +
+
+
+
+ {userData.name} +
+
-
-
-
- {userData.name} -
- -
- (@ +
+
Repositories: {userData.public_repos}
+
+ + Followers: {userData.followers} + +
+
+ + Following: {userData.following} + +
+
+
+ {userData.blog && ( + - -
-
-
- Repositories: {userData.public_repos} -
-
- - Followers: {userData.followers} - {" "} -
-
- - Following: {userData.following} - -
-
-
- {userData.blog && ( -
- Website:{" "} - - {userData.blog} - -
- )} - {userData.company && ( -
Company: {userData.company}
- )} - {userData.bio && ( -
Bio: {userData.bio}
- )} -
+ )} + {userData.company && ( +
Company: {userData.company}
+ )} + {userData.bio && ( +
Bio: {userData.bio}
+ )}
-
- {userData.blog && ( - - )} - {userData.company && ( -
Company: {userData.company}
- )} - {userData.bio && ( -
{userData.bio}
- )} -
-
-
- Repos: {userData.public_repos} -
- - Followers: {userData.followers} - {" "} - - Following: {userData.following} - +
+ {userData.blog && ( + + )} + {userData.company && ( +
Company: {userData.company}
+ )} + {userData.bio && ( +
{userData.bio}
+ )}
-
- -
-
-
setContent("post")} - > - - Posts -
-
setContent("repo")} - > - - Repositories +
+
+ + +
+
+ {content === "repo" ? ( + repo.length ? ( +
+ {repo.map((item) => ( + + ))} +
+ ) : ( +

No repositories found.

+ ) + ) : post.length ? ( +
+ {post.map(({ id, data }) => ( + + ))}
-
- {(content === "post" && post.length) !== 0 ? ( - "" ) : ( - <> -
- You have not created any post yet. -
- +

No posts found.

)} - {content === "post" - ? post.map( - ({ - id, - data: { - logo, - name, - usernames, - bio, - like, - likedBy, - commentCnt, - commentObj, - description, - }, - }) => { - return ( - - ); - } - ) - : repo.map((repos) => - repos.fork ? ( - "" - ) : ( - - ) - )} -
- - )} -
+
+ + )}
- +
); }