SearchPage.jsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // This is the root component for #search-page
  2. import React from 'react';
  3. import PropTypes from 'prop-types';
  4. import { withTranslation } from 'react-i18next';
  5. import { createSubscribedElement } from './UnstatedUtils';
  6. import AppContainer from '../services/AppContainer';
  7. import SearchPageForm from './SearchPage/SearchPageForm';
  8. import SearchResult from './SearchPage/SearchResult';
  9. class SearchPage extends React.Component {
  10. constructor(props) {
  11. super(props);
  12. this.state = {
  13. searchingKeyword: decodeURI(this.props.query.q) || '',
  14. searchedKeyword: '',
  15. searchedPages: [],
  16. searchResultMeta: {},
  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. const search = location.search || '';
  29. const query = {};
  30. search.replace(/^\?/, '').split('&').forEach((element) => {
  31. const queryParts = element.split('=');
  32. query[queryParts[0]] = decodeURIComponent(queryParts[1]).replace(/\+/g, ' ');
  33. });
  34. return query;
  35. }
  36. changeURL(keyword, refreshHash) {
  37. let hash = window.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. });
  54. return true;
  55. }
  56. this.setState({
  57. searchingKeyword: keyword,
  58. });
  59. this.props.appContainer.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. })
  68. .catch((err) => {
  69. // TODO error
  70. // this.setState({
  71. // });
  72. });
  73. }
  74. render() {
  75. return (
  76. <div>
  77. <div className="search-page-input">
  78. <SearchPageForm
  79. t={this.props.t}
  80. onSearchFormChanged={this.search}
  81. keyword={this.state.searchingKeyword}
  82. />
  83. </div>
  84. <SearchResult
  85. pages={this.state.searchedPages}
  86. searchingKeyword={this.state.searchingKeyword}
  87. searchResultMeta={this.state.searchResultMeta}
  88. />
  89. </div>
  90. );
  91. }
  92. }
  93. /**
  94. * Wrapper component for using unstated
  95. */
  96. const SearchPageWrapper = (props) => {
  97. return createSubscribedElement(SearchPage, props, [AppContainer]);
  98. };
  99. SearchPage.propTypes = {
  100. t: PropTypes.func.isRequired, // i18next
  101. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  102. query: PropTypes.object,
  103. };
  104. SearchPage.defaultProps = {
  105. // pollInterval: 1000,
  106. query: SearchPage.getQueryByLocation(window.location || {}),
  107. };
  108. export default withTranslation()(SearchPageWrapper);