File size: 4,382 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// @flow
import * as React from 'react';
import styled from 'styled-components';
import { connect } from 'react-redux';
import compose from 'recompose/compose';
import { collections } from './collections';
import viewNetworkHandler from 'src/components/viewNetworkHandler';
import { withCurrentUser } from 'src/components/withCurrentUser';
import {
  ListWithTitle,
  ListTitle,
  ListWrapper,
  Collections,
  CollectionWrapper,
  ProfileCardWrapper,
} from './style';
import { getCommunitiesBySlug } from 'shared/graphql/queries/community/getCommunities';
import type { GetCommunitiesType } from 'shared/graphql/queries/community/getCommunities';
import { SegmentedControl, Segment } from 'src/components/segmentedControl';
import { ErrorBoundary } from 'src/components/error';
import { Loading } from 'src/components/loading';
import { ErrorView } from 'src/views/viewHelpers';
import { CommunityProfileCard } from 'src/components/entities';

const ChartGrid = styled.div`
  display: flex;
  flex-direction: column;
  flex: auto;
`;

export const Charts = () => {
  return <ChartGrid>{collections && <CollectionSwitcher />}</ChartGrid>;
};

type State = {
  selectedView: string,
};

class CollectionSwitcher extends React.Component<{}, State> {
  state = {
    selectedView: 'top-communities-by-members',
  };

  parentRef = null;
  ref = null;

  componentDidMount() {
    this.parentRef = document.getElementById('main');
  }

  handleSegmentClick(selectedView) {
    if (this.state.selectedView === selectedView) return;

    return this.setState({ selectedView });
  }

  componentDidUpdate(prevProps, prevState) {
    const currState = this.state;
    if (prevState.selectedView !== currState.selectedView) {
      if (!this.parentRef || !this.ref) return;
      return (this.parentRef.scrollTop = this.ref.offsetTop);
    }
  }

  render() {
    return (
      <Collections ref={el => (this.ref = el)}>
        <SegmentedControl>
          {collections.map((collection, i) => (
            <Segment
              key={i}
              onClick={() =>
                this.handleSegmentClick(collection.curatedContentType)
              }
              isActive={
                collection.curatedContentType === this.state.selectedView
              }
            >
              {collection.title}
            </Segment>
          ))}
        </SegmentedControl>

        <CollectionWrapper>
          {collections.map((collection, index) => {
            const communitySlugs = collection.communities;
            return (
              <div key={index}>
                {collection.curatedContentType === this.state.selectedView && (
                  <Category
                    slugs={communitySlugs}
                    curatedContentType={collection.curatedContentType}
                  />
                )}
              </div>
            );
          })}
        </CollectionWrapper>
      </Collections>
    );
  }
}

type CategoryListProps = {
  title: string,
  currentUser?: Object,
  slugs: Array<string>,
  data: {
    communities?: GetCommunitiesType,
  },
  isLoading: boolean,
};
class CategoryList extends React.Component<CategoryListProps> {
  render() {
    const {
      data: { communities },
      title,
      slugs,
      isLoading,
    } = this.props;

    if (communities) {
      let filteredCommunities = communities;
      if (slugs) {
        filteredCommunities = communities.filter(c => {
          if (!c) return null;
          if (slugs.indexOf(c.slug) > -1) return c;
          return null;
        });
      }

      return (
        <ListWithTitle>
          {title ? <ListTitle>{title}</ListTitle> : null}
          <ListWrapper>
            {filteredCommunities.map(
              (community, i) =>
                community && (
                  <ErrorBoundary key={i}>
                    <ProfileCardWrapper>
                      <CommunityProfileCard community={community} />
                    </ProfileCardWrapper>
                  </ErrorBoundary>
                )
            )}
          </ListWrapper>
        </ListWithTitle>
      );
    }

    if (isLoading) {
      return <Loading style={{ padding: '64px 32px', minHeight: '100vh' }} />;
    }

    return <ErrorView />;
  }
}

export const Category = compose(
  withCurrentUser,
  getCommunitiesBySlug,
  viewNetworkHandler,
  connect()
)(CategoryList);