summaryrefslogtreecommitdiffstats
path: root/browser/components/newtab/content-src/components/TopSites/SearchShortcutsForm.jsx
blob: 4324c019f6a08517f4a40e745d1ee5c79311ff39 (plain)
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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

import {
  actionCreators as ac,
  actionTypes as at,
} from "common/Actions.sys.mjs";
import React from "react";
import { TOP_SITES_SOURCE } from "./TopSitesConstants";

export class SelectableSearchShortcut extends React.PureComponent {
  render() {
    const { shortcut, selected } = this.props;
    const imageStyle = { backgroundImage: `url("${shortcut.tippyTopIcon}")` };
    return (
      <div className="top-site-outer search-shortcut">
        <input
          type="checkbox"
          id={shortcut.keyword}
          name={shortcut.keyword}
          checked={selected}
          onChange={this.props.onChange}
        />
        <label htmlFor={shortcut.keyword}>
          <div className="top-site-inner">
            <span>
              <div className="tile">
                <div
                  className="top-site-icon rich-icon"
                  style={imageStyle}
                  data-fallback="@"
                />
                <div className="top-site-icon search-topsite" />
              </div>
              <div className="title">
                <span dir="auto">{shortcut.keyword}</span>
              </div>
            </span>
          </div>
        </label>
      </div>
    );
  }
}

export class SearchShortcutsForm extends React.PureComponent {
  constructor(props) {
    super(props);
    this.handleChange = this.handleChange.bind(this);
    this.onCancelButtonClick = this.onCancelButtonClick.bind(this);
    this.onSaveButtonClick = this.onSaveButtonClick.bind(this);

    // clone the shortcuts and add them to the state so we can add isSelected property
    const shortcuts = [];
    const { rows, searchShortcuts } = props.TopSites;
    searchShortcuts.forEach(shortcut => {
      shortcuts.push({
        ...shortcut,
        isSelected: !!rows.find(
          row =>
            row &&
            row.isPinned &&
            row.searchTopSite &&
            row.label === shortcut.keyword
        ),
      });
    });
    this.state = { shortcuts };
  }

  handleChange(event) {
    const { target } = event;
    const { name, checked } = target;
    this.setState(prevState => {
      const shortcuts = prevState.shortcuts.slice();
      let shortcut = shortcuts.find(({ keyword }) => keyword === name);
      shortcut.isSelected = checked;
      return { shortcuts };
    });
  }

  onCancelButtonClick(ev) {
    ev.preventDefault();
    this.props.onClose();
  }

  onSaveButtonClick(ev) {
    ev.preventDefault();

    // Check if there were any changes and act accordingly
    const { rows } = this.props.TopSites;
    const pinQueue = [];
    const unpinQueue = [];
    this.state.shortcuts.forEach(shortcut => {
      const alreadyPinned = rows.find(
        row =>
          row &&
          row.isPinned &&
          row.searchTopSite &&
          row.label === shortcut.keyword
      );
      if (shortcut.isSelected && !alreadyPinned) {
        pinQueue.push(this._searchTopSite(shortcut));
      } else if (!shortcut.isSelected && alreadyPinned) {
        unpinQueue.push({
          url: alreadyPinned.url,
          searchVendor: shortcut.shortURL,
        });
      }
    });

    // Tell the feed to do the work.
    this.props.dispatch(
      ac.OnlyToMain({
        type: at.UPDATE_PINNED_SEARCH_SHORTCUTS,
        data: {
          addedShortcuts: pinQueue,
          deletedShortcuts: unpinQueue,
        },
      })
    );

    // Send the Telemetry pings.
    pinQueue.forEach(shortcut => {
      this.props.dispatch(
        ac.UserEvent({
          source: TOP_SITES_SOURCE,
          event: "SEARCH_EDIT_ADD",
          value: { search_vendor: shortcut.searchVendor },
        })
      );
    });
    unpinQueue.forEach(shortcut => {
      this.props.dispatch(
        ac.UserEvent({
          source: TOP_SITES_SOURCE,
          event: "SEARCH_EDIT_DELETE",
          value: { search_vendor: shortcut.searchVendor },
        })
      );
    });

    this.props.onClose();
  }

  _searchTopSite(shortcut) {
    return {
      url: shortcut.url,
      searchTopSite: true,
      label: shortcut.keyword,
      searchVendor: shortcut.shortURL,
    };
  }

  render() {
    return (
      <form className="topsite-form">
        <div className="search-shortcuts-container">
          <h3
            className="section-title grey-title"
            data-l10n-id="newtab-topsites-add-search-engine-header"
          />
          <div>
            {this.state.shortcuts.map(shortcut => (
              <SelectableSearchShortcut
                key={shortcut.keyword}
                shortcut={shortcut}
                selected={shortcut.isSelected}
                onChange={this.handleChange}
              />
            ))}
          </div>
        </div>
        <section className="actions">
          <button
            className="cancel"
            type="button"
            onClick={this.onCancelButtonClick}
            data-l10n-id="newtab-topsites-cancel-button"
          />
          <button
            className="done"
            type="submit"
            onClick={this.onSaveButtonClick}
            data-l10n-id="newtab-topsites-save-button"
          />
        </section>
      </form>
    );
  }
}