aboutsummaryrefslogtreecommitdiff
path: root/src/components/Feed/Feed.tsx
blob: 03358da1e77710e0179815e6f83b6976b5d134ec (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
75
76
77
78
79
80
81
import React from 'react';
import { Poll } from 'which-types';
import { WindowScroller, AutoSizer, List } from 'react-virtualized';
import CircularProgress from '@material-ui/core/CircularProgress';
import { makeStyles } from '@material-ui/core';
import PollCard from '../PollCard/PollCard';

interface PropTypes {
  polls: Poll[];
}

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

const useStyles = makeStyles(theme => ({
  loader: {
    width: '100%',
    textAlign: 'center',
    marginTop: theme.spacing(10)
  }
}));

const Feed: React.FC<PropTypes> = ({ polls }) => {
  const classes = useStyles();


  const RenderItem: React.FC<RenderPropTypes> = ({ index, style, key }) => {
    const poll = polls[index];
    return (
      <div key={key} style={style}>
        <PollCard initialPoll={poll} />
      </div>
    );
  };

  const loader = (
    <div className={classes.loader}>
      <CircularProgress color="primary" style={{ margin: '0 auto' }} />
    </div>
  );

  const list = (
    <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>
  );

  return polls.length ? list : loader;
};

export default Feed;