DescriptorInfiniteLoader.tsx 2.61 KB
Newer Older
Valeriy Panov's avatar
Valeriy Panov committed
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
import * as React from 'react';
import {InfiniteLoader, AutoSizer, List} from 'react-virtualized';

import DescriptorCard from 'ui/catalog/descriptor/DescriptorCard';
import {Page} from 'model/common.model';
import {Descriptor, IDescriptorFilter} from 'model/descriptor.model';

interface IDescriptorInfiniteLoaderProps extends React.ClassAttributes<any> {
    filter: any;
    loading: boolean;
    paged: Page<Descriptor>;
    list: Descriptor[];
    loadDescriptors: (page?: number, results?: number, sortBy?: string, filter?: IDescriptorFilter) => void;
}

class DescriptorInfiniteLoader extends React.PureComponent<IDescriptorInfiniteLoaderProps, any> {

    protected loadNextPage = () => {
        const {filter, loadDescriptors, paged} = this.props;
        loadDescriptors(paged.number + 1, 20, null, filter);
    }

    private isRowLoaded = ({ index }) => {
        const {list, paged} = this.props;
        return paged.last || index < list.length;
    }

    private rowRenderer = ({ index, key, style }) => {
        const {list} = this.props;

        return this.isRowLoaded({ index }) && (
            <div key={ key } style={ style }>
                <DescriptorCard
                    className="mb-20"
                    descriptor={ list[index] }
                    complete
                    dataType
                    vocabulary
                />
            </div>
        );
    }

    protected loadMoreRows = () => {
        const {loading} = this.props;

        if (!loading) {
            this.loadNextPage();
        }
    }

    public render() {
        const {list, paged} = this.props;

        const rowCount = paged.last ? list.length : list.length + 1;

        return (
            <InfiniteLoader
                isRowLoaded={ this.isRowLoaded }
                loadMoreRows={ this.loadMoreRows }
                rowCount={ rowCount }
            >
                { ({ onRowsRendered, registerChild }) => (
                    <AutoSizer>
                        { ({ height, width }) => (
                            <List
                                ref={ registerChild }
                                height={ height || 800 }
                                onRowsRendered={ onRowsRendered }
                                rowCount={ rowCount }
                                rowHeight={ 330 }
                                rowRenderer={ this.rowRenderer }
                                width={ width }
                            />
                        ) }
                    </AutoSizer>
                ) }
            </InfiniteLoader>
        );
    }
}

export default DescriptorInfiniteLoader;