Skip to content

Commit

Permalink
Merge pull request #862 from ABHISHEK-PANDEY2/post
Browse files Browse the repository at this point in the history
[FEAT]: Feed tutorials according to followers posts
  • Loading branch information
ABHISHEK-PANDEY2 authored Aug 24, 2023
2 parents 8ef46b2 + c23d019 commit d8a3465
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 10 deletions.
20 changes: 14 additions & 6 deletions src/components/HomePage/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ import useWindowSize from "../../helpers/customHooks/useWindowSize";
import NewTutorial from "../Tutorials/NewTutorial";
import { useDispatch, useSelector } from "react-redux";
import { useFirebase, useFirestore } from "react-redux-firebase";
import { getTutorialFeedData } from "../../store/actions/tutorialPageActions";
import {
getTutorialFeedData,
getTutorialFeedIdArray
} from "../../store/actions/tutorialPageActions";

function HomePage({ background = "white", textColor = "black" }) {
const classes = useStyles();
Expand Down Expand Up @@ -161,10 +164,17 @@ function HomePage({ background = "white", textColor = "black" }) {
}
]);

const tutorialIdArray = ["9VF7JGNPYmTQe7tdhZfQ"];

const profileData = useSelector(({ firebase: { profile } }) => profile);
useEffect(() => {
getTutorialFeedData(tutorialIdArray)(firebase, firestore, dispatch);
const getFeed = async () => {
const tutorialIdArray = await getTutorialFeedIdArray(profileData.uid)(
firebase,
firestore,
dispatch
);
getTutorialFeedData(tutorialIdArray)(firebase, firestore, dispatch);
};
getFeed();
}, []);
const tutorials = useSelector(
({
Expand All @@ -174,8 +184,6 @@ function HomePage({ background = "white", textColor = "black" }) {
}) => homepageFeedArray
);

console.log("tutorials array", tutorials);

const notification = () => {};
const handleChange = (event, newValue) => {
setValue(newValue);
Expand Down
21 changes: 20 additions & 1 deletion src/components/TutorialPage/components/UserDetails.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import Avatar from "@mui/material/Avatar";
import { useDispatch, useSelector } from "react-redux";
import { useFirebase, useFirestore } from "react-redux-firebase";
import { getUserProfileData } from "../../../store/actions";
import { isUserFollower } from "../../../store/actions/profileActions";
import { addUserFollower } from "../../../store/actions";
const useStyles = makeStyles(() => ({
container: {
padding: "20px",
Expand All @@ -22,10 +24,13 @@ const User = ({ id, timestamp, showFollowButton, size }) => {
const dispatch = useDispatch();
const firebase = useFirebase();
const firestore = useFirestore();
const [isFollowed, setIsFollowed] = useState(true);
useEffect(() => {
getUserProfileData(id)(firebase, firestore, dispatch);
}, [id]);

const profileData = useSelector(({ firebase: { profile } }) => profile);

const user = useSelector(
({
profile: {
Expand All @@ -34,6 +39,19 @@ const User = ({ id, timestamp, showFollowButton, size }) => {
}) => data
);

useEffect(() => {
const checkIsFollowed = async () => {
const status = await isUserFollower(profileData.uid, user.uid, firestore);
setIsFollowed(status);
console.log(status);
};
checkIsFollowed();
}, [profileData, user]);

const followUser = () => {
addUserFollower(profileData, user, firestore);
};

const getTime = timestamp => {
return timestamp.toDate().toDateString();
};
Expand Down Expand Up @@ -81,7 +99,8 @@ const User = ({ id, timestamp, showFollowButton, size }) => {
{showFollowButton && (
<Button
variant="contained"
disabled
onClick={followUser}
disabled={isFollowed}
sx={{
borderRadius: "50px",
height: "20px",
Expand Down
2 changes: 1 addition & 1 deletion src/store/actions/profileActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ export const clearUserProfile = () => dispatch => {
dispatch({ type: actions.CLEAR_USER_PROFILE_DATA_STATE });
};

const isUserFollower = async (followerId, followingId, firestore) => {
export const isUserFollower = async (followerId, followingId, firestore) => {
const followerDoc = await firestore
.collection("user_followers")
.doc(`${followingId}_${followerId}`)
Expand Down
82 changes: 80 additions & 2 deletions src/store/actions/tutorialPageActions.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,88 @@
import * as actions from "./actionTypes";

export const getTutorialFeedIdArray =
uid => async (firebase, firestore, dispatch) => {
try {
let followings = [];
if (uid) {
followings = await firestore
.collection("user_followers")
.where("followerId", "==", uid)
.get()
.then(async docs => {
const result = [];
for (const doc of docs.docs) {
const handle = await firestore
.collection("cl_user")
.doc(doc.data().followingId)
.get()
.then(doc => doc.data().handle);

result.push(handle);
}
return result;
});
}
let followingUsersTutorials = [];
if (followings.length > 0) {
followingUsersTutorials = await firestore
.collection("tutorials")
.where("created_by", "in", followings)
.limit(50)
.get()
.then(docs => {
const tutorialsArray = [];
docs.docs.map(doc => {
const tutorialId = doc.id;
tutorialsArray.push(tutorialId);
});
return tutorialsArray;
});
}
followings.push(uid);
let newTutorials = [];
if (uid) {
newTutorials = await firestore
.collection("tutorials")
.where("created_by", "not-in", followings)
.limit(50)
.get()
.then(docs => {
const tutorialsArray = [];
docs.docs.map(doc => {
const tutorialId = doc.id;
tutorialsArray.push(tutorialId);
});
return tutorialsArray;
});
} else {
newTutorials = await firestore
.collection("tutorials")
.limit(50)
.get()
.then(docs => {
const tutorialsArray = [];
docs.docs.map(doc => {
const tutorialId = doc.id;
tutorialsArray.push(tutorialId);
});
return tutorialsArray;
});
}

const tutorials = followingUsersTutorials.concat(newTutorials);

return tutorials;
} catch (e) {
console.log(e);
}
};

export const getTutorialFeedData =
tutorialIdArray => async (firestore, firebase, dispatch) => {
tutorialIdArray => async (firebase, firestore, dispatch) => {
try {
dispatch({ type: actions.GET_TUTORIAL_FEED_START });
const tutorials = await firebase
const tutorials = await firestore
.collection("tutorials")
.where("tutorial_id", "in", tutorialIdArray)
.get();
Expand Down
1 change: 1 addition & 0 deletions src/store/actions/tutorialsActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ export const createTutorial =
owner,
summary,
title,
tutorial_id: documentID,
featured_image: "",
icon: "",
url: "",
Expand Down

0 comments on commit d8a3465

Please sign in to comment.