File size: 2,699 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
import React, { useState } from 'react';
import { storiesOf } from '@storybook/react';
import {
  Configure,
  ExperimentalConfigureRelatedItems,
  Hits,
  Index,
  connectPagination,
} from 'react-instantsearch-dom';
import { WrapWithHits } from './util';

type Hit = any;

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

stories.add('default', () => <ConfigureRelatedItemsExample />);

function RelatedHit({ hit }: { hit: Hit }) {
  return (
    <div>
      <div className="ais-RelatedHits-item-image">
        <img src={hit.image} alt={hit.name} />
      </div>

      <div className="ais-RelatedHits-item-title">
        <h4>{hit.name}</h4>
      </div>
    </div>
  );
}

const PreviousPagination = connectPagination(
  ({ currentRefinement, refine }) => {
    return (
      <button
        className="ais-RelatedHits-button"
        disabled={currentRefinement === 1}
        onClick={() => {
          refine(currentRefinement - 1);
        }}
      >

      </button>
    );
  }
);

const NextPagination = connectPagination(
  ({ currentRefinement, refine, nbPages }) => {
    return (
      <button
        className="ais-RelatedHits-button"
        disabled={currentRefinement + 1 === nbPages}
        onClick={() => {
          refine(currentRefinement + 1);
        }}
      >

      </button>
    );
  }
);

function ConfigureRelatedItemsExample() {
  const [referenceHit, setReferenceHit] = useState<Hit | null>(null);

  const ReferenceHit = React.memo<{ hit: Hit }>(
    ({ hit }) => {
      return (
        <div ref={() => setReferenceHit(hit)}>
          <img src={hit.image} alt={hit.name} />
          <h3>{hit.name}</h3>
        </div>
      );
    },
    (prevProps, nextProps) => {
      return prevProps.hit.objectID === nextProps.hit.objectID;
    }
  );

  return (
    <WrapWithHits linkedStoryGroup="ConfigureRelatedItems.stories.tsx">
      <Index indexName="instant_search" indexId="mainIndex">
        <Configure hitsPerPage={1} />

        <Hits hitComponent={ReferenceHit} />
      </Index>

      {referenceHit && (
        <Index indexName="instant_search" indexId="relatedIndex">
          <Configure hitsPerPage={4} />
          <ExperimentalConfigureRelatedItems
            hit={referenceHit}
            matchingPatterns={{
              brand: { score: 3 },
              type: { score: 10 },
              categories: { score: 2 },
            }}
          />

          <h2>Related items</h2>

          <div className="related-items">
            <PreviousPagination />
            <Hits hitComponent={RelatedHit} />
            <NextPagination />
          </div>
        </Index>
      )}
    </WrapWithHits>
  );
}