SearchPage.js 2.8 KB

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