Home Reference Source

application/components/common/content-search/search.js

  1. import React from 'react';
  2. import Paper from 'material-ui/Paper';
  3. import TextField from 'material-ui/TextField';
  4. import SelectField from 'material-ui/SelectField';
  5. import MenuItem from 'material-ui/MenuItem';
  6. import IconButton from 'material-ui/IconButton';
  7. import FlatButton from 'material-ui/FlatButton';
  8. import Divider from 'material-ui/Divider';
  9. import FontIcon from 'material-ui/FontIcon';
  10.  
  11. import {connect} from 'react-redux'
  12.  
  13. import {Col, Row} from '../../flexbox';
  14. import BaseView from './../../base-view';
  15.  
  16. import {contentSearchFetch, contentSearchReset} from './../../../redux/actions/content-search-actions';
  17.  
  18. import ReduxPaginator from './../redux-paginator';
  19. import ContentResults from './content-results';
  20.  
  21. class ContentSearch extends BaseView {
  22.  
  23. constructor(props) {
  24. super(props);
  25. this.displayName = 'ContentSearch';
  26.  
  27. this.state = {
  28. 'keywords': '',
  29. 'content_type': this.props.contentType ? this.props.contentType : false
  30. };
  31.  
  32. this.handleSearch = this.handleSearch.bind(this);
  33. this.handleSelectContent = this.handleSelectContent.bind(this);
  34. this.handleKeyDown = this.handleKeyDown.bind(this);
  35.  
  36. this.handlePagination = this.handlePagination.bind(this);
  37. }
  38.  
  39. componentDidMount() {
  40. const {dispatch} = this.props;
  41. dispatch(contentSearchReset())
  42. .then(() => this.handleSearch());
  43. }
  44.  
  45. handleChangeView(type, e) {
  46. this.setState({'viewType': type});
  47. }
  48.  
  49. handlePagination(page) {
  50. console.log(page);
  51. this.state.page = page;
  52. this.handleSearch();
  53. }
  54.  
  55. handleChangeType(type, e) {
  56. this.setState({'type': type}, () => {
  57. this.handleSearch();
  58. });
  59. }
  60.  
  61. /**
  62. * Simple search handler. See the API search docs
  63. * for field specifications
  64. */
  65. handleSearch() {
  66. const page = this.state.page ? this.state.page : 1;
  67. let filters = null;
  68. if (this.state.content_type) {
  69. filters = {
  70. 'content_type': this.state.content_type
  71. };
  72. }
  73. this.props.dispatch(contentSearchFetch({
  74. 'keywords': this.refs.keywords.getValue(),
  75. 'author': this.refs.author.getValue(),
  76. 'tag': this.refs.tag.getValue(),
  77. 'type': 'content',
  78. 'filters': filters,
  79. 'page': page
  80. })).then(() => {
  81. if (!window) {
  82. return;
  83. }
  84. // forces the modal reposition when the contents of the search box
  85. // update
  86. window.dispatchEvent(new Event('resize'))
  87. });
  88. }
  89.  
  90. /**
  91. * Bubble the select media back up to the parent component
  92. */
  93. handleSelectContent(m) {
  94. if (this.props.onSelectContent) {
  95. this.props.onSelectContent(m);
  96. }
  97. }
  98.  
  99. handleKeyDown(e) {
  100. if (e.which === 13) {
  101. this.handleSearch();
  102. }
  103. }
  104. handleFilterType(type, e) {
  105. this.setState({
  106. 'content_type': type
  107. }, () => {
  108. this.handleSearch.call(this);
  109. });
  110. }
  111.  
  112. render() {
  113. return (
  114. <div>
  115. <Row middle='xs'>
  116. <Col xs={12}>
  117. <strong>Search Content</strong>
  118. </Col>
  119. </Row>
  120. <div>
  121. <Row middle='xs'>
  122. <Col xs={12}>
  123. <TextField
  124. ref='keywords'
  125. onKeyDown={this.handleKeyDown}
  126. placeholder='Keywords'
  127. fullWidth={true}
  128. />
  129. </Col>
  130. </Row>
  131. <Row middle='xs'>
  132. <Col xs={6}>
  133. <TextField
  134. ref='tag'
  135. onKeyDown={this.handleKeyDown}
  136. placeholder='Tag'
  137. />
  138. </Col>
  139. <Col xs={6}>
  140. <TextField
  141. ref='author'
  142. onKeyDown={this.handleKeyDown}
  143. placeholder='Author'
  144. />
  145. </Col>
  146. </Row>
  147.  
  148. <Divider />
  149.  
  150. <Row middle='xs' className='clear-bottom clear-top'>
  151. <Col xs={6}>
  152. Type:
  153. <span className={'state-pill' + (!this.state.content_type ? ' active' : '')} onClick={this.handleFilterType.bind(this, false)}>Any</span>
  154. <span className={'state-pill' + (this.state.content_type == 'article' ? ' active' : '')} onClick={this.handleFilterType.bind(this, 'article')}>Article</span>
  155. <span className={'state-pill' + (this.state.content_type == 'media' ? ' active' : '')} onClick={this.handleFilterType.bind(this, 'media')}>Media</span>
  156. <span className={'state-pill' + (this.state.content_type == 'post' ? ' active' : '')} onClick={this.handleFilterType.bind(this, 'post')}>Post</span>
  157. <span className={'state-pill' + (this.state.content_type == 'page' ? ' active' : '')} onClick={this.handleFilterType.bind(this, 'page')}>Page</span>
  158. </Col>
  159. <Col xs={6} right='xs'>
  160. <ReduxPaginator pagination={this.props.contentSearch.pagination} onPaginate={this.handlePagination.bind(this)} />
  161. </Col>
  162. </Row>
  163.  
  164. <ContentResults onSelectContent={this.handleSelectContent} view={this.state.viewType} />
  165. </div>
  166. </div>
  167. );
  168. }
  169. }
  170.  
  171. // This is just to force the modal to resize on update
  172. export default connect((state) => {
  173. return {
  174. contentSearch: state.contentSearch
  175. }
  176. })(ContentSearch);