From 951c8afebc434be3518408d4d21c1c2d99afd10b Mon Sep 17 00:00:00 2001 From: SleeplessOne1917 Date: Wed, 27 Sep 2023 21:36:34 +0000 Subject: [PATCH] fix: Fix bug where user could not block person from profile page (#2151) * fix: Fix bug where user could not block person from profile page * fix: Fix linting errors * chore: Replace arrow function with regular function --- src/shared/components/person/profile.tsx | 46 ++++++++++++------------ 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/src/shared/components/person/profile.tsx b/src/shared/components/person/profile.tsx index 6b95a44a..cfff6394 100644 --- a/src/shared/components/person/profile.tsx +++ b/src/shared/components/person/profile.tsx @@ -155,6 +155,16 @@ const Moderates = ({ moderates }: { moderates?: CommunityModeratorView[] }) => const Follows = () => getCommunitiesListing("subscribed", UserService.Instance.myUserInfo?.follows); +function isPersonBlocked(personRes: RequestState) { + 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 @@ -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), }; } } @@ -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() { @@ -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, @@ -791,6 +791,7 @@ export class Profile extends Component< }); if (res.state === "success") { updatePersonBlock(res.data); + this.setState({ personBlocked: res.data.blocked }); } } @@ -826,6 +827,7 @@ export class Profile extends Component< const blockPersonRes = await HttpService.client.blockPerson(form); if (blockPersonRes.state === "success") { updatePersonBlock(blockPersonRes.data); + this.setState({ personBlocked: blockPersonRes.data.blocked }); } }