Changeset View
Changeset View
Standalone View
Standalone View
lib/selectors/relationship-selectors.js
Show All 13 Lines | const userRelationshipsSelector: (state: BaseAppState<*>) => UserRelationships = | ||||
createSelector( | createSelector( | ||||
(state: BaseAppState<*>) => state.userStore.userInfos, | (state: BaseAppState<*>) => state.userStore.userInfos, | ||||
(userInfos: UserInfos) => { | (userInfos: UserInfos) => { | ||||
const unorderedFriendRequests = []; | const unorderedFriendRequests = []; | ||||
const unorderedFriends = []; | const unorderedFriends = []; | ||||
const blocked = []; | const blocked = []; | ||||
for (const userID in userInfos) { | for (const userID in userInfos) { | ||||
const userInfo = userInfos[userID]; | const userInfo = userInfos[userID]; | ||||
const { id, username, avatar, relationshipStatus } = userInfo; | const { id, username, relationshipStatus } = userInfo; | ||||
if (!username) { | if (!username) { | ||||
continue; | continue; | ||||
} | } | ||||
if ( | if ( | ||||
relationshipStatus === userRelationshipStatus.REQUEST_RECEIVED || | relationshipStatus === userRelationshipStatus.REQUEST_RECEIVED || | ||||
relationshipStatus === userRelationshipStatus.REQUEST_SENT | relationshipStatus === userRelationshipStatus.REQUEST_SENT | ||||
) { | ) { | ||||
unorderedFriendRequests.push({ | unorderedFriendRequests.push({ id, username, relationshipStatus }); | ||||
id, | |||||
username, | |||||
avatar, | |||||
relationshipStatus, | |||||
}); | |||||
} else if (relationshipStatus === userRelationshipStatus.FRIEND) { | } else if (relationshipStatus === userRelationshipStatus.FRIEND) { | ||||
unorderedFriends.push({ id, username, avatar, relationshipStatus }); | unorderedFriends.push({ id, username, relationshipStatus }); | ||||
} else if ( | } else if ( | ||||
relationshipStatus === userRelationshipStatus.BLOCKED_BY_VIEWER || | relationshipStatus === userRelationshipStatus.BLOCKED_BY_VIEWER || | ||||
relationshipStatus === userRelationshipStatus.BOTH_BLOCKED | relationshipStatus === userRelationshipStatus.BOTH_BLOCKED | ||||
) { | ) { | ||||
blocked.push({ id, username, avatar, relationshipStatus }); | blocked.push({ id, username, relationshipStatus }); | ||||
} | } | ||||
} | } | ||||
const friendRequests = _orderBy('relationshipStatus')('desc')( | const friendRequests = _orderBy('relationshipStatus')('desc')( | ||||
unorderedFriendRequests, | unorderedFriendRequests, | ||||
); | ); | ||||
const friends = friendRequests.concat(unorderedFriends); | const friends = friendRequests.concat(unorderedFriends); | ||||
return { friends, blocked }; | return { friends, blocked }; | ||||
}, | }, | ||||
); | ); | ||||
export { userRelationshipsSelector }; | export { userRelationshipsSelector }; |