File size: 9,498 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
import algoliasearch from 'algoliasearch/lite';
import {
  InstantSearch,
  ClearRefinements,
  SearchBox,
  Pagination,
  Highlight,
  Configure,
  connectHits,
  connectNumericMenu,
  connectRefinementList,
  connectRange,
} from 'react-instantsearch-dom';
import {
  GoogleMapsLoader,
  GeoSearch,
  Marker,
} from 'react-instantsearch-dom-maps';
import PropTypes from 'prop-types';
import React, { Component, Fragment } from 'react';
import Rheostat from 'rheostat';
import withURLSync from './URLSync';
import './App.css';

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

const App = (props) => (
  <InstantSearch
    searchClient={searchClient}
    indexName="airbnb"
    searchState={props.searchState}
    createURL={props.createURL}
    onSearchStateChange={props.onSearchStateChange}
  >
    <Configure aroundLatLngViaIP={true} />
    <Header />
    <Filters />
    <Results />
  </InstantSearch>
);

function Header() {
  return (
    <header className="navbar navbar-static-top aisdemo-navbar">
      <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">
        BnB
      </a>
      <i className="fa fa-search" />
      <SearchBox />
    </header>
  );
}

function Filters() {
  return (
    <Fragment>
      <div className="search-filters">
        <div className="aisdemo--left-column">
          <div className="aisdemo-filters">
            <DatesAndGuest />
            <Capacity />
            <RoomType attribute="room_type" operator="or" limit={3} />
            <Price />
          </div>

          <div className="row">
            <div id="stats" />
          </div>
        </div>

        <div className="aisdemo--right-column">
          <GoogleMapsLoader
            apiKey="AIzaSyBawL8VbstJDdU5397SUX7pEt9DslAwWgQ"
            endpoint="https://maps.googleapis.com/maps/api/js?v=weekly"
          >
            {(google) => (
              <GeoSearch google={google} minZoom={2}>
                {({ hits }) => (
                  <Fragment>
                    {hits.map((hit) => (
                      <Marker key={hit.objectID} hit={hit} />
                    ))}
                    <ClearRefinements
                      className="ClearGeoRefinement"
                      transformItems={(items) =>
                        items.filter((item) => item.id === 'boundingBox')
                      }
                      translations={{
                        reset: 'Clear the map refinement',
                      }}
                    />
                  </Fragment>
                )}
              </GeoSearch>
            )}
          </GoogleMapsLoader>
        </div>
      </div>
    </Fragment>
  );
}

function Capacity() {
  return (
    <div className="row aisdemo-filter">
      <div className="col-sm-2 aisdemo-filter-title">Capacity</div>
      <div className="col-sm-3">
        <CapacitySelector
          attribute="person_capacity"
          items={[
            { label: '1 guest', start: 1, end: 1 },
            { label: '2 guests', start: 2, end: 2 },
            { label: '3 guests', start: 3, end: 3 },
            { label: '4 guests', start: 4, end: 4 },
            { label: '5 guests', start: 5, end: 5 },
            { label: '6 guests', start: 6, end: 6 },
          ]}
        />
      </div>
    </div>
  );
}

function OptionCapacity({ label, value }) {
  return <option value={value}>{label}</option>;
}

OptionCapacity.propTypes = {
  label: PropTypes.string,
  value: PropTypes.string,
};

const CapacitySelector = connectNumericMenu(
  ({ items, currentRefinement, refine }) => {
    const selectValue = (e) => refine(e.target.value);
    const options = items.map((item) => (
      <OptionCapacity
        label={item.label}
        value={item.value}
        isSelected={item.isRefined}
        key={item.value}
      />
    ));

    return (
      <div className="capacity-menu-wrapper">
        <select defaultValue={currentRefinement} onChange={selectValue}>
          {options}
        </select>
      </div>
    );
  }
);

function DatesAndGuest() {
  return (
    <div className="row aisdemo-filter">
      <div className="col-sm-2 aisdemo-filter-title">Dates</div>
      <div className="col-sm-3">
        <input className="date form-control" value="10/30/3015" disabled />
      </div>
      <div className="col-sm-3">
        <input className="date form-control" value="10/30/3015" disabled />
      </div>
      <div className="col-sm-3" />
    </div>
  );
}

const RoomType = connectRefinementList(({ items, refine }) => {
  const sortedItems = items.sort((i1, i2) => i1.label.localeCompare(i2.label));
  const hitComponents = sortedItems.map((item) => {
    const selectedClassName = item.isRefined
      ? ' ais-refinement-list--item__active'
      : '';
    const itemClassName = `ais-refinement-list--item col-sm-3 ${selectedClassName}`;
    return (
      <div className={itemClassName} key={item.label}>
        <div>
          <label
            className="ais-refinement-list--label"
            onClick={(e) => {
              e.preventDefault();
              refine(item.value);
            }}
          >
            <input
              type="checkbox"
              className="ais-refinement-list--checkbox"
              defaultChecked={item.isRefined ? 'checked' : ''}
            />
            {item.label}
            <span className="ais-refinement-list--count">{item.count}</span>
          </label>
        </div>
      </div>
    );
  });

  return (
    <div className="row aisdemo-filter">
      <div className="col-sm-2 aisdemo-filter-title">Room Type</div>
      <div id="room_types col-sm-3">{hitComponents}</div>
    </div>
  );
});

function Price() {
  return (
    <div className="row aisdemo-filter rheostat-container">
      <div className="col-sm-2 aisdemo-filter-title">Price Range</div>
      <div className="col-sm-9">
        <ConnectedRange attribute="price" />
      </div>
    </div>
  );
}

const MyHits = connectHits(({ hits }) => {
  const hs = hits.map((hit) => <HitComponent key={hit.objectID} hit={hit} />);
  return <div id="hits">{hs}</div>;
});

function HitComponent({ hit }) {
  return (
    <div className="hit col-sm-3">
      <div className="pictures-wrapper">
        <img className="picture" alt={hit.name} src={hit.picture_url} />
        <img
          className="profile"
          alt={hit.user.user.first_name}
          src={hit.user.user.thumbnail_url}
        />
      </div>
      <div className="infos">
        <h4 className="media-heading">
          <Highlight attribute="name" hit={hit} />
        </h4>
        <HitDescription hit={hit} />
      </div>
    </div>
  );
}

function HitDescription({ hit }) {
  return (
    <p>
      {hit.room_type} - <Highlight attribute="city" hit={hit} />,{' '}
      <Highlight attribute="country" hit={hit} />
    </p>
  );
}

HitComponent.propTypes = {
  hit: PropTypes.object,
};

function Results() {
  return (
    <div className="container-fluid" id="results">
      <div className="row">
        <MyHits />
      </div>
      <div className="row">
        <Pagination />
        <div className="thank-you">
          Data from <a href="https://www.airbnb.com/">airbnb.com</a>, user pics
          from <a href="https://randomuser.me/">randomuser.me</a>
        </div>
      </div>
    </div>
  );
}

class Range extends Component {
  static propTypes = {
    min: PropTypes.number,
    max: PropTypes.number,
    currentRefinement: PropTypes.object,
    refine: PropTypes.func.isRequired,
    canRefine: PropTypes.bool.isRequired,
  };

  state = { currentValues: { min: this.props.min, max: this.props.max } };

  componentDidUpdate(prevProps) {
    if (
      this.props.canRefine &&
      (prevProps.currentRefinement.min !== this.props.currentRefinement.min ||
        prevProps.currentRefinement.max !== this.props.currentRefinement.max)
    ) {
      this.setState({
        currentValues: {
          min: this.props.currentRefinement.min,
          max: this.props.currentRefinement.max,
        },
      });
    }
  }

  onValuesUpdated = (sliderState) => {
    this.setState({
      currentValues: { min: sliderState.values[0], max: sliderState.values[1] },
    });
  };

  onChange = (sliderState) => {
    if (
      this.props.currentRefinement.min !== sliderState.values[0] ||
      this.props.currentRefinement.max !== sliderState.values[1]
    ) {
      this.props.refine({
        min: sliderState.values[0],
        max: sliderState.values[1],
      });
    }
  };

  render() {
    const { min, max, currentRefinement } = this.props;
    const { currentValues } = this.state;
    return min !== max ? (
      <div>
        <Rheostat
          min={min}
          max={max}
          values={[currentRefinement.min, currentRefinement.max]}
          onChange={this.onChange}
          onValuesUpdated={this.onValuesUpdated}
        />
        <div className="rheostat-values">
          <div>{currentValues.min}</div>
          <div>{currentValues.max}</div>
        </div>
      </div>
    ) : null;
  }
}

const ConnectedRange = connectRange(Range);

export default withURLSync(App);