SearchPage.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // This is the root component for #search-page
  2. import React from 'react';
  3. import axios from 'axios'
  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. searchedPages: [],
  13. searchResultMeta: {},
  14. searchError: null,
  15. }
  16. this.search = this.search.bind(this);
  17. this.changeURL = this.changeURL.bind(this);
  18. }
  19. componentDidMount() {
  20. if (this.state.searchingKeyword !== '') {
  21. this.search({keyword: this.state.searchingKeyword});
  22. this.setState({searchedKeyword: this.state.keyword});
  23. }
  24. }
  25. static getQueryByLocation(location) {
  26. let search = location.search || '';
  27. let query = {};
  28. search.replace(/^\?/, '').split('&').forEach(function(element) {
  29. let queryParts = element.split('=');
  30. query[queryParts[0]] = decodeURIComponent(queryParts[1]).replace(/\+/g, ' ');
  31. });
  32. return query;
  33. }
  34. changeURL(keyword, refreshHash) {
  35. let hash = location.hash || '';
  36. // TODO 整理する
  37. if (refreshHash) {
  38. hash = '';
  39. }
  40. if (window.history && window.history.pushState){
  41. window.history.pushState('', `Search - ${keyword}`, `/_search?q=${keyword}${hash}`);
  42. }
  43. }
  44. search(data) {
  45. const keyword = data.keyword;
  46. const lastSearchedKeyword = this.state.searhingKeyword;
  47. if (keyword === '') {
  48. this.setState({
  49. searchingKeyword: '',
  50. searchedPages: [],
  51. });
  52. return true;
  53. }
  54. this.setState({
  55. searchingKeyword: keyword,
  56. });
  57. axios.get('/_api/search', {params: {q: keyword}})
  58. .then((res) => {
  59. if (res.data.ok) {
  60. this.setState({
  61. searchingKeyword: keyword,
  62. searchedPages: res.data.data,
  63. searchResultMeta: res.data.meta,
  64. });
  65. }
  66. // if lastSearchedKeyword is empty, refresh hash
  67. this.changeURL(keyword, lastSearchedKeyword !== undefined);
  68. // TODO error
  69. })
  70. .catch((res) => {
  71. // TODO error
  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. />
  90. </div>
  91. );
  92. }
  93. }
  94. SearchPage.propTypes = {
  95. query: React.PropTypes.object,
  96. };
  97. SearchPage.defaultProps = {
  98. //pollInterval: 1000,
  99. query: SearchPage.getQueryByLocation(location || {}),
  100. };