File size: 4,594 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import React from 'react';
import {
  InstantSearch,
  Hits,
  Stats,
  Pagination,
  RatingMenu,
  Highlight,
  Configure,
  connectSearchBox,
  connectRefinementList,
} from 'react-instantsearch-dom';
import algoliasearch from 'algoliasearch/lite';
import withURLSync from './URLSync';
import './App.css';

const searchClient = algoliasearch(
  'latency',
  '6be0576ff61c053d5f9a3225e2a90f76'
);

const App = (props) => (
  <InstantSearch
    searchClient={searchClient}
    indexName="movies"
    searchState={props.searchState}
    createURL={props.createURL}
    onSearchStateChange={props.onSearchStateChange}
  >
    <Configure hitsPerPage={10} />
    <Header />
    <section>
      <Facets />
      <Results />
    </section>
  </InstantSearch>
);

const Header = () => (
  <header>
    <a
      href="https://www.algolia.com/doc/guides/building-search-ui/what-is-instantsearch/react/"
      className="is-logo"
    >
      <img
        alt="React InstantSearch"
        src="https://res.cloudinary.com/hilnmyskv/image/upload/w_100,h_100,dpr_2.0//v1461180087/logo-instantsearchjs-avatar.png"
        width="40"
      />
    </a>
    <a href="./" className="logo">
      You
      <i className="fa fa-youtube-play" />
    </a>
    <SearchBox />
  </header>
);

const SearchBox = connectSearchBox(({ currentRefinement, refine }) => (
  <div className="searchbox-container">
    <div className="input-group">
      <input
        type="text"
        value={currentRefinement}
        onChange={(e) => refine(e.target.value)}
        autoComplete="off"
        className="form-control"
      />
      <span className="input-group-btn">
        <button className="btn btn-default">
          <i className="fa fa-search" />
        </button>
      </span>
    </div>
  </div>
));

const Facets = () => (
  <aside>
    <ul className="nav nav-list panel">
      <li>
        <a href="./">
          <i className="fa fa-home" /> Home
        </a>
      </li>
      <li className="separator" />
    </ul>
    <Panel title="Genres" id="genres">
      <RefinementListLinks attribute="genre" />
    </Panel>
    <Panel title="Rating" id="ratings">
      <RatingMenu attribute="rating" max={5} />
    </Panel>
    <div className="thank-you">
      {' '}
      Data courtesy of <a href="https://www.imdb.com/">imdb.com</a>
    </div>
  </aside>
);

const Panel = ({ title, children, id }) => (
  <div id={id}>
    <h5>
      <i className="fa fa-chevron-right" /> {title}
    </h5>
    {children}
  </div>
);

const Star = ({ active }) => (
  <span className={`star${active ? '' : '__empty'}`} />
);
const Stars = ({ rating }) => {
  const stars = [];
  for (let i = 1; i <= 5; ++i) {
    stars.push(i <= rating);
  }
  return (
    <span className="stars">
      {stars.map((active, idx) => (
        <Star key={idx} active={active} />
      ))}
    </span>
  );
};
const Genre = ({ name }) => <span className="badge">{name}</span>;
const Genres = ({ genres }) => (
  <p className="genre">
    {genres.map((genre, idx) => (
      <Genre name={genre} key={idx} />
    ))}
  </p>
);

const Hit = (hit) => {
  const { image, rating, year, genre } = hit.hit;
  return (
    <div className="hit media">
      <div className="media-left">
        <div
          className="media-object"
          style={{ backgroundImage: `url(${image})` }}
        />
      </div>
      <div className="media-body">
        <h4 className="media-heading">
          <Highlight attribute="title" hit={hit.hit} />
          <Stars rating={rating} />
        </h4>
        <p className="year">{year}</p>
        <Genres genres={genre} />
      </div>
    </div>
  );
};

const Results = connectSearchBox(() => (
  <article>
    <div id="stats" className="text-right text-muted">
      <Stats />
    </div>
    <hr />
    <div id="hits">
      <Hits hitComponent={Hit} />
    </div>
    <div id="pagination" className="text-center">
      <Pagination />
    </div>
  </article>
));

const RefinementListLinks = connectRefinementList(
  ({ items, refine, createURL }) => {
    const hitComponents = items.map((item) => (
      <div className={item.isRefined ? ' active' : ''} key={item.label}>
        <a
          className="item"
          href={createURL(item.value)}
          onClick={(e) => {
            e.preventDefault();
            refine(item.value);
          }}
        >
          <span> {item.label}</span>
          <span className="badge pull-right">{item.count}</span>
        </a>
      </div>
    ));

    return <div className="nav nav-list">{hitComponents}</div>;
  }
);

export default withURLSync(App);