Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 105 106 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import Icon from 'antd/lib/icon'; import { DocumentNode } from 'graphql'; import { max } from 'lodash'; import * as React from 'react'; import { Query } from 'react-apollo'; import styled from 'styled-components'; import { useBoolean } from '../../customHooks'; import Button from './Button'; import Spin from './Spin'; const ButtonWrapper = styled.div` padding: 20px; text-align: center; `; const limit = 5; interface IProps { size: number; query: DocumentNode; queryVariables: any; listComponent: React.JSXElementConstructor<any>; } const LoadMoreContainer = ({ query, queryVariables, listComponent: ListComponent, size, }: IProps) => { // pagination const hasMore = useBoolean(size > limit); const fetchMoreVariables = (start: number) => ({ ...queryVariables, limit, start, }); return ( <Query query={query} variables={{ limit, ...queryVariables }}> {({ loading, error, data, fetchMore }) => { if (error) { console.log(error); return null; // TODO: show error } const loadMore = () => { const lengths = Object.values(data).map((a: []) => a.length); const offset = max(lengths || []) || 0; if (offset >= size) { hasMore.setFalse(); } fetchMore({ query, updateQuery: (prev, { fetchMoreResult }) => { const moreResultLengths = Object.values(fetchMoreResult).map( (a: []) => a.length, ); const moreResultOffset = max(moreResultLengths || []) || 0; if (moreResultOffset < limit) { hasMore.setFalse(); } if (moreResultOffset === 0) { return prev; } const result = {}; Object.keys(prev).forEach((key: string) => { result[key] = [...prev[key], ...fetchMoreResult[key]]; }); return result; }, variables: fetchMoreVariables(offset), }); }; return ( <React.Fragment> {loading && <Spin size={15} />} {!loading && ( <React.Fragment> <ListComponent dataSource={data} /> {hasMore.value && ( <ButtonWrapper> <Button onClick={loadMore}> <Icon type="down" style={{ marginRight: '5px' }} /> Zobrazit vĂce </Button> </ButtonWrapper> )} </React.Fragment> )} </React.Fragment> ); }} </Query> ); }; export default LoadMoreContainer; |