File size: 3,394 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
// @flow
import { graphql } from 'react-apollo';
import gql from 'graphql-tag';
import threadInfoFragment from '../../fragments/thread/threadInfo';
import communityInfoFragment from '../../fragments/community/communityInfo';
import type { CommunityInfoType } from '../../fragments/community/communityInfo';
import communityThreadConnectionFragment from '../../fragments/community/communityThreadConnection';
import type { CommunityThreadConnectionType } from '../../fragments/community/communityThreadConnection';

export type GetCommunityThreadConnectionType = {
  ...$Exact<CommunityInfoType>,
  ...$Exact<CommunityThreadConnectionType>,
};

const LoadMoreThreads = gql`
  query loadMoreCommunityThreads(
    $after: String
    $id: ID
    $sort: CommunityThreadConnectionSort
  ) {
    community(id: $id) {
      ...communityInfo
      ...communityThreadConnection
    }
  }
  ${threadInfoFragment}
  ${communityInfoFragment}
  ${communityThreadConnectionFragment}
`;

export const getCommunityThreadConnectionQuery = gql`
  query getCommunityThreadConnection(
    $id: ID
    $after: String
    $sort: CommunityThreadConnectionSort
  ) {
    community(id: $id) {
      ...communityInfo
      ...communityThreadConnection
    }
  }
  ${threadInfoFragment}
  ${communityInfoFragment}
  ${communityThreadConnectionFragment}
`;

const getCommunityThreadConnectionOptions = {
  props: ({
    ownProps,
    data: { fetchMore, error, loading, community, networkStatus, refetch },
  }) => ({
    data: {
      error,
      loading,
      networkStatus,
      community,
      refetch,
      threadConnection: community && community.threadConnection,
      threads:
        community && community.threadConnection
          ? community.threadConnection.edges
          : [],
      hasNextPage:
        community && community.threadConnection
          ? community.threadConnection.pageInfo.hasNextPage
          : false,
      feed: community && community.id,
      fetchMore: () =>
        fetchMore({
          query: LoadMoreThreads,
          variables: {
            after:
              community.threadConnection.edges[
                community.threadConnection.edges.length - 1
              ].cursor,
            id: community.id,
          },
          updateQuery: (prev, { fetchMoreResult }) => {
            if (!fetchMoreResult.community) {
              return prev;
            }
            return {
              ...prev,
              community: {
                ...prev.community,
                threadConnection: {
                  ...prev.community.threadConnection,
                  pageInfo: {
                    ...prev.community.threadConnection.pageInfo,
                    ...fetchMoreResult.community.threadConnection.pageInfo,
                  },
                  edges: [
                    ...prev.community.threadConnection.edges,
                    ...fetchMoreResult.community.threadConnection.edges,
                  ],
                },
              },
            };
          },
        }),
    },
  }),
  options: ({
    id,
    after,
    sort,
  }: {
    id: string,
    after?: ?string,
    sort?: ?string,
  }) => ({
    variables: {
      id,
      after: after || null,
      sort,
    },
    fetchPolicy: 'cache-and-network',
  }),
};

export default graphql(
  getCommunityThreadConnectionQuery,
  getCommunityThreadConnectionOptions
);