Skip to content

Commit

Permalink
refactor: Implement dynamic data rendering for "Who to Follow" sidebar
Browse files Browse the repository at this point in the history
  • Loading branch information
lokeshwar777 committed Aug 22, 2024
1 parent 82736f5 commit 36dd7b3
Showing 1 changed file with 48 additions and 11 deletions.
59 changes: 48 additions & 11 deletions src/components/CardTabs/Users/UserElement.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,49 @@
import React, { useState } from "react";
import React, { useState, useEffect } from "react";
import Box from "@mui/material/Box";
import AddUser from "../../../assets/images/add-user.svg";
import CheckUser from "../../../assets/images/square-check-regular.svg";
import { useFirestore } from "react-redux-firebase";
import { useSelector } from "react-redux";
import {
isUserFollower,
addUserFollower,
removeUserFollower
} from "../../../store/actions";

const UserElement = ({ user, index, useStyles }) => {
const classes = useStyles();
const [icon, setIcon] = useState(true);
const firestore = useFirestore();

const profileData = user;
const currentProfileData = useSelector(
({ firebase: { profile } }) => profile
);
const followerId = currentProfileData.uid;
const followingId = profileData.uid;
const [followed, setFollowed] = useState(false);

const handleFollowToggle = async () => {
if (!followed) {
await addUserFollower(currentProfileData, profileData, firestore);
} else {
await removeUserFollower(currentProfileData, profileData, firestore);
}
setFollowed(!followed);
};

useEffect(() => {
const checkIfFollowing = async () => {
const isFollowing = await isUserFollower(
followerId,
followingId,
firestore
);
setFollowed(isFollowing);
};

checkIfFollowing();
}, [followerId, followingId, firestore]);

return (
<Box
sx={{
Expand Down Expand Up @@ -46,17 +84,16 @@ const UserElement = ({ user, index, useStyles }) => {
</Box>
</Box>
<Box
onClick={() => {
setIcon(false);
}}
onClick={handleFollowToggle}
data-testId={index == 0 ? "UserAdd" : ""}
sx={
icon && {
cursor: "pointer"
}
}
sx={{
cursor: followed ? "default" : "pointer"
}}
>
<img src={icon ? AddUser : CheckUser} />
<img
src={followed ? CheckUser : AddUser}
alt={followed ? "Following" : "Add User"}
/>
</Box>
</Box>
);
Expand Down

0 comments on commit 36dd7b3

Please sign in to comment.