From 4e2f935de4c3ae152582537b08911505cb5dc38a Mon Sep 17 00:00:00 2001 From: noumantahir Date: Wed, 13 Apr 2022 06:00:24 +0500 Subject: [PATCH] cleaned logic for checking pin elgibility --- .../container/postDropdownContainer.tsx | 31 +++++++++---------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/src/components/postDropdown/container/postDropdownContainer.tsx b/src/components/postDropdown/container/postDropdownContainer.tsx index 4c285964c..8c4544f17 100644 --- a/src/components/postDropdown/container/postDropdownContainer.tsx +++ b/src/components/postDropdown/container/postDropdownContainer.tsx @@ -35,7 +35,7 @@ class PostDropdownContainer extends PureComponent { super(props); this.state = { - options:OPTIONS + options: OPTIONS }; } @@ -67,28 +67,25 @@ class PostDropdownContainer extends PureComponent { } }; - _initOptions = ({content, currentAccount, pageType, subscribedCommunities} = this.props) => { + _initOptions = ({ content, currentAccount, pageType, subscribedCommunities } = this.props) => { //check if post is owned by current user or not, if so pinned or not const _canUpdateBlogPin = !!pageType && !!content && !!currentAccount && currentAccount.name === content.author const _isPinnedInProfile = !!content && content.stats?.is_pinned_blog; - //get user role for community; - const defaultRole = 'guest'; - const _userCommunitiyRole = subscribedCommunities.data && !!content && content.community ? subscribedCommunities.data.reduce((role, subscription)=>{ - if(content.community === subscription[0]){ - return subscription[2]; - } - return role; - }, defaultRole) : defaultRole; //check community pin update eligibility - const _canUpdateCommunityPin = !!content && content.community - && ['owner', 'admin', 'mod'].includes(_userCommunitiyRole); + const _canUpdateCommunityPin = subscribedCommunities.data && !!content && content.community + ? subscribedCommunities.data.reduce((role, subscription) => { + if (content.community === subscription[0]) { + return ['owner', 'admin', 'mod'].includes(subscription[2]); + } + return role; + }, false) : false; const _isPinnedInCommunity = !!content && content.stats?.is_pinned; //cook options list based on collected flags - const options = OPTIONS.filter((option)=>{ - switch(option){ + const options = OPTIONS.filter((option) => { + switch (option) { case 'pin-blog': return _canUpdateBlogPin && !_isPinnedInProfile; case 'unpin-blog': @@ -168,7 +165,7 @@ class PostDropdownContainer extends PureComponent { this._updatePinnedPostCommunity(); break; case 'unpin-community': - this._updatePinnedPostCommunity({unpinPost:true}); + this._updatePinnedPostCommunity({ unpinPost: true }); break; default: break; @@ -324,7 +321,7 @@ class PostDropdownContainer extends PureComponent { await pinCommunityPost(currentAccount, pinCode, content.community, content.author, content.permlink, unpinPost); dispatch(toastNotification(intl.formatMessage({ id: 'alert.successful' }))); - }catch(err){ + } catch (err) { console.warn("Failed to update pin status of community post", err); Alert.alert( intl.formatMessage({ @@ -333,7 +330,7 @@ class PostDropdownContainer extends PureComponent { get(err, 'message', err.toString()), ); } - + } _redirectToReply = () => {