SearchPage.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // This is the root component for #search-page
  2. import React from 'react';
  3. import Crowi from '../util/Crowi';
  4. import SearchForm from './SearchPage/SearchForm';
  5. import SearchResult from './SearchPage/SearchResult';
  6. export default class SearchPage extends React.Component {
  7. constructor(props) {
  8. super(props);
  9. this.crowi = window.crowi; // FIXME
  10. this.state = {
  11. location: location,
  12. searchingKeyword: this.props.query.q || '',
  13. searchedKeyword: '',
  14. searchedPages: [],
  15. searchResultMeta: {},
  16. searchError: null,
  17. }
  18. this.search = this.search.bind(this);
  19. this.changeURL = this.changeURL.bind(this);
  20. }
  21. componentDidMount() {
  22. const keyword = this.state.searchingKeyword;
  23. if (keyword !== '') {
  24. this.search({keyword});
  25. }
  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. changeURL(keyword, refreshHash) {
  37. let hash = location.hash || '';
  38. // TODO 整理する
  39. if (refreshHash || this.state.searchedKeyword !== '') {
  40. hash = '';
  41. }
  42. if (window.history && window.history.pushState){
  43. window.history.pushState('', `Search - ${keyword}`, `/_search?q=${keyword}${hash}`);
  44. }
  45. }
  46. search(data) {
  47. const keyword = data.keyword;
  48. if (keyword === '') {
  49. this.setState({
  50. searchingKeyword: '',
  51. searchedPages: [],
  52. searchResultMeta: {},
  53. searchError: null,
  54. });
  55. return true;
  56. }
  57. this.setState({
  58. searchingKeyword: keyword,
  59. });
  60. this.crowi.apiGet('/search', {q: keyword})
  61. .then(res => {
  62. this.changeURL(keyword);
  63. this.setState({
  64. searchedKeyword: keyword,
  65. searchedPages: res.data,
  66. searchResultMeta: res.meta,
  67. });
  68. }).catch(err => {
  69. // TODO error
  70. this.setState({
  71. searchError: err,
  72. });
  73. });
  74. };
  75. render() {
  76. return (
  77. <div>
  78. <div className="header-wrap">
  79. <header>
  80. <SearchForm
  81. onSearchFormChanged={this.search}
  82. keyword={this.state.searchingKeyword}
  83. />
  84. </header>
  85. </div>
  86. <SearchResult
  87. pages={this.state.searchedPages}
  88. searchingKeyword={this.state.searchingKeyword}
  89. searchResultMeta={this.state.searchResultMeta}
  90. />
  91. </div>
  92. );
  93. }
  94. }
  95. SearchPage.propTypes = {
  96. query: React.PropTypes.object,
  97. };
  98. SearchPage.defaultProps = {
  99. //pollInterval: 1000,
  100. query: SearchPage.getQueryByLocation(location || {}),
  101. searchError: null,
  102. };