summaryrefslogtreecommitdiffstats
path: root/browser/components/newtab/content-src/asrouter/templates/EOYSnippet/EOYSnippet.jsx
blob: f324a69853947613e4f788bedd03624407daff76 (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
/* 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 React from "react";
import { SimpleSnippet } from "../SimpleSnippet/SimpleSnippet";

class EOYSnippetBase extends React.PureComponent {
  constructor(props) {
    super(props);
    this.handleSubmit = this.handleSubmit.bind(this);
  }

  /**
   * setFrequencyValue - `frequency` form parameter value should be `monthly`
   *                     if `monthly-checkbox` is selected or `single` otherwise
   */
  setFrequencyValue() {
    const frequencyCheckbox = this.refs.form.querySelector("#monthly-checkbox");
    if (frequencyCheckbox.checked) {
      this.refs.form.querySelector("[name='frequency']").value = "monthly";
    }
  }

  handleSubmit(event) {
    event.preventDefault();
    this.props.sendClick(event);
    this.setFrequencyValue();
    if (!this.props.content.do_not_autoblock) {
      this.props.onBlock();
    }
    this.refs.form.submit();
  }

  renderDonations() {
    const fieldNames = ["first", "second", "third", "fourth"];
    const numberFormat = new Intl.NumberFormat(
      this.props.content.locale || navigator.language,
      {
        style: "currency",
        currency: this.props.content.currency_code,
        minimumFractionDigits: 0,
      }
    );
    // Default to `second` button
    const { selected_button } = this.props.content;
    const btnStyle = {
      color: this.props.content.button_color,
      backgroundColor: this.props.content.button_background_color,
    };
    const donationURLParams = [];
    const paramsStartIndex = this.props.content.donation_form_url.indexOf("?");
    for (const entry of new URLSearchParams(
      this.props.content.donation_form_url.slice(paramsStartIndex)
    ).entries()) {
      donationURLParams.push(entry);
    }

    return (
      <form
        className="EOYSnippetForm"
        action={this.props.content.donation_form_url}
        method={this.props.form_method}
        onSubmit={this.handleSubmit}
        data-metric="EOYSnippetForm"
        ref="form"
      >
        {donationURLParams.map(([key, value], idx) => (
          <input type="hidden" name={key} value={value} key={idx} />
        ))}
        {fieldNames.map((field, idx) => {
          const button_name = `donation_amount_${field}`;
          const amount = this.props.content[button_name];
          return (
            <React.Fragment key={idx}>
              <input
                type="radio"
                name="amount"
                value={amount}
                id={field}
                defaultChecked={button_name === selected_button}
              />
              <label htmlFor={field} className="donation-amount">
                {numberFormat.format(amount)}
              </label>
            </React.Fragment>
          );
        })}

        <div className="monthly-checkbox-container">
          <input id="monthly-checkbox" type="checkbox" />
          <label htmlFor="monthly-checkbox">
            {this.props.content.monthly_checkbox_label_text}
          </label>
        </div>

        <input type="hidden" name="frequency" value="single" />
        <input
          type="hidden"
          name="currency"
          value={this.props.content.currency_code}
        />
        <input
          type="hidden"
          name="presets"
          value={fieldNames.map(
            field => this.props.content[`donation_amount_${field}`]
          )}
        />
        <button
          style={btnStyle}
          type="submit"
          className="ASRouterButton primary donation-form-url"
        >
          {this.props.content.button_label}
        </button>
      </form>
    );
  }

  render() {
    const textStyle = {
      color: this.props.content.text_color,
      backgroundColor: this.props.content.background_color,
    };
    const customElement = (
      <em style={{ backgroundColor: this.props.content.highlight_color }} />
    );
    return (
      <SimpleSnippet
        {...this.props}
        className={this.props.content.test}
        customElements={{ em: customElement }}
        textStyle={textStyle}
        extraContent={this.renderDonations()}
      />
    );
  }
}

export const EOYSnippet = props => {
  const extendedContent = {
    monthly_checkbox_label_text: "Make my donation monthly",
    locale: "en-US",
    currency_code: "usd",
    selected_button: "donation_amount_second",
    ...props.content,
  };

  return (
    <EOYSnippetBase {...props} content={extendedContent} form_method="GET" />
  );
};