Skip to content

Commit

Permalink
Merge pull request #3150 from fettuccinae/LB-1726
Browse files Browse the repository at this point in the history
LB-1726: Fixed following status glitching when followed from similar pane
  • Loading branch information
anshg1214 authored Jan 28, 2025
2 parents 4e25412 + ac106f1 commit 46374f7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 18 deletions.
42 changes: 28 additions & 14 deletions frontend/js/src/user/components/follow/UserSocialNetwork.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -254,22 +254,36 @@ export default class UserSocialNetwork extends React.Component<
) => {
const { currentUser } = this.context;
const { user: profileUser } = this.props;
const { followingList } = this.state;
const newFollowingList = [...followingList];
const index = newFollowingList.findIndex(
(following) => following === user.name
);
if (action === "follow" && index === -1) {
newFollowingList.push(user.name);
const { followingList, currentUserFollowingList } = this.state;

if (!currentUser) return;

// update the logged-in user's following list (for similar users pane)
const newCurrentUserFollowingList = [...currentUserFollowingList];
const currentUserIndex = newCurrentUserFollowingList.indexOf(user.name);

if (action === "follow" && currentUserIndex === -1) {
newCurrentUserFollowingList.push(user.name);
} else if (action === "unfollow" && currentUserIndex !== -1) {
newCurrentUserFollowingList.splice(currentUserIndex, 1);
}
if (
action === "unfollow" &&
index !== -1 &&
profileUser.name === currentUser?.name
) {
newFollowingList.splice(index, 1);

// update the users following list (for followers/following pane)
const newFollowingList = [...followingList];
if (profileUser.name === currentUser.name) {
const profileUserIndex = newFollowingList.indexOf(user.name);
if (action === "follow" && profileUserIndex === -1) {
newFollowingList.push(user.name);
} else if (action === "unfollow" && profileUserIndex !== -1) {
newFollowingList.splice(profileUserIndex, 1);
}
}
this.setState({ followingList: newFollowingList });

// Update both lists in state
this.setState({
followingList: newFollowingList,
currentUserFollowingList: newCurrentUserFollowingList,
});
};

render() {
Expand Down
8 changes: 4 additions & 4 deletions frontend/js/tests/user/follow/UserSocialNetwork.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,11 @@ describe("<UserSocialNetwork />", () => {
});

// initial state after first fetch
expect(instance.state.followingList).toEqual(["jack", "fnord"]);
expect(instance.state.currentUserFollowingList).toEqual(["jack", "fnord"]);
await act(async () => {
instance.updateFollowingList({ name: "Baldur" }, "follow");
});
expect(instance.state.followingList).toEqual(["jack", "fnord", "Baldur"]);
expect(instance.state.currentUserFollowingList).toEqual(["jack", "fnord", "Baldur"]);
});

it("updates the state when called with action unfollow", async () => {
Expand Down Expand Up @@ -249,13 +249,13 @@ describe("<UserSocialNetwork />", () => {
await act(async () => {
instance.updateFollowingList({ name: "Baldur" }, "follow");
});
expect(instance.state.followingList).toEqual(["jack", "fnord", "Baldur"]);
expect(instance.state.currentUserFollowingList).toEqual(["jack", "fnord", "Baldur"]);

// Ensure we can't add a user twice
await act(async () => {
instance.updateFollowingList({ name: "Baldur" }, "follow");
});
expect(instance.state.followingList).toEqual(["jack", "fnord", "Baldur"]);
expect(instance.state.currentUserFollowingList).toEqual(["jack", "fnord", "Baldur"]);
});

it("does nothing when trying to unfollow a user that is not followed", async () => {
Expand Down

0 comments on commit 46374f7

Please sign in to comment.