SearchPage.js 2.8 KB

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