sujinleeme / reactnd-project-myreads

The bookshelf App : A Project for @udacity's React React Nanodegree
https://sujinlee-reactnd-project.firebaseapp.com/
MIT License
6 stars 3 forks source link

Update. Search Book Results Page #3

Open sujinleeme opened 7 years ago

sujinleeme commented 7 years ago

TODO

AJAX

REDUCX

UI

Autocomplete Keywords

sujinleeme commented 7 years ago

References

sujinleeme commented 7 years ago

Issue

Search Input Field

sujinleeme commented 7 years ago

Learn

1. call onChange event after pressing Enter key

In onKeyDown you can check key. If it is ENTER pressed call event.preventDefault() to prevent onChange call, or if it is not - do nothing Javascript event queue doesn't contain change event until default handler for keydown event is called. If you call event.preventDefault() in onKeyDown handler no change event will be fired.

2. [Object object]

JSON.stringify() is to create a JSON string out of an object/array. Object object is the default string representation of a JavaScript Object. To Fix with : use JSON.stringify(theObject) theObject.toString()

A quick and dirty way to do this is to stringify your object, and then parse that JSON string back into a brand new object. I recommend creating a utility function, since you’ll likely want to re-use this throughout your app. Below is an example: - Comparing and Modifying Objects in React

function deepCopy(obj) {
  if (obj !== undefined && obj !== null) {
    return JSON.parse(JSON.stringify(obj));
  }

  return null;
}

3. Check if an array is empty in React

{this.state.books && this.state.books.length > 0
<ol className="books-grid">
  {this.state.books && this.state.books.length > 0 ? (
  <div>
    <BookShelf {...showingBooks} title="Result" />
  </div> ) : (
  <p>No Books Found :(</p>  )}
</ol>

4. Make Search keywords in Autocomplete

  currentMatches() {
    const list = this.state.list.SearchKeywords
    return list.filter((item) => {
      let searchInput = this.props.content;
      return item.label.indexOf(searchInput) > -1;
    })
  }

5. componentWillReceiveProps

Use LifeCyle methods to update state before render

  renderMatches() {
    let searchableKeywords = this.state.matched;
    return (
      <ol 
        className="match-keywords">
        {Object.keys(searchableKeywords).map((k, index) =>
          <AutocompleteItem key={k} keywords={this.state.list} {...searchableKeywords[k]} />
        )} 
      </ol>
   )
  }

  componentWillReceiveProps (nextProps){
    this.setState({matched : this.currentMatches()})
  }

  currentMatches() {
    const list = this.state.list.SearchKeywords
    return list.filter((item) => {
      let searchInput = this.props.content;
      return item.label.indexOf(searchInput) > -1;
    })
  }