// // Copyright (C) Stanislaw Adaszewski, 2020 // Contact: s.adaszewski@gmail.com // Website: https://adared.ch/wba // License: GNU Affero General Public License, Version 3 // import { h, Component, createRef } from 'preact'; import WBDialog from 'wb-dialog'; import WBTable from 'wb-table'; import WBPagination from 'wb-pagination'; import makeArvadosRequest from 'make-arvados-request'; import arvadosObjectName from 'arvados-object-name'; class WBPickObjectDialog extends Component { constructor(...args) { super(...args); this.state.title = 'WBPickObjectDialog'; this.state.rows = []; this.state.textSearch = null; this.dialogRef = createRef(); } show(title, objectType, accept, filters=[]) { this.setState({ title, objectType, page: 0, rows: [], accept, filters, textSearch: null }); this.dialogRef.current.show(); this.fetchData(); } hide() { this.dialogRef.current.hide(); } fetchData() { const { app, itemsPerPage } = this.props; const { arvHost, arvToken } = app.state; const { objectType, page, textSearch } = this.state; let { filters } = this.state; if (textSearch) filters = filters.concat([['any', 'ilike', '%' + textSearch + '%']]); const order = (objectType === 'user') ? ['last_name asc', 'first_name asc'] : ['name asc']; let prom = makeArvadosRequest(arvHost, arvToken, '/arvados/v1/' + objectType + 's?offset=' + (page * itemsPerPage) + '&limit=' + itemsPerPage + '&filters=' + encodeURIComponent(JSON.stringify(filters)) + '&order=' + encodeURIComponent(JSON.stringify(order)) ); prom = prom.then(xhr => this.setState({ numPages: Math.ceil(xhr.response.items_available / itemsPerPage), rows: this.prepareRows(xhr.response.items) })); return prom; } prepareRows(items) { const { accept } = this.state; const { dialogRef } = this; return items.map(it => [ (
{ dialogRef.current.hide(); accept(it); } }> { arvadosObjectName(it) }
{ it.uuid }
) ]); } search(textSearch) { this.setState({ textSearch, page: 0}); this.fetchData(); } render({}, { title, rows, page, numPages, textSearch }) { return (
{ if (e.keyCode === 13) { e.preventDefault(); this.search(e.target.value); } } } onchange={ e => this.search(e.target.value) } />
{ this.setState({ page: i }); this.fetchData(); } } />
); } } WBPickObjectDialog.defaultProps = { itemsPerPage: 20 }; export default WBPickObjectDialog;