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

Remove redundant, stringly-typed fields from PmConversationData #4116

Closed
Closed
Changes from 1 commit
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
Next Next commit
pm UI: Convert GroupPmConversationItem to use users.
Remove its dependence on the old stringly-typed fields.

The existing code has relied on the selector to munge the data for UI
presentation; this now becomes the responsibility of the UI component
itself. (The logic to do so is presently inline, but is expected to be
unified with existing related logic in `recipient.js` later.)
rk-for-zulip committed May 20, 2020
commit 7cc45cd57bb01e398c77f797112bfc55c50c1d9f
21 changes: 6 additions & 15 deletions src/pm-conversations/GroupPmConversationItem.js
Original file line number Diff line number Diff line change
@@ -15,33 +15,24 @@ const componentStyles = StyleSheet.create({
});

type Props = $ReadOnly<{|
email: string,
usersByEmail: Map<string, UserOrBot>,
users: UserOrBot[],
unreadCount: number,
onPress: (emails: string) => void,
onPress: (users: UserOrBot[]) => void,
|}>;

/**
* A list item describing one group PM conversation.
* */
export default class GroupPmConversationItem extends PureComponent<Props> {
handlePress = () => {
const { email, onPress } = this.props;
onPress(email);
const { users, onPress } = this.props;
onPress(users);
};

render() {
const { email, usersByEmail, unreadCount } = this.props;
const allUsers = email.split(',').map(e => usersByEmail.get(e));
const { users, unreadCount } = this.props;

const allUsersFound = allUsers.every(user => user);

if (!allUsersFound) {
return null;
}

// $FlowFixMe Flow doesn't see the `every` check above.
const allNames = allUsers.map(user => user.full_name);
const allNames = users.map(user => user.full_name);

return (
<Touchable onPress={this.handlePress}>
29 changes: 18 additions & 11 deletions src/pm-conversations/PmConversationList.js
Original file line number Diff line number Diff line change
@@ -3,12 +3,13 @@ import React, { PureComponent } from 'react';
import { FlatList, StyleSheet } from 'react-native';

import { connect } from '../react-redux';
import type { Dispatch, PmConversationData, UserOrBot } from '../types';
import type { Dispatch, PmConversationData, User, UserOrBot } from '../types';
import { privateNarrow, groupNarrow } from '../utils/narrow';
import UserItem from '../users/UserItem';
import { getAllUsersByEmail } from '../users/userSelectors';
import { getOwnUser } from '../users/userSelectors';
import GroupPmConversationItem from './GroupPmConversationItem';
import { doNarrow } from '../actions';
import { normalizeUsersSansMe } from '../utils/recipient';

const styles = StyleSheet.create({
list: {
@@ -18,7 +19,7 @@ const styles = StyleSheet.create({
});

type SelectorProps = $ReadOnly<{|
usersByEmail: Map<string, UserOrBot>,
ownUser: User,
|}>;

type Props = $ReadOnly<{|
@@ -35,12 +36,17 @@ class PmConversationList extends PureComponent<Props> {
this.props.dispatch(doNarrow(privateNarrow(email)));
};

handleGroupNarrow = (email: string) => {
this.props.dispatch(doNarrow(groupNarrow(email.split(','))));
handleGroupNarrow = (users: UserOrBot[]) => {
const { dispatch, ownUser } = this.props;
const emails = users
.filter(u => u.user_id !== ownUser.user_id)
.map(u => u.email)
.sort();
dispatch(doNarrow(groupNarrow(emails)));
};

render() {
const { conversations, usersByEmail } = this.props;
const { conversations, ownUser } = this.props;

return (
<FlatList
@@ -49,8 +55,10 @@ class PmConversationList extends PureComponent<Props> {
data={conversations}
keyExtractor={item => item.recipients}
renderItem={({ item }) => {
if (item.recipients.indexOf(',') === -1) {
const user = usersByEmail.get(item.recipients);
const users = normalizeUsersSansMe(item.users, ownUser.user_id);

if (users.length === 1) {
const user = users[0];

if (!user) {
return null;
@@ -69,9 +77,8 @@ class PmConversationList extends PureComponent<Props> {

return (
<GroupPmConversationItem
email={item.recipients}
users={users}
unreadCount={item.unread}
usersByEmail={usersByEmail}
onPress={this.handleGroupNarrow}
/>
);
@@ -82,5 +89,5 @@ class PmConversationList extends PureComponent<Props> {
}

export default connect<SelectorProps, _, _>(state => ({
usersByEmail: getAllUsersByEmail(state),
ownUser: getOwnUser(state),
}))(PmConversationList);