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: Fix bug where user could not block person from profile page #2151

Merged
merged 3 commits into from
Sep 27, 2023
Merged
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
46 changes: 24 additions & 22 deletions src/shared/components/person/profile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,16 @@ const Moderates = ({ moderates }: { moderates?: CommunityModeratorView[] }) =>
const Follows = () =>
getCommunitiesListing("subscribed", UserService.Instance.myUserInfo?.follows);

function isPersonBlocked(personRes: RequestState<GetPersonDetailsResponse>) {
return (
(personRes.state === "success" &&
UserService.Instance.myUserInfo?.person_blocks.some(
({ target: { id } }) => id === personRes.data.person_view.person.id,
)) ??
false
);
}

export class Profile extends Component<
RouteComponentProps<{ username: string }>,
ProfileState
Expand Down Expand Up @@ -211,10 +221,12 @@ export class Profile extends Component<

// Only fetch the data if coming from another route
if (FirstLoadService.isFirstLoad) {
const personRes = this.isoData.routeData.personResponse;
this.state = {
...this.state,
personRes: this.isoData.routeData.personResponse,
personRes,
isIsomorphic: true,
personBlocked: isPersonBlocked(personRes),
};
}
}
Expand All @@ -234,17 +246,18 @@ export class Profile extends Component<
const { page, sort, view } = getProfileQueryParams();

this.setState({ personRes: { state: "loading" } });
const personRes = await HttpService.client.getPersonDetails({
username: this.props.match.params.username,
sort,
saved_only: view === PersonDetailsView.Saved,
page,
limit: fetchLimit,
});
this.setState({
personRes: await HttpService.client.getPersonDetails({
username: this.props.match.params.username,
sort,
saved_only: view === PersonDetailsView.Saved,
page,
limit: fetchLimit,
}),
personRes,
personBlocked: isPersonBlocked(personRes),
});
restoreScrollPosition(this.context);
this.setPersonBlock();
}

get amCurrentUser() {
Expand All @@ -258,19 +271,6 @@ export class Profile extends Component<
}
}

setPersonBlock() {
const mui = UserService.Instance.myUserInfo;
const res = this.state.personRes;

if (mui && res.state === "success") {
this.setState({
personBlocked: mui.person_blocks.some(
({ target: { id } }) => id === res.data.person_view.person.id,
),
});
}
}

static async fetchInitialData({
client,
path,
Expand Down Expand Up @@ -791,6 +791,7 @@ export class Profile extends Component<
});
if (res.state === "success") {
updatePersonBlock(res.data);
this.setState({ personBlocked: res.data.blocked });
}
}

Expand Down Expand Up @@ -826,6 +827,7 @@ export class Profile extends Component<
const blockPersonRes = await HttpService.client.blockPerson(form);
if (blockPersonRes.state === "success") {
updatePersonBlock(blockPersonRes.data);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having this in state is much cleaner, thx.

this.setState({ personBlocked: blockPersonRes.data.blocked });
}
}

Expand Down