File size: 2,113 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//@flow
import * as React from 'react';
import { connect } from 'react-redux';
import { withRouter } from 'react-router';
import compose from 'recompose/compose';
import { CommunityListItem } from 'src/components/entities';
import { ErrorBoundary } from 'src/components/error';
import { Loading } from 'src/components/loading';
import { PrimaryOutlineButton } from 'src/components/button';
import { getUserCommunityConnection } from 'shared/graphql/queries/user/getUserCommunityConnection';
import type { GetUserCommunityConnectionType } from 'shared/graphql/queries/user/getUserCommunityConnection';

type Props = {
  data: {
    user: GetUserCommunityConnectionType,
  },
  currentUser: Object,
  user: Object,
};

class CommunityList extends React.Component<Props> {
  render() {
    const { data } = this.props;

    if (data.loading) {
      return <Loading style={{ padding: '32px' }} />;
    }

    if (
      !data.user ||
      !data.user.communityConnection ||
      !data.user.communityConnection.edges ||
      data.user.communityConnection.edges.length === 0
    ) {
      return (
        <div style={{ padding: '16px' }}>
          <PrimaryOutlineButton style={{ flex: '1' }} to={'/explore'}>
            Explore communities
          </PrimaryOutlineButton>
        </div>
      );
    }

    const communities = data.user.communityConnection.edges.map(
      c => c && c.node
    );

    let sortedCommunities = communities;

    if (sortedCommunities[0] && sortedCommunities[0].contextPermissions) {
      sortedCommunities = communities.slice();
    }

    return (
      <div>
        {sortedCommunities.map(community => {
          if (!community) return null;
          return (
            <ErrorBoundary key={community.id}>
              <CommunityListItem
                communityObject={community}
                profilePhoto={community.profilePhoto}
                name={community.name}
              />
            </ErrorBoundary>
          );
        })}
      </div>
    );
  }
}

export default compose(
  withRouter,
  getUserCommunityConnection,
  connect()
)(CommunityList);