File size: 4,522 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
import React, { Component } from 'react';
import { storiesOf } from '@storybook/react';
import algoliasearch from 'algoliasearch/lite';
import { InstantSearch, SearchBox, Configure } from 'react-instantsearch-dom';
import { CustomHits, Content } from './util';

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

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

class AppWithRefresh extends Component {
  state = {
    refresh: false,
  };

  refresh = () => {
    this.setState({ refresh: true }, () => {
      this.setState({ refresh: false });
    });
  };

  render() {
    return (
      <InstantSearch
        indexName="instant_search"
        searchClient={searchClient}
        refresh={this.state.refresh}
      >
        <Configure hitsPerPage={5} />
        <div>
          <h2
            style={{
              color: '#182359',
              font: 'Raleway',
              size: '30px',
              lineHeight: '1',
              fontWeight: '200',
            }}
          >
            Feature: Refresh cache
          </h2>
          <p style={{ color: '#182359', font: 'open sans' }}>
            Adding the refresh prop to your InstantSearch component gives you
            the possibility to refresh the cache.
          </p>

          <h3
            style={{
              color: '#182359',
              font: 'Raleway',
              size: '18px',
              lineHeight: '1',
              fontWeight: 'bold',
            }}
          >
            How to test it?
          </h3>
          <div style={{ color: '#182359', font: 'open sans' }}>
            By default, the &apos;refresh&apos; prop is disabled. You will need
            to open your network tab in the developer tools.
            <ol>
              <li>
                Type a query in the SearchBox (for instance
                &apos;cellphones&apos;). You should see 10 requests to Algolia
                (one per letter)
              </li>
              <li>
                Type &apos;cellphones&apos; again, you will see that no
                additional query is made since the results are retrieved from
                the cache
              </li>
              <li>
                Make sure the SearchBox is empty, click on the &apos;Refresh
                cache&apos; button (you should see that Refresh set to true in
                the info box below the SearchBox)
              </li>
              <li>
                Type your previous query again: the cache has been cleared and
                you will see new requests made to Algolia
              </li>
            </ol>
          </div>
        </div>
        <hr />
        <SearchBox
          translations={{
            placeholder: 'Search our furnitures: chairs, tables etc.',
          }}
        />
        <button
          onClick={this.refresh}
          style={{
            borderRadius: '4px',
            padding: '10px',
            border: 'none',
            fontSize: '12px',
            cursor: 'pointer',
            color: '#fff',
            background: '#3369e7',
            marginTop: '10px',
          }}
        >
          Refresh cache
        </button>
        <CustomHits />
      </InstantSearch>
    );
  }
}

stories.add('with a refresh button', () => (
  <Content linkedStoryGroup="RefreshCache.stories.js">
    <AppWithRefresh />
  </Content>
));

class App extends Component {
  state = {
    refresh: false,
    count: 0,
  };

  componentDidMount() {
    this.interval = setInterval(
      () =>
        this.setState(
          (prevState) => ({
            refresh: prevState.count === 5,
            count: prevState.count === 5 ? 0 : prevState.count + 1,
          }),
          () => {
            this.setState({
              refresh: false,
            });
          }
        ),
      1000
    );
  }

  componentWillUnmount() {
    clearInterval(this.interval);
  }

  render() {
    return (
      <InstantSearch
        indexName="instant_search"
        searchClient={searchClient}
        refresh={this.state.refresh}
      >
        <Configure hitsPerPage={5} />

        <span>The cache is refreshed every 5 seconds.</span>

        <SearchBox
          translations={{
            placeholder: 'Search our products: phones, tv, etc.',
          }}
        />

        <CustomHits />
      </InstantSearch>
    );
  }
}

stories.add('with setInterval', () => (
  <Content linkedStoryGroup="RefreshCache.stories.js">
    <App />
  </Content>
));