IF YOU WOULD LIKE TO GET AN ACCOUNT, please write an email to s dot adaszewski at gmail dot com. User accounts are meant only to report issues and/or generate pull requests. This is a purpose-specific Git hosting for ADARED projects. Thank you for your understanding!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
3.4KB

  1. import { h, Component, createRef } from 'preact';
  2. import WBDialog from 'wb-dialog';
  3. import WBTable from 'wb-table';
  4. import WBPagination from 'wb-pagination';
  5. import makeArvadosRequest from 'make-arvados-request';
  6. import arvadosObjectName from 'arvados-object-name';
  7. class WBPickObjectDialog extends Component {
  8. constructor(...args) {
  9. super(...args);
  10. this.state.title = 'WBPickObjectDialog';
  11. this.state.rows = [];
  12. this.state.textSearch = null;
  13. this.dialogRef = createRef();
  14. }
  15. show(title, objectType, accept, filters=[]) {
  16. this.setState({ title, objectType, page: 0, rows: [], accept, filters, textSearch: null });
  17. this.dialogRef.current.show();
  18. this.fetchData();
  19. }
  20. hide() {
  21. this.dialogRef.current.hide();
  22. }
  23. fetchData() {
  24. const { app, itemsPerPage } = this.props;
  25. const { arvHost, arvToken } = app.state;
  26. const { objectType, page, textSearch } = this.state;
  27. let { filters } = this.state;
  28. if (textSearch)
  29. filters = filters.concat([['any', 'ilike', '%' + textSearch + '%']]);
  30. const order = (objectType === 'user') ?
  31. ['last_name asc', 'first_name asc'] :
  32. ['name asc'];
  33. let prom = makeArvadosRequest(arvHost, arvToken,
  34. '/arvados/v1/' + objectType +
  35. 's?offset=' + (page * itemsPerPage) +
  36. '&limit=' + itemsPerPage +
  37. '&filters=' + encodeURIComponent(JSON.stringify(filters)) +
  38. '&order=' + encodeURIComponent(JSON.stringify(order))
  39. );
  40. prom = prom.then(xhr => this.setState({
  41. numPages: Math.ceil(xhr.response.items_available / itemsPerPage),
  42. rows: this.prepareRows(xhr.response.items)
  43. }));
  44. return prom;
  45. }
  46. prepareRows(items) {
  47. const { accept } = this.state;
  48. const { dialogRef } = this;
  49. return items.map(it => [
  50. (<div>
  51. <div>
  52. <a href="#" onclick={ () => { dialogRef.current.hide(); accept(it); } }>
  53. { arvadosObjectName(it) }
  54. </a>
  55. </div>
  56. <div>{ it.uuid }</div>
  57. </div>)
  58. ]);
  59. }
  60. search(textSearch) {
  61. this.setState({ textSearch, page: 0});
  62. this.fetchData();
  63. }
  64. render({}, { title, rows, page, numPages, textSearch }) {
  65. return (
  66. <WBDialog title={ title } ref={ this.dialogRef }>
  67. <div>
  68. <div class="input-group mb-3">
  69. <input type="text" class="form-control" placeholder="Search"
  70. aria-label="Search" value={ textSearch }
  71. onkeydown={ e => { if (e.keyCode === 13) {
  72. e.preventDefault();
  73. this.search(e.target.value);
  74. } } }
  75. onchange={ e => this.search(e.target.value) } />
  76. <div class="input-group-append">
  77. <button class="btn btn-outline-primary" type="button" onclick={ e => e.preventDefault() }>Search</button>
  78. </div>
  79. </div>
  80. <WBTable columns={ [ 'Name' ] } rows={ rows } />
  81. <WBPagination activePage={ page } numPages={ numPages } chunkSize={ 3 }
  82. onPageChanged={ i => { this.setState({ page: i }); this.fetchData(); } } />
  83. </div>
  84. <div>
  85. <button class="btn btn-secondary"
  86. onclick={ e => { e.preventDefault(); this.hide(); } }>
  87. Cancel
  88. </button>
  89. </div>
  90. </WBDialog>
  91. );
  92. }
  93. }
  94. WBPickObjectDialog.defaultProps = {
  95. itemsPerPage: 20
  96. };
  97. export default WBPickObjectDialog;