diff --git a/src/components/Card/CardWithPicture.jsx b/src/components/Card/CardWithPicture.jsx index e8cd65c2..7e28f756 100644 --- a/src/components/Card/CardWithPicture.jsx +++ b/src/components/Card/CardWithPicture.jsx @@ -76,6 +76,7 @@ export default function CardWithPicture({ tutorial }) { const [alignment, setAlignment] = React.useState("left"); const [count, setCount] = useState(1); const dispatch = useDispatch(); + const [user,setUser]=useState(null) const firebase = useFirebase(); const firestore = useFirestore(); const handleIncrement = () => { @@ -91,16 +92,25 @@ export default function CardWithPicture({ tutorial }) { }; useEffect(() => { - getUserProfileData(tutorial?.created_by)(firebase, firestore, dispatch); + const fetchData = async () => { + const data = await getUserProfileData(tutorial?.user_uid)( + firebase, + firestore, + dispatch + ); + console.log("Data From CardWithPic ",data) + setUser(data); + }; + fetchData(); }, [tutorial]); - const user = useSelector( - ({ - profile: { - user: { data } - } - }) => data - ); + // const user = useSelector( + // ({ + // profile: { + // user: { data } + // } + // }) => data + // ); const getTime = timestamp => { return timestamp.toDate().toDateString(); diff --git a/src/components/Card/CardWithoutPicture.jsx b/src/components/Card/CardWithoutPicture.jsx index abc4ecb0..3aa130e7 100644 --- a/src/components/Card/CardWithoutPicture.jsx +++ b/src/components/Card/CardWithoutPicture.jsx @@ -69,6 +69,7 @@ export default function CardWithoutPicture({ tutorial }) { const classes = useStyles(); const [alignment, setAlignment] = React.useState("left"); const [count, setCount] = useState(1); + const [user,setUser]=useState(null) const dispatch = useDispatch(); const firebase = useFirebase(); const firestore = useFirestore(); @@ -85,16 +86,24 @@ export default function CardWithoutPicture({ tutorial }) { }; useEffect(() => { - getUserProfileData(tutorial?.created_by)(firebase, firestore, dispatch); + const fetchData = async () => { + const data = await getUserProfileData(tutorial?.user_uid)( + firebase, + firestore, + dispatch + ); + setUser(data); + }; + fetchData(); }, [tutorial]); - const user = useSelector( - ({ - profile: { - user: { data } - } - }) => data - ); + // const user = useSelector( + // ({ + // profile: { + // user: { data } + // } + // }) => data + // ); const getTime = timestamp => { return timestamp.toDate().toDateString(); diff --git a/src/components/TutorialPage/components/Commnets/Comment.jsx b/src/components/TutorialPage/components/Commnets/Comment.jsx index 09e486fc..e823dc94 100644 --- a/src/components/TutorialPage/components/Commnets/Comment.jsx +++ b/src/components/TutorialPage/components/Commnets/Comment.jsx @@ -73,7 +73,6 @@ const Comment = ({ id }) => { ); const [data] = commentsArray.filter(comment => comment.comment_id == id); - const repliesArray = useSelector( ({ tutorialPage: { @@ -102,7 +101,7 @@ const Comment = ({ id }) => { replyTo: data.comment_id, tutorial_id: data.tutorial_id, createdAt: firestore.FieldValue.serverTimestamp(), - userId: "codelabzuser" + userId: data?.userId }; addComment(commentData)(firebase, firestore, dispatch); }; diff --git a/src/components/TutorialPage/components/Commnets/CommentBox.jsx b/src/components/TutorialPage/components/Commnets/CommentBox.jsx index 02f462f9..d888f3e0 100644 --- a/src/components/TutorialPage/components/Commnets/CommentBox.jsx +++ b/src/components/TutorialPage/components/Commnets/CommentBox.jsx @@ -28,7 +28,7 @@ const useStyles = makeStyles(() => ({ } })); -const CommentBox = ({ commentsArray, tutorialId }) => { +const CommentBox = ({ commentsArray, tutorialId,userId }) => { const classes = useStyles(); const firestore = useFirestore(); const firebase = useFirebase(); @@ -41,7 +41,7 @@ const CommentBox = ({ commentsArray, tutorialId }) => { replyTo: tutorialId, tutorial_id: tutorialId, createdAt: firestore.FieldValue.serverTimestamp(), - userId: "codelabzuser" + userId: userId }; addComment(commentData)(firebase, firestore, dispatch); }; @@ -70,7 +70,7 @@ const CommentBox = ({ commentsArray, tutorialId }) => { {comments?.map((id, index) => { return ( - + ); })} diff --git a/src/components/TutorialPage/components/UserDetails.jsx b/src/components/TutorialPage/components/UserDetails.jsx index 0438b2f9..d24366e4 100644 --- a/src/components/TutorialPage/components/UserDetails.jsx +++ b/src/components/TutorialPage/components/UserDetails.jsx @@ -25,20 +25,24 @@ const User = ({ id, timestamp, showFollowButton, size }) => { const firebase = useFirebase(); const firestore = useFirestore(); const [isFollowed, setIsFollowed] = useState(true); + const [user, setUser] = useState(null); useEffect(() => { - getUserProfileData(id)(firebase, firestore, dispatch); - return () => {}; + const fetchData = async () => { + const data = await getUserProfileData(id)(firebase, firestore, dispatch); + setUser(data); + }; + fetchData(); }, [id]); const profileData = useSelector(({ firebase: { profile } }) => profile); - const user = useSelector( - ({ - profile: { - user: { data } - } - }) => data - ); + // const user = useSelector( + // ({ + // profile: { + // user: { data } + // } + // }) => data + // ); useEffect(() => { const checkIsFollowed = async () => { diff --git a/src/components/TutorialPage/index.jsx b/src/components/TutorialPage/index.jsx index dd7895c2..57b58a17 100644 --- a/src/components/TutorialPage/index.jsx +++ b/src/components/TutorialPage/index.jsx @@ -17,11 +17,13 @@ import { getUserProfileData } from "../../store/actions"; import { useDispatch, useSelector } from "react-redux"; import { useFirebase, useFirestore } from "react-redux-firebase"; import { useParams, useHistory } from "react-router-dom"; +import { getCurrentUserData } from "../../store/actions/profileActions"; function TutorialPage({ background = "white", textColor = "black" }) { const classes = useStyles(); const { id } = useParams(); const history = useHistory(); + const [userId, setUserId]=useState(null); const windowSize = useWindowSize(); const [openMenu, setOpen] = useState(false); const toggleSlider = () => { @@ -35,6 +37,13 @@ function TutorialPage({ background = "white", textColor = "black" }) { getTutorialSteps(id)(firebase, firestore, dispatch); return () => {}; }, []); + useEffect(()=>{ + const fetchUserData = async()=>{ + const data = await getCurrentUserData()(firebase, firestore,dispatch); + setUserId(data.uid); + } + fetchUserData() + },[firebase]) const tutorial = useSelector( ({ tutorialPage: { @@ -112,7 +121,7 @@ function TutorialPage({ background = "white", textColor = "black" }) { > - + async dispatch => { */ export const checkUserHandleExists = userHandle => async firebase => { try { - const handle = await firebase - .ref(`/cl_user_handle/${userHandle}`) - .once("value"); - return handle.exists(); + const userSnapshot = await firebase + .firestore() + .collection("cl_user") + .doc(userHandle) + .get(); + return userSnapshot.exists; } catch (e) { throw e.message; } diff --git a/src/store/actions/profileActions.js b/src/store/actions/profileActions.js index c3a96c31..913a0f42 100644 --- a/src/store/actions/profileActions.js +++ b/src/store/actions/profileActions.js @@ -160,26 +160,28 @@ export const uploadProfileImage = export const getUserProfileData = handle => async (firebase, firestore, dispatch) => { try { + let doc; dispatch({ type: actions.GET_USER_DATA_START }); - const isUserExists = await checkUserHandleExists(handle)(firestore); + const isUserExists = await checkUserHandleExists(handle)(firebase); if (isUserExists) { - const docs = await firestore - .collection("cl_user") - .where("handle", "==", handle) - .get(); - const doc = docs.docs[0].data(); - const currentUserId = firebase.auth().currentUser.uid; - const followingStatus = await isUserFollower( - currentUserId, - doc.uid, - firestore - ); - dispatch({ - type: actions.GET_USER_DATA_SUCCESS, - payload: { ...doc, isFollowing: followingStatus } - }); + const docRef = firestore.collection("cl_user").doc(handle); + doc = (await docRef.get()).data(); + if (doc) { + const currentUserId = firebase.auth().currentUser.uid; + const followingStatus = await isUserFollower( + currentUserId, + doc.uid, + firestore + ); + dispatch({ + type: actions.GET_USER_DATA_SUCCESS, + payload: { ...doc, isFollowing: followingStatus } + }); + } + return doc; } else { dispatch({ type: actions.GET_USER_DATA_SUCCESS, payload: false }); + return null; } } catch (e) { dispatch({ type: actions.GET_USER_DATA_FAIL, payload: e.message }); @@ -297,3 +299,14 @@ const getAllOrgsOfCurrentUser = () => async (firebase, firestore) => { console.log(e); } }; + +export const getCurrentUserData = + () => async (firebase, firestore, dispatch) => { + try { + const user = await firebase.auth().currentUser; + if (user) return user; + } catch (error) { + console.error("Error fetching current user data:", error); + return null; + } + };