diff --git a/src/dashboard/Data/Browser/Browser.react.js b/src/dashboard/Data/Browser/Browser.react.js index 9f9702261..2eeab2aa0 100644 --- a/src/dashboard/Data/Browser/Browser.react.js +++ b/src/dashboard/Data/Browser/Browser.react.js @@ -84,6 +84,7 @@ class Browser extends DashboardView { lastMax: -1, newObject: null, editCloneRows: null, + fetchingNextPage: false, lastError: null, lastNote: null, @@ -975,53 +976,21 @@ class Browser extends DashboardView { } async fetchNextPage() { - if (!this.state.data || this.state.isUnique) { + if (!this.state.data || this.state.isUnique || this.state.fetchingNextPage) { return null; } + this.setState({ fetchingNextPage: true }); const className = this.props.params.className; const source = this.state.relation || className; let query = await queryFromFilters(source, this.state.filters); - if (this.state.ordering !== '-createdAt') { - // Construct complex pagination query - const equalityQuery = queryFromFilters(source, this.state.filters); - let field = this.state.ordering; - let ascending = true; - let comp = this.state.data[this.state.data.length - 1].get(field); - if (field === 'objectId' || field === '-objectId') { - comp = this.state.data[this.state.data.length - 1].id; - } - if (field[0] === '-') { - field = field.substr(1); - query.lessThan(field, comp); - ascending = false; - } else { - query.greaterThan(field, comp); - } - if (field === 'createdAt') { - equalityQuery.greaterThan( - 'createdAt', - this.state.data[this.state.data.length - 1].get('createdAt') - ); - } else { - equalityQuery.lessThan( - 'createdAt', - this.state.data[this.state.data.length - 1].get('createdAt') - ); - equalityQuery.equalTo(field, comp); - } - query = Parse.Query.or(query, equalityQuery); - if (ascending) { - query.ascending(this.state.ordering); - } else { - query.descending(this.state.ordering.substr(1)); - } + if (this.state.ordering[0] === '-') { + query.descending(this.state.ordering.substr(1)); } else { - query.lessThan('createdAt', this.state.data[this.state.data.length - 1].get('createdAt')); - query.addDescending('createdAt'); + query.ascending(this.state.ordering); } + query.skip(this.state.lastMax); query.limit(MAX_ROWS_FETCHED); this.excludeFields(query, source); - const { useMasterKey } = this.state; query.find({ useMasterKey }).then(nextPage => { if (className === this.props.params.className) { @@ -1029,6 +998,10 @@ class Browser extends DashboardView { data: state.data.concat(nextPage), })); } + }).catch(() => { + this.setState({ lastMax: this.state.lastMax - MAX_ROWS_FETCHED }); + }).finally(() => { + this.setState({ fetchingNextPage: false }); }); this.setState({ lastMax: this.state.lastMax + MAX_ROWS_FETCHED }); } diff --git a/src/dashboard/Data/Browser/BrowserTable.react.js b/src/dashboard/Data/Browser/BrowserTable.react.js index 59dc53d5d..6212bae7d 100644 --- a/src/dashboard/Data/Browser/BrowserTable.react.js +++ b/src/dashboard/Data/Browser/BrowserTable.react.js @@ -35,7 +35,7 @@ export default class BrowserTable extends React.Component { maxWidth: window.innerWidth - 300, }; this.handleScroll = this.handleScroll.bind(this); - this.tableRef = React.createRef(); + this.scrollRef = React.createRef(); this.handleResize = this.handleResize.bind(this); this.updateMaxWidth = this.updateMaxWidth.bind(this); } @@ -45,27 +45,27 @@ export default class BrowserTable extends React.Component { this.setState({ offset: 0, }); - this.tableRef.current.scrollTop = 0; + this.scrollRef.current.scrollTop = 0; } else if (this.props.newObject !== props.newObject) { this.setState({ offset: 0 }); - this.tableRef.current.scrollTop = 0; + this.scrollRef.current.scrollTop = 0; } else if (this.props.ordering !== props.ordering) { this.setState({ offset: 0 }); - this.tableRef.current.scrollTop = 0; + this.scrollRef.current.scrollTop = 0; } else if (this.props.filters.size !== props.filters.size) { this.setState({ offset: 0 }, () => { - this.tableRef.current.scrollTop = 0; + this.scrollRef.current.scrollTop = 0; }); } } componentDidMount() { - this.tableRef.current.addEventListener('scroll', this.handleScroll); + this.scrollRef.current.addEventListener('scroll', this.handleScroll); window.addEventListener('resize', this.updateMaxWidth); } componentWillUnmount() { - this.tableRef.current.removeEventListener('scroll', this.handleScroll); + this.scrollRef.current.removeEventListener('scroll', this.handleScroll); window.removeEventListener('resize', this.updateMaxWidth); } @@ -98,7 +98,7 @@ export default class BrowserTable extends React.Component { return; } requestAnimationFrame(() => { - const currentScrollTop = this.tableRef.current.scrollTop; + const currentScrollTop = this.scrollRef.current.scrollTop; let rowsAbove = Math.floor(currentScrollTop / ROW_HEIGHT); let offset = this.state.offset; const currentRow = rowsAbove - this.state.offset; @@ -116,7 +116,7 @@ export default class BrowserTable extends React.Component { } if (this.state.offset !== offset) { this.setState({ offset }); - this.tableRef.current.scrollTop = currentScrollTop; + this.scrollRef.current.scrollTop = currentScrollTop; } if (this.props.maxFetched - offset <= ROWS_OFFSET * 1.4) { this.props.fetchNextPage(); @@ -154,7 +154,7 @@ export default class BrowserTable extends React.Component { required, })); let editor = null; - let table =
; + let table =
; if (this.props.data) { const rowWidth = this.props.order.reduce( (rowWidth, { visible, width }) => (visible ? rowWidth + width : rowWidth), @@ -518,7 +518,7 @@ export default class BrowserTable extends React.Component { if (this.props.newObject || this.props.data.length > 0) { table = ( -
+
{editCloneRows} {newRow} @@ -537,7 +537,7 @@ export default class BrowserTable extends React.Component { ); } else { table = ( -
+
{this.props.relation ? (