File size: 3,715 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
import React from 'react';
import { storiesOf } from '@storybook/react';
import { connectHits } from 'react-instantsearch-core';
import { QueryRuleCustomData, Panel, Highlight } from 'react-instantsearch-dom';
import { WrapWithHits } from './util';

type CustomDataItem = {
  title: string;
  banner: string;
  link: string;
};

type MovieHit = {
  actors: string[];
  color: string;
  genre: string[];
  image: string;
  objectID: string;
  score: number;
  title: string;
};

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

const StoryHits = connectHits(({ hits }: { hits: MovieHit[] }) => (
  <div className="hits">
    {hits.map((hit) => (
      <div key={hit.objectID} className="hit">
        <div className="hit-picture">
          <img src={hit.image} />
        </div>

        <div className="hit-content">
          <div>
            <Highlight attribute="title" hit={hit} />
          </div>
        </div>
      </div>
    ))}
  </div>
));

const storyProps = {
  appId: 'latency',
  apiKey: 'af044fb0788d6bb15f807e4420592bc5',
  indexName: 'instant_search_movies',
  linkedStoryGroup: 'QueryRuleCustomData.stories.tsx',
  hitsElement: <StoryHits />,
};

stories
  .add('default', () => (
    <WrapWithHits {...storyProps}>
      <p>
        Type <q>music</q> and a banner will appear.
      </p>

      <QueryRuleCustomData>
        {({ items }: { items: CustomDataItem[] }) =>
          items.map(({ banner, title, link }) => {
            if (!banner) {
              return null;
            }

            return (
              <section key={title}>
                <h2>{title}</h2>

                <a href={link}>
                  <img src={banner} alt={title} />
                </a>
              </section>
            );
          })
        }
      </QueryRuleCustomData>
    </WrapWithHits>
  ))
  .add('with default banner', () => (
    <WrapWithHits {...storyProps}>
      <p>
        Kill Bill appears whenever no other results are promoted. Type{' '}
        <q>music</q> to see another movie promoted.
      </p>

      <QueryRuleCustomData
        transformItems={(items: CustomDataItem[]) => {
          if (items.length > 0) {
            return items;
          }

          return [
            {
              title: 'Kill Bill',
              banner: 'http://static.bobatv.net/IMovie/mv_2352/poster_2352.jpg',
              link: 'https://www.netflix.com/title/60031236',
            },
          ];
        }}
      >
        {({ items }: { items: CustomDataItem[] }) =>
          items.map(({ banner, title, link }) => {
            if (!banner) {
              return null;
            }

            return (
              <section key={title}>
                <h2>{title}</h2>

                <a href={link}>
                  <img src={banner} alt={title} />
                </a>
              </section>
            );
          })
        }
      </QueryRuleCustomData>
    </WrapWithHits>
  ))
  .add('with Panel', () => (
    <WrapWithHits {...storyProps}>
      <p>
        Type <q>music</q> and a banner will appear.
      </p>

      <Panel header="QueryRuleCustomData" footer="footer">
        <QueryRuleCustomData>
          {({ items }: { items: CustomDataItem[] }) =>
            items.map(({ banner, title, link }) => {
              if (!banner) {
                return null;
              }

              return (
                <section key={title}>
                  <h2>{title}</h2>

                  <a href={link}>
                    <img src={banner} alt={title} />
                  </a>
                </section>
              );
            })
          }
        </QueryRuleCustomData>
      </Panel>
    </WrapWithHits>
  ));