All files / client/components SearchPage.tsx

0% Statements 0/37
0% Branches 0/24
0% Functions 0/10
0% Lines 0/35

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131                                                                                                                                                                                                                                                                     
// This is the root component for #search-page
 
import React from 'react'
 
import queryString from 'query-string'
import Emitter from '../emitter'
import SearchToolbar from 'components/SearchPage/SearchToolbar'
import SearchResult from './SearchPage/SearchResult'
import Crowi from 'client/util/Crowi'
import { Page } from 'client/types/crowi'
 
interface Props {
  crowi: Crowi
}
 
interface State {
  searching: boolean
  searchingKeyword: string
  searchingType: string
  searchedPages: Page[]
  searchResultMeta: { total?: number }
  searchError: Error | null
}
 
interface Query {
  q: string
  type: string
}
 
export default class SearchPage extends React.Component<Props, State> {
  static defaultProps = { searchError: null }
 
  constructor(props: Props) {
    super(props)
 
    const { q = '', type = '' } = queryString.parse(this.props.crowi.location.search)
    this.state = {
      searching: false,
      searchingKeyword: String(q),
      searchingType: String(type),
      searchedPages: [],
      searchResultMeta: {},
      searchError: null,
    }
 
    this.search = this.search.bind(this)
    this.buildQuery = this.buildQuery.bind(this)
    this.changeURL = this.changeURL.bind(this)
    this.changeType = this.changeType.bind(this)
 
    Emitter.on('search', ({ keyword: q = '' }) => {
      this.search(this.buildQuery({ q }))
    })
  }
 
  componentDidMount() {
    if (this.state.searchingKeyword !== '') {
      this.search(this.buildQuery())
    }
  }
 
  buildQuery(override = {}) {
    const { searchingKeyword: q = '', searchingType: type = '' } = this.state
    const removeEmpty = (query: any) => Object.keys(query).forEach(k => !query[k] && delete query[k])
    const query = { q, type, ...override }
    removeEmpty(query)
    return query
  }
 
  changeURL({ q, type }: Query, refreshHash = false) {
    let { hash = '' } = this.props.crowi.location
    // TODO 整理する
    if (refreshHash || q !== '') {
      hash = ''
    }
    const query = queryString.stringify({ q, type })
    if (window.history && window.history.pushState) {
      window.history.pushState('', `Search - ${q}`, `/_search?${query}${hash}`)
    }
  }
 
  changeType(type: string) {
    this.search(this.buildQuery({ type }))
  }
 
  async search(query: Query) {
    const { q = '', type = '' } = query
    if (q === '') {
      this.setState({
        searchingKeyword: '',
        searchedPages: [],
        searchResultMeta: {},
        searchError: null,
      })
 
      return true
    }
 
    this.setState({ searching: true })
    try {
      const { data, meta } = await this.props.crowi.apiGet('/search', query)
      this.changeURL(query)
      this.setState({
        searchingKeyword: q,
        searchingType: type,
        searchedPages: data,
        searchResultMeta: meta,
        searching: false,
      })
    } catch (err) {
      // TODO error
      this.setState({ searchError: err, searching: false })
    }
  }
 
  render() {
    return (
      <div className="content-main">
        <SearchToolbar
          keyword={this.state.searchingKeyword}
          type={this.state.searchingType}
          total={this.state.searchResultMeta.total}
          searching={this.state.searching}
          changeType={this.changeType}
        />
        <SearchResult pages={this.state.searchedPages} searchingKeyword={this.state.searchingKeyword} searchResultMeta={this.state.searchResultMeta} />
      </div>
    )
  }
}