summaryrefslogtreecommitdiffstats
path: root/browser/components/payments/res/containers/shipping-option-picker.js
blob: 01a97f1ac4d957d2deb4254252f43b4900c178cf (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
/* 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 RichPicker from "./rich-picker.js";
import ShippingOption from "../components/shipping-option.js";
import HandleEventMixin from "../mixins/HandleEventMixin.js";

/**
 * <shipping-option-picker></shipping-option-picker>
 * Container around <rich-select> with
 * <option> listening to shippingOptions.
 */

export default class ShippingOptionPicker extends HandleEventMixin(RichPicker) {
  constructor() {
    super();
    this.dropdown.setAttribute("option-type", "shipping-option");
  }

  render(state) {
    this.addLink.hidden = true;
    this.editLink.hidden = true;

    // If requestShipping is true but paymentDetails.shippingOptions isn't defined
    // then use an empty array as a fallback.
    let shippingOptions = state.request.paymentDetails.shippingOptions || [];
    let desiredOptions = [];
    for (let option of shippingOptions) {
      let optionEl = this.dropdown.getOptionByValue(option.id);
      if (!optionEl) {
        optionEl = document.createElement("option");
        optionEl.value = option.id;
      }

      optionEl.setAttribute("label", option.label);
      optionEl.setAttribute("amount-currency", option.amount.currency);
      optionEl.setAttribute("amount-value", option.amount.value);

      optionEl.textContent = ShippingOption.formatSingleLineLabel(option);
      desiredOptions.push(optionEl);
    }

    this.dropdown.popupBox.textContent = "";
    for (let option of desiredOptions) {
      this.dropdown.popupBox.appendChild(option);
    }

    // Update selectedness after the options are updated
    let selectedShippingOption = state.selectedShippingOption;
    this.dropdown.value = selectedShippingOption;

    if (
      selectedShippingOption &&
      selectedShippingOption !== this.dropdown.popupBox.value
    ) {
      throw new Error(
        `The option ${selectedShippingOption} ` +
          `does not exist in the shipping option picker`
      );
    }
  }

  onChange(event) {
    let selectedOptionId = this.dropdown.value;
    this.requestStore.setState({
      selectedShippingOption: selectedOptionId,
    });
  }
}

customElements.define("shipping-option-picker", ShippingOptionPicker);