-
Notifications
You must be signed in to change notification settings - Fork 4.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix #78 - Implement server side pagination and sorting for queries #2566
Changes from all commits
c41b90a
a524808
45416e6
28864a6
3632705
73310b1
ae03700
eb3b3dd
16dfcb3
ad644ef
6f2714d
a50f8e7
6be187c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,23 @@ | ||
import moment from 'moment'; | ||
import { isString } from 'underscore'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the React pull request gets merged before this one, we need to remember to replace this with |
||
import startsWith from 'underscore.string/startsWith'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm, I thought it's not supported by IE11? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thinking about IE11 is very benevolent of you :) But I would assume Babel takes care of this for us? |
||
|
||
import { LivePaginator } from '@/lib/pagination'; | ||
import template from './queries-list.html'; | ||
|
||
|
||
class QueriesListCtrl { | ||
constructor($location, Title, Query) { | ||
constructor($location, $log, $route, Title, Query) { | ||
const page = parseInt($location.search().page || 1, 10); | ||
|
||
const orderSeparator = '-'; | ||
const pageOrder = $location.search().order || 'created_at'; | ||
const pageOrderReverse = startsWith(pageOrder, orderSeparator); | ||
this.showEmptyState = false; | ||
this.showDrafts = false; | ||
this.pageSize = parseInt($location.search().page_size || 20, 10); | ||
this.pageSizeOptions = [5, 10, 20, 50, 100]; | ||
this.searchTerm = $location.search().search || ''; | ||
this.oldSearchTerm = $location.search().q; | ||
this.defaultOptions = {}; | ||
|
||
const self = this; | ||
|
@@ -20,16 +31,42 @@ class QueriesListCtrl { | |
Title.set('My Queries'); | ||
this.resource = Query.myQueries; | ||
break; | ||
// Redirect to the real search view. | ||
// TODO: check if that really always works. | ||
case '/queries/search': | ||
window.location.replace('/queries?q=' + this.oldSearchTerm); | ||
break; | ||
default: | ||
break; | ||
} | ||
|
||
function queriesFetcher(requestedPage, itemsPerPage, paginator) { | ||
const setSearchOrClear = (name, value) => { | ||
if (value) { | ||
$location.search(name, value); | ||
} else { | ||
$location.search(name, undefined); | ||
} | ||
}; | ||
|
||
function queriesFetcher(requestedPage, itemsPerPage, orderByField, orderByReverse, params, paginator) { | ||
$location.search('page', requestedPage); | ||
$location.search('page_size', itemsPerPage); | ||
if (orderByReverse && !startsWith(orderByField, orderSeparator)) { | ||
orderByField = orderSeparator + orderByField; | ||
} | ||
setSearchOrClear('order', orderByField); | ||
setSearchOrClear('search', params.searchTerm); | ||
setSearchOrClear('drafts', params.showDrafts); | ||
|
||
const request = Object.assign( | ||
{}, self.defaultOptions, | ||
{ page: requestedPage, page_size: itemsPerPage }, | ||
{ | ||
page: requestedPage, | ||
page_size: itemsPerPage, | ||
order: orderByField, | ||
search: params.searchTerm, | ||
drafts: params.showDrafts, | ||
}, | ||
); | ||
|
||
return self.resource(request).$promise.then((data) => { | ||
|
@@ -43,16 +80,49 @@ class QueriesListCtrl { | |
}); | ||
} | ||
|
||
this.paginator = new LivePaginator(queriesFetcher, { page }); | ||
this.paginator = new LivePaginator( | ||
queriesFetcher, | ||
{ | ||
page, | ||
itemsPerPage: this.pageSize, | ||
orderByField: pageOrder, | ||
orderByReverse: pageOrderReverse, | ||
params: this.parameters, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a bit of a kerfuffle, I'll clean this up while porting this to #2573 |
||
}, | ||
); | ||
|
||
this.parameters = () => ({ | ||
searchTerm: this.searchTerm, | ||
showDrafts: this.showDrafts, | ||
}); | ||
|
||
this.tabs = [ | ||
{ path: 'queries', name: 'All Queries', isActive: path => path === '/queries' }, | ||
{ name: 'My Queries', path: 'queries/my' }, | ||
{ name: 'Search', path: 'queries/search' }, | ||
]; | ||
|
||
this.showList = () => this.paginator.getPageRows() !== undefined && this.paginator.getPageRows().length > 0; | ||
this.showEmptyState = () => this.paginator.getPageRows() !== undefined && this.paginator.getPageRows().length === 0; | ||
this.searchUsed = () => this.searchTerm !== undefined || this.searchTerm !== ''; | ||
|
||
this.hasResults = () => this.paginator.getPageRows() !== undefined && | ||
this.paginator.getPageRows().length > 0; | ||
|
||
this.showEmptyState = () => !this.hasResults() && !this.searchUsed(); | ||
|
||
this.showDraftsCheckbox = () => $location.path() !== '/queries/my'; | ||
|
||
this.clearSearch = () => { | ||
this.searchTerm = ''; | ||
this.update(); | ||
}; | ||
|
||
this.update = () => { | ||
if (!isString(this.searchTerm) || this.searchTerm.trim() === '') { | ||
this.searchTerm = ''; | ||
} | ||
this.paginator.itemsPerPage = this.pageSize; | ||
this.paginator.params = this.parameters(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. any reason not to just inline the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, good idea. |
||
this.paginator.fetch(page); | ||
}; | ||
} | ||
} | ||
|
||
|
@@ -71,5 +141,10 @@ export default function init(ngModule) { | |
template: '<page-queries-list></page-queries-list>', | ||
reloadOnSearch: false, | ||
}, | ||
// just for backward-compatible routes | ||
'/queries/search': { | ||
template: '<page-queries-list></page-queries-list>', | ||
reloadOnSearch: false, | ||
}, | ||
}; | ||
} |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be
...params
instead?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, hm, maybe?