Guest User

Untitled

a guest
Jan 16th, 2017
468
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //List view
  2. //
  3. class List extends React.Component {
  4.     constructor(props){
  5.         super(props);
  6.         this.test = this.test.bind(this);
  7.         this.state = {
  8.             params: "",
  9.             searchResults: [],
  10.             recipeName: [],
  11.             desc: [],
  12.             UID: []
  13.         }
  14.     }
  15.  
  16.     componentDidMount () {
  17.         console.log("getting");
  18.         $.ajax ({
  19.           method: 'GET',
  20.           url: "http://localhost:3333/search/" + this.props.location.query.q,
  21.           success: (data) => {
  22.             console.log(data);
  23.             this.setState({searchResults: data[0]});
  24.             for (var i = 0; i < data[0].length; i++) {
  25.                 this.setState({recipeName: this.state.recipeName.concat(data[0][i]['recipeName'])});
  26.                 this.setState({UID: this.state.UID.concat(data[0][i]['UID'])});
  27.                 this.setState({desc: this.state.desc.concat(data[0][i]['shortDesc'])});
  28.             }
  29.            
  30.           }
  31.         });
  32.     }
  33.     test (e) {
  34.         if ( e.key == "Enter") {
  35.             console.log(this.refs.searchBar.state.searchBar);
  36.             console.log(this.props);
  37.             this.setState({
  38.                 params: "-"
  39.             });
  40.            
  41.  
  42.         }
  43.     }
  44.     render() {
  45.         return(
  46.             <div id="testDIV" onKeyPress={this.test}>
  47.                 <HeaderMenu ref="searchBar"/>
  48.                 <RenderArray results={this.state.searchResults}/>
  49.             </div>
  50.             );
  51.     }
  52.  
  53. }
  54.  
  55.  
  56. //HeaderMenu component
  57. //
  58. class HeaderMenu extends React.Component{
  59.     constructor(props) {
  60.         super (props);
  61.         this.onChange = this.onChange.bind(this);
  62.         this.onKeyPress = this.onKeyPress.bind(this);
  63.         this.searchGo = this.searchGo.bind(this);
  64.         this.state = {
  65.             searchBar: "",
  66.             DropdownName: "Menu",
  67.             subNames: ["Recipes", "Categories"],
  68.             subLinks: ["/list", "/index"]
  69.         };
  70.     }
  71.     onKeyPress (e) {
  72.         if ( e.key == "Enter") {
  73.             console.log("Enter is pressed");
  74.             this.searchGo();
  75.  
  76.         }
  77.     };
  78.     searchGo (e) {
  79.         console.log(this.state.searchBar);
  80.  
  81.         var pathname = '/list/';
  82.         var query = {
  83.             q: this.state.searchBar
  84.         };
  85.         hashHistory.push({pathname, query}); //Endres til browserHistory når siden deployes
  86.     }
  87.  
  88.  
  89.     onChange (e) {
  90.         var t = {};
  91.         var id = e.target.id
  92.         t[id] = e.target.value
  93.         this.setState(t);
  94.     };
  95.  
  96.     render(){
  97.         return(
  98.             <div onClick={this.handleBodyClick}>
  99.                 <ul className="headerBar">
  100.                     <li className="liSearchBar"><button  id="searchButton" onClick={this.searchGo}>Go!</button></li>
  101.                     <li className="liSearchBar"><input type="text" className="searchBar" id="searchBar" placeholder="Search" onChange={this.onChange} onKeyPress={this.onKeyPress}/></li>
  102.  
  103.                 </ul>
  104.  
  105.             </div>
  106.         );
  107.     }
  108.  
  109. }
Advertisement
Add Comment
Please, Sign In to add comment