File size: 4,768 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
import React from 'react';
import PropTypes from 'prop-types';
import { storiesOf } from '@storybook/react';
import {
  InfiniteHits,
  Highlight,
  Panel,
  Snippet,
  Configure,
  connectInfiniteHits,
  createInfiniteHitsSessionStorageCache,
} from 'react-instantsearch-dom';
import { WrapWithHits } from './util';
import { action } from '@storybook/addon-actions';
import { connectHitInsights } from 'react-instantsearch-core';

const stories = storiesOf('InfiniteHits', module);

stories
  .add('default', () => (
    <WrapWithHits linkedStoryGroup="InfiniteHits.stories.js" pagination={false}>
      <InfiniteHits />
    </WrapWithHits>
  ))
  .add('with previous button', () => {
    const urlLogger = action('Routing state');
    return (
      <WrapWithHits
        linkedStoryGroup="InfiniteHits.stories.js"
        pagination={false}
        initialSearchState={{ page: 3 }}
        onSearchStateChange={({ configure, ...searchState }) => {
          urlLogger(JSON.stringify(searchState, null, 2));
        }}
      >
        <InfiniteHits showPrevious={true} />
      </WrapWithHits>
    );
  })
  .add('with custom rendering', () => {
    const urlLogger = action('Routing state');

    const MyInfiniteHits = ({
      hits,
      hasMore,
      hasPrevious,
      refine,
      refinePrevious,
    }) => (
      <div>
        <button disabled={!hasPrevious} onClick={refinePrevious}>
          Show previous
        </button>
        <ol>
          {hits.map((hit) => (
            <li key={hit.objectID}>{hit.name}</li>
          ))}
        </ol>
        <button disabled={!hasMore} onClick={refine}>
          Show more
        </button>
      </div>
    );

    MyInfiniteHits.propTypes = {
      hits: PropTypes.array.isRequired,
      hasMore: PropTypes.bool.isRequired,
      hasPrevious: PropTypes.bool.isRequired,
      refine: PropTypes.func.isRequired,
      refinePrevious: PropTypes.func.isRequired,
    };

    const CustomInfiniteHits = connectInfiniteHits(MyInfiniteHits);

    return (
      <WrapWithHits
        linkedStoryGroup="InfiniteHits.stories.js"
        pagination={false}
        initialSearchState={{ page: 3 }}
        onSearchStateChange={({ configure, ...searchState }) => {
          urlLogger(JSON.stringify(searchState, null, 2));
        }}
      >
        <CustomInfiniteHits />
      </WrapWithHits>
    );
  })
  .add('with custom hitComponent', () => {
    function Product({ hit }) {
      return (
        <div>
          <Highlight attribute="name" hit={hit} />
          <p>
            <Highlight attribute="type" hit={hit} />
          </p>
          <p>
            <Snippet attribute="description" hit={hit} />
          </p>
        </div>
      );
    }

    Product.propTypes = {
      hit: PropTypes.object.isRequired,
    };

    return (
      <WrapWithHits linkedStoryGroup="InfiniteHits.stories.js">
        <InfiniteHits hitComponent={Product} />
      </WrapWithHits>
    );
  })
  .add('with Panel', () => (
    <WrapWithHits linkedStoryGroup="InfiniteHits.stories.js" pagination={false}>
      <Panel header="Infinite hits" footer="Footer">
        <InfiniteHits />
      </Panel>
    </WrapWithHits>
  ))
  .add('with Insights', () => {
    const insightsClient = (method, payload) =>
      action(`[InsightsClient] sent ${method} with payload`)(payload);
    const ProductWithInsights = connectHitInsights(insightsClient)(Product);

    function Product({ hit, insights }) {
      return (
        <div>
          <Highlight attribute="name" hit={hit} />
          <button
            onClick={() =>
              insights('clickedObjectIDsAfterSearch', {
                eventName: 'Add to cart',
              })
            }
          >
            Add to cart
          </button>
        </div>
      );
    }
    Product.propTypes = {
      hit: PropTypes.object.isRequired,
      insights: PropTypes.func.isRequired,
    };
    return (
      <WrapWithHits linkedStoryGroup="Hits.stories.js">
        <Configure clickAnalytics />
        <InfiniteHits hitComponent={ProductWithInsights} />
      </WrapWithHits>
    );
  })
  .add('with sessionStorage cache', () => {
    function Product({ hit }) {
      return (
        <div>
          <span>#{hit.__position}. </span>
          <Highlight attribute="name" hit={hit} />
          <p>
            <a href="https://google.com">Details</a>
          </p>
        </div>
      );
    }
    Product.propTypes = {
      hit: PropTypes.object.isRequired,
    };

    return (
      <WrapWithHits linkedStoryGroup="InfiniteHits.stories.js">
        <Configure hitsPerPage={16} />
        <InfiniteHits
          hitComponent={Product}
          cache={createInfiniteHitsSessionStorageCache()}
        />
      </WrapWithHits>
    );
  });