Selector.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import React, {Component} from 'react';
  2. import Select from "react-virtualized-select";
  3. import AlbumList from './AlbumList.js';
  4. import {getQueryString} from './utils.js';
  5. const fetchOpts = { credentials: 'same-origin' }
  6. export default class Selector extends Component {
  7. selects = [{type: 'artist', title: 'Artists'},
  8. {type: 'year', title: 'Years'},
  9. {type: 'genre', title: 'Genres'},
  10. {type: 'publisher', title: 'Labels'},
  11. {type: 'country', title: 'Countries'},
  12. {type: 'type', title: 'Album types'},
  13. {type: 'status', title: 'Album statuses'}];
  14. constructor(props) {
  15. super(props);
  16. this.state = {
  17. albumList: {key:'', albums: [], error: false, hasMore: false},
  18. filters: {},
  19. filter: '',
  20. };
  21. this.filterTimeout = null;
  22. }
  23. genAlbumListKey() {
  24. const {filter, filters} = this.state
  25. return `${filter}-${this.selects.map(({type}) => filters[type] ? filters[type].item : '').join('-')}`
  26. }
  27. loadAlbums = (page) => {
  28. const {filter, filters} = this.state
  29. const limit = 15;
  30. const offset = page * limit;
  31. const params = {filter, offset, limit:(limit+1)};
  32. let key = (page === 0 ? this.genAlbumListKey() : this.state.albumList.key)
  33. for (const {type} of this.selects) {
  34. if (filters[type]) {
  35. params[type] = filters[type].item;
  36. }
  37. }
  38. return fetch(`/cat/album?${getQueryString(params)}`, fetchOpts)
  39. .then(res => (res.ok ? res.json() : Promise.reject({message:res.statusText})))
  40. .then(data => {
  41. const hasMore = (data.length > limit)
  42. const albums = page === 0 ? data.slice(0, limit) : this.state.albumList.albums.concat(data.slice(0, limit))
  43. const albumList = {key, albums, hasMore}
  44. this.setState({albumList})
  45. return albums;
  46. })
  47. .catch(error => {
  48. console.log(error)
  49. const albumList = {key, error}
  50. this.setState({albumList})
  51. })
  52. }
  53. componentDidMount() {
  54. this.loadAlbums(0)
  55. }
  56. componentDidUpdate(prevProps, prevState) {
  57. const {filters} = this.state;
  58. if (filters !== prevState.filters) {
  59. this.loadAlbums(0);
  60. }
  61. }
  62. handleFilterChange = (e) => {
  63. if (this.filterTimeout) {
  64. clearInterval(this.filterTimeout);
  65. }
  66. const filter = e.target.value;
  67. this.filterTimeout = setTimeout(() => {
  68. this.loadAlbums(0);
  69. }, 500);
  70. this.setState({filter})
  71. }
  72. handleLoadOptions(cat, q, page) {
  73. const pageSize = 100
  74. const params = {filter:q}
  75. params.offset = (page - 1) * pageSize;
  76. params.limit = pageSize;
  77. return fetch(`/cat/${cat}?${getQueryString(params)}`, fetchOpts)
  78. .then(res => (res.ok ? res.json() : Promise.reject({message:res.statusText})))
  79. .then(options => ({options}))
  80. }
  81. handleSelectChange(cat, option) {
  82. const filters = Object.assign({}, this.state.filters, {[cat]: option});
  83. this.setState({filters, filter: ''})
  84. }
  85. _optionRenderer ({ focusedOption, focusOption, key, labelKey, option, selectValue, style, valueArray }) {
  86. const className = ['VirtualizedSelectOption']
  87. if (option === focusedOption) {
  88. className.push('VirtualizedSelectFocusedOption')
  89. }
  90. if (option.disabled) {
  91. className.push('VirtualizedSelectDisabledOption')
  92. }
  93. if (valueArray && valueArray.indexOf(option) >= 0) {
  94. className.push('VirtualizedSelectSelectedOption')
  95. }
  96. if (option.className) {
  97. className.push(option.className)
  98. }
  99. const events = option.disabled
  100. ? {}
  101. : {
  102. onClick: () => selectValue(option),
  103. onMouseEnter: () => focusOption(option)
  104. }
  105. return (
  106. <div
  107. className={className.join(' ')}
  108. key={key}
  109. style={style}
  110. title={option.title}
  111. {...events}
  112. >
  113. {option.item} - {option.count}
  114. </div>
  115. )
  116. }
  117. render() {
  118. return (
  119. <div className="Selector">
  120. <div className="SelectorFilters">
  121. {this.selects.map(({type, title}) => (
  122. <div className="section" key={type}>
  123. <h3>{title}</h3>
  124. <Select
  125. async
  126. pagination
  127. autoload={true}
  128. optionRenderer={this._optionRenderer}
  129. loadOptions={(q,p) => this.handleLoadOptions(type, q, p)}
  130. onChange={o => this.handleSelectChange(type, o)}
  131. labelKey="item"
  132. valueKey="item"
  133. value={this.state.filters[type]}
  134. />
  135. </div>
  136. ))}
  137. <div className="section">
  138. <h3>Search</h3>
  139. <input className="Filter"
  140. type="text"
  141. value={this.state.filter}
  142. placeholder="Search albums"
  143. onChange={this.handleFilterChange} />
  144. </div>
  145. </div><br style={{clear:'both'}} />
  146. <AlbumList
  147. title="Albums"
  148. {...this.state.albumList}
  149. loadMore={this.loadAlbums}
  150. onPlayAlbum={this.props.onPlayAlbum} />
  151. </div>
  152. );
  153. }
  154. }