File size: 2,934 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
// @flow
import { graphql } from 'react-apollo';
import gql from 'graphql-tag';
import channelInfoFragment from '../../fragments/channel/channelInfo';
import type { ChannelInfoType } from '../../fragments/channel/channelInfo';
import channelThreadConnectionFragment from '../../fragments/channel/channelThreadConnection';
import type { ChannelThreadConnectionType } from '../../fragments/channel/channelThreadConnection';

export type GetChannelThreadConnectionType = {
  ...$Exact<ChannelInfoType>,
  ...$Exact<ChannelThreadConnectionType>,
};

export const getChannelThreadConnectionQuery = gql`
  query getChannelThreadConnection($id: ID, $after: String) {
    channel(id: $id) {
      ...channelInfo
      ...channelThreadConnection
    }
  }
  ${channelInfoFragment}
  ${channelThreadConnectionFragment}
`;

const LoadMoreThreads = gql`
  query loadMoreChannelThreads($id: ID, $after: String) {
    channel(id: $id) {
      ...channelInfo
      ...channelThreadConnection
    }
  }
  ${channelInfoFragment}
  ${channelThreadConnectionFragment}
`;

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

export default graphql(
  getChannelThreadConnectionQuery,
  getChannelThreadConnectionOptions
);