0

I am already working on Pagination.

I used PaginationContainer for that. It work’s but no way what I am looking for. I got button next which call props.relay.loadMore(2) function. So when I click on this button it will call query and add me 2 more items to list. It works like load more. But I would like instead of add these two new items to list, replace the old item with new.

I try to use this getFragmentVariables for modifying variables for reading from the store but it’s not working.

Have somebody Idea or implemented something similar before?

class QueuesBookingsList extends Component {
  props: Props;

  handleLoadMore = () => {
    const { hasMore, isLoading, loadMore } = this.props.relay;
    console.log('hasMore', hasMore());
    if (!hasMore() || isLoading()) {
      return;
    }
    this.setState({ isLoading });
    loadMore(1, () => {
      this.setState({ isLoading: false });
    });
  };

  getItems = () => {
    const edges = idx(this.props, _ => _.data.queuesBookings.edges) || [];
    return edges.map(edge => edge && edge.node);
  };

  getItemUrl = ({ bid }: { bid: number }) => getDetailUrlWithId(BOOKING, bid);

  render() {
    return (
      <div>
        <button onClick={this.handleLoadMore}>TEST</button>
        <GenericList
          displayValue={'bid'}
          items={this.getItems()}
          itemUrl={this.getItemUrl}
          emptyText="No matching booking found"
        />
      </div>
    );
  }
}

export default createPaginationContainer(
  QueuesBookingsList,
  {
    data: graphql`
      fragment QueuesBookingsList_data on RootQuery {
        queuesBookings(first: $count, after: $after, queueId: $queueId)
          @connection(
            key: "QueuesBookingsList_queuesBookings"
            filters: ["queueId"]
          ) {
          edges {
            cursor
            node {
              id
              bid
              url
            }
          }
          pageInfo {
            endCursor
            hasNextPage
          }
        }
      }
    `,
  },
  {
    direction: 'forward',
    query: graphql`
      query QueuesBookingsListQuery(
        $count: Int!
        $after: String
        $queueId: ID
      ) {
        ...QueuesBookingsList_data
      }
    `,
    getConnectionFromProps(props) {
      return props.data && props.data.queuesBookings;
    },
    getFragmentVariables(prevVars, totalCount) {
      console.log({ prevVars });
      return {
        ...prevVars,
        count: totalCount,
      };
    },
    getVariables(props, variables, fragmentVariables) {
      return {
        count: variables.count,
        after: variables.cursor,
        queueId: fragmentVariables.queueId,
      };
    },
  },
);
Grund
  • 309
  • 1
  • 2
  • 12

1 Answers1

0

As I figure out, there are two solutions, use refechConnection method for Pagination Container or use Refech Container.

Grund
  • 309
  • 1
  • 2
  • 12