PageListSearch.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // This is the root component for #page-list-search
  2. import React from 'react';
  3. import axios from 'axios'
  4. export default class PageListSearch extends React.Component {
  5. constructor(props) {
  6. super(props);
  7. this.state = {
  8. keyword: '',
  9. }
  10. //this.changeURL = this.changeURL.bind(this);
  11. this.search = this.search.bind(this);
  12. this.handleChange = this.handleChange.bind(this);
  13. this.ticker = null;
  14. }
  15. componentDidMount() {
  16. // This is temporary data bind
  17. this.ticker = setInterval(this.pageListObserver.bind(this), 1000);
  18. }
  19. componentWillUnmount() {
  20. clearInterval(this.ticker);
  21. }
  22. pageListObserver() {
  23. let value = $('#page-list-search-form').val();
  24. this.setState({keyword: value});
  25. this.search();
  26. }
  27. static getQueryByLocation(location) {
  28. let search = location.search || '';
  29. let query = {};
  30. search.replace(/^\?/, '').split('&').forEach(function(element) {
  31. let queryParts = element.split('=');
  32. query[queryParts[0]] = decodeURIComponent(queryParts[1]).replace(/\+/g, ' ');
  33. });
  34. return query;
  35. }
  36. handleChange(event) {
  37. // this is not fired now because of force-data-bound by jQuery
  38. const keyword = event.target.value;
  39. this.setState({keyword});
  40. console.log('Changed');
  41. }
  42. changeURL(keyword, refreshHash) {
  43. let hash = location.hash || '';
  44. // TODO 整理する
  45. if (refreshHash || this.state.searchedKeyword !== '') {
  46. hash = '';
  47. }
  48. if (window.history && window.history.pushState){
  49. window.history.pushState('', `Search - ${keyword}`, `/_search?q=${keyword}${hash}`);
  50. }
  51. }
  52. search() {
  53. const keyword = this.state.keyword;
  54. console.log('Search with', keyword);
  55. return true ;
  56. if (keyword === '') {
  57. this.setState({
  58. searchingKeyword: '',
  59. searchedPages: [],
  60. });
  61. return true;
  62. }
  63. this.setState({
  64. searchingKeyword: keyword,
  65. });
  66. axios.get('/_api/search', {params: {q: keyword}})
  67. .then((res) => {
  68. if (res.data.ok) {
  69. this.changeURL(keyword);
  70. this.setState({
  71. searchedKeyword: keyword,
  72. searchedPages: res.data.data,
  73. searchResultMeta: res.data.meta,
  74. });
  75. }
  76. // TODO error
  77. })
  78. .catch((res) => {
  79. // TODO error
  80. });
  81. };
  82. render() {
  83. return (
  84. <div>
  85. <input
  86. type="hidden"
  87. name="q"
  88. value={this.state.keyword}
  89. onChange={this.handleChange}
  90. className="form-control"
  91. />
  92. </div>
  93. );
  94. }
  95. }
  96. PageListSearch.propTypes = {
  97. query: React.PropTypes.object,
  98. };
  99. PageListSearch.defaultProps = {
  100. //pollInterval: 1000,
  101. query: PageListSearch.getQueryByLocation(location || {}),
  102. };