aboutsummaryrefslogtreecommitdiff
path: root/src/components/PollsList/PollsList.tsx
blob: c95bfdefd3ff1d04062dbbdb36e79a230c81f7f6 (plain)
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
import React from 'react';
import { Poll } from 'which-types';
import { WindowScroller, AutoSizer, List } from 'react-virtualized';
import PollCard from '../PollCard/PollCard';


interface PropTypes {
  polls: Poll[];
  mutate: (polls: Poll[], refetch: boolean) => void;
}

interface RenderPropTypes {
  index: number;
  key: string;
  style: React.CSSProperties;
}


const PollsList: React.FC<PropTypes> = ({ polls, mutate }) => {
  const RenderItem: React.FC<RenderPropTypes> = ({ index, style, key }) => {
    const poll = polls[index];

    const setPoll = (newPoll: Poll) => {
      const newPolls = [...polls];
      newPolls[index] = newPoll;

      // Force-update list-size so everything re-renders
      mutate([], false);
      mutate(newPolls, false);
    };

    return (
      // To re-render on list resize, add this info to key
      <div key={`${key}-${poll._id}-${polls.length}`} style={style}>
        <PollCard poll={poll} setPoll={setPoll} />
      </div>
    );
  };

  return (
    <WindowScroller>
      {({
        height,
        isScrolling,
        registerChild,
        onChildScroll,
        scrollTop
      }) => (
        <AutoSizer disableHeight>
          {({ width }) => (
            <div ref={registerChild}>
              <List
                autoHeight
                height={height}
                isScrolling={isScrolling}
                onScroll={onChildScroll}
                rowCount={polls.length}
                rowHeight={550}
                rowRenderer={RenderItem}
                scrollTop={scrollTop}
                width={width}
                containerStyle={{ pointerEvents: 'auto' }}
                overscanRowCount={1}
              />
            </div>
          )}
        </AutoSizer>
      )}
    </WindowScroller>
  );
};

export default PollsList;