Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ Fix : Stored the commented_user Id and Used it to Show Avatar ] #1211

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions src/components/Card/CardWithPicture.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
Expand All @@ -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();
Expand Down
25 changes: 17 additions & 8 deletions src/components/Card/CardWithoutPicture.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down
3 changes: 1 addition & 2 deletions src/components/TutorialPage/components/Commnets/Comment.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ const Comment = ({ id }) => {
);

const [data] = commentsArray.filter(comment => comment.comment_id == id);

const repliesArray = useSelector(
({
tutorialPage: {
Expand Down Expand Up @@ -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);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
};
Expand Down Expand Up @@ -70,7 +70,7 @@ const CommentBox = ({ commentsArray, tutorialId }) => {
{comments?.map((id, index) => {
return (
<Grid item xs={12}>
<Comment id={id} key={index} />
<Comment id={id} key={index}/>
</Grid>
);
})}
Expand Down
22 changes: 13 additions & 9 deletions src/components/TutorialPage/components/UserDetails.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
11 changes: 10 additions & 1 deletion src/components/TutorialPage/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
Expand All @@ -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: {
Expand Down Expand Up @@ -112,7 +121,7 @@ function TutorialPage({ background = "white", textColor = "black" }) {
>
<PostDetails details={postDetails} />
<Tutorial steps={steps} />
<CommentBox commentsArray={tutorial?.comments} tutorialId={id} />
<CommentBox commentsArray={tutorial?.comments} tutorialId={id} userId={userId} />
</Grid>

<Grid
Expand Down
10 changes: 6 additions & 4 deletions src/store/actions/authActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,12 @@ export const resendVerifyEmail = email => 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;
}
Expand Down
45 changes: 29 additions & 16 deletions src/store/actions/profileActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down Expand Up @@ -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;
}
};