|
| 1 | +import { Glyphicon, Panel, tableClassStripedHover } from 'f61ui/component/bootstrap'; |
| 2 | +import { Result } from 'f61ui/component/result'; |
| 3 | +import { browseUrl, collectionUrl, searchUrl } from 'generated/frontend_uiroutes'; |
| 4 | +import { search } from 'generated/stoserver/stoservertypes_endpoints'; |
| 5 | +import { SearchResult } from 'generated/stoserver/stoservertypes_types'; |
| 6 | +import { AppDefaultLayout } from 'layout/appdefaultlayout'; |
| 7 | +import * as React from 'react'; |
| 8 | + |
| 9 | +interface SearchPageProps { |
| 10 | + query: string; |
| 11 | +} |
| 12 | + |
| 13 | +interface SearchPageState { |
| 14 | + queryTransient: string; |
| 15 | + results: Result<SearchResult[]>; |
| 16 | +} |
| 17 | + |
| 18 | +export default class SearchPage extends React.Component<SearchPageProps, SearchPageState> { |
| 19 | + state: SearchPageState = { |
| 20 | + queryTransient: this.props.query, |
| 21 | + results: new Result<SearchResult[]>((_) => { |
| 22 | + this.setState({ results: _ }); |
| 23 | + }), |
| 24 | + }; |
| 25 | + |
| 26 | + componentDidMount() { |
| 27 | + this.fetchData(); |
| 28 | + } |
| 29 | + |
| 30 | + componentWillReceiveProps() { |
| 31 | + this.fetchData(); |
| 32 | + } |
| 33 | + |
| 34 | + render() { |
| 35 | + return ( |
| 36 | + <AppDefaultLayout title="Search" breadcrumbs={[]}> |
| 37 | + <Panel heading="Search">{this.renderData()}</Panel> |
| 38 | + </AppDefaultLayout> |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + private renderData() { |
| 43 | + const [results, loadingOrError] = this.state.results.unwrap(); |
| 44 | + |
| 45 | + return ( |
| 46 | + <div> |
| 47 | + <form action={searchUrl({ q: '' })} method="get"> |
| 48 | + <div className="input-group"> |
| 49 | + <input |
| 50 | + className="form-control" |
| 51 | + name="q" |
| 52 | + value={this.state.queryTransient} |
| 53 | + onChange={(e) => { |
| 54 | + this.setState({ queryTransient: e.target.value }); |
| 55 | + }} |
| 56 | + autoFocus={true} |
| 57 | + /> |
| 58 | + <span className="input-group-btn"> |
| 59 | + <button className="btn btn-default" type="submit"> |
| 60 | + 🔍 |
| 61 | + </button> |
| 62 | + </span> |
| 63 | + </div> |
| 64 | + </form> |
| 65 | + |
| 66 | + <table className={tableClassStripedHover}> |
| 67 | + <thead> |
| 68 | + <tr> |
| 69 | + <th>Kind</th> |
| 70 | + <th>Result</th> |
| 71 | + </tr> |
| 72 | + </thead> |
| 73 | + <tbody> |
| 74 | + {(results || []).map((result: SearchResult) => { |
| 75 | + const kindIndicator = ((): [string, JSX.Element, JSX.Element] => { |
| 76 | + if (result.Collection) { |
| 77 | + return [ |
| 78 | + `coll-${result.Collection.Id}`, |
| 79 | + <Glyphicon icon="duplicate" />, |
| 80 | + <a href={collectionUrl({ id: result.Collection.Id })}> |
| 81 | + {result.Collection.Name} |
| 82 | + </a>, |
| 83 | + ]; |
| 84 | + } |
| 85 | + if (result.Directory) { |
| 86 | + return [ |
| 87 | + `dir-${result.Directory.Directory.Directory.Id}`, |
| 88 | + <Glyphicon icon="folder-open" />, |
| 89 | + <a |
| 90 | + href={browseUrl({ |
| 91 | + dir: result.Directory.Directory.Directory.Id, |
| 92 | + })}> |
| 93 | + {result.Directory.Directory.Directory.Name} |
| 94 | + </a>, |
| 95 | + ]; |
| 96 | + } |
| 97 | + throw new Error('should not happen'); |
| 98 | + })(); |
| 99 | + |
| 100 | + return ( |
| 101 | + <tr id={kindIndicator[0]}> |
| 102 | + <td>{kindIndicator[1]}</td> |
| 103 | + <td>{kindIndicator[2]}</td> |
| 104 | + </tr> |
| 105 | + ); |
| 106 | + })} |
| 107 | + </tbody> |
| 108 | + <tfoot>{loadingOrError}</tfoot> |
| 109 | + </table> |
| 110 | + </div> |
| 111 | + ); |
| 112 | + } |
| 113 | + |
| 114 | + private fetchData() { |
| 115 | + this.state.results.load(() => search(this.props.query)); |
| 116 | + } |
| 117 | +} |
0 commit comments