Giter Site home page Giter Site logo

saturn226 / react-type-ahead-wikipedia-search-lab-v-000 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from learn-co-students/react-type-ahead-wikipedia-search-lab-v-000

0.0 1.0 0.0 288 KB

License: Other

JavaScript 93.50% Shell 3.17% HTML 3.33%

react-type-ahead-wikipedia-search-lab-v-000's Introduction

Type-Ahead Wikipedia Search Lab

Objectives

  1. Put React and unidirectional data to use
  2. Use AJAX in a small React library

Overview

In this lesson we're going to build an autocomplete component using Wikipedia's Search API.

This is what it's going to look like:

Screenshot

Stores

Our <Autocomplete /> component uses a centralized resultStore store. The store has to keep track of two different things:

  1. The actual search results as returned by Wikipedia's search API.

Before storing the results that the Wikipedia search API returns, we should convert them to an array of objects. This is what Wikipedia gives us by default when hitting the search endpoint:

[
  "search query",
  [
    "search result 1",
    "search result 2",
    "search result 3"
  ],
  [
    "longer description of search result 1",
    "longer description of search result 2",
    "longer description of search result 3"
  ],
  [
    "https://en.wikipedia.org/wiki/search_result_1",
    "https://en.wikipedia.org/wiki/search_result_2",
    "https://en.wikipedia.org/wiki/search_result_3"
  ]
]

While it's possible to store those results directly in the resultStore, having a simple array of objects to iterate over is arguably easier to reason about and write components for.

Therefore, before storing the results in the store, we convert them into the following structure:

[
  {
    "title": "search result 1",
    "description": "longer description of search result 1",
    "link": "https://en.wikipedia.org/wiki/search_result_1"
  },
  {
    "title": "search result 2",
    "description": "longer description of search result 2",
    "link": "https://en.wikipedia.org/wiki/search_result_2"
  },
  {
    "title": "search result 3",
    "description": "longer description of search result 3",
    "link": "https://en.wikipedia.org/wiki/search_result_3"
  }
]
  1. The last time the store was updated.

Since AJAX requests are by definition asynchronous, the order in which we receive responses from Wikipedia's API might be different from the order in which we sent those requests in the first place.

When we receive an "outdated" response to a previous XHR request, we simply discard it.

Therefore we need to keep track of the last time the store was updated. The easiest way to achieve this, is by adding a separate updated property to the store.

Components

Our <Autocomplete /> component consists of a <SearchField /> and <SearchResults /> component.

The <SearchField /> renders an input box where the user can search for arbitrary Wikipedia articles.

The <SearchResults /> component renders the results as received by the API.

Resources

react-type-ahead-wikipedia-search-lab-v-000's People

Contributors

alexandergugel avatar aturkewi avatar thomastuts avatar pletcher avatar saturn226 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.