summaryrefslogtreecommitdiffstats
path: root/browser/components/aboutlogins/content/aboutLoginsImportReport.mjs
blob: 38002563826f6f37968fec2ece2f8f5b540f7183 (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
/* 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 ImportDetailsRow from "./components/import-details-row.mjs";

const detailsLoginsList = document.querySelector(".logins-list");
const detailedNewCount = document.querySelector(".new-logins");
const detailedExitingCount = document.querySelector(".exiting-logins");
const detailedDuplicateCount = document.querySelector(".duplicate-logins");
const detailedErrorsCount = document.querySelector(".errors-logins");

document.dispatchEvent(
  new CustomEvent("AboutLoginsImportReportInit", { bubbles: true })
);

function importReportDataHandler(event) {
  switch (event.detail.messageType) {
    case "ImportReportData":
      const logins = event.detail.value;
      const report = {
        added: 0,
        modified: 0,
        no_change: 0,
        error: 0,
      };
      for (let loginRow of logins) {
        if (loginRow.result.includes("error")) {
          report.error++;
        } else {
          report[loginRow.result]++;
        }
      }
      document.l10n.setAttributes(
        detailedNewCount,
        "about-logins-import-report-added",
        { count: report.added }
      );
      document.l10n.setAttributes(
        detailedExitingCount,
        "about-logins-import-report-modified",
        { count: report.modified }
      );
      document.l10n.setAttributes(
        detailedDuplicateCount,
        "about-logins-import-report-no-change",
        { count: report.no_change }
      );
      document.l10n.setAttributes(
        detailedErrorsCount,
        "about-logins-import-report-error",
        { count: report.error }
      );
      if (report.no_change > 0) {
        detailedDuplicateCount
          .querySelector(".not-imported")
          .classList.toggle("not-imported-hidden");
      }
      if (report.error > 0) {
        detailedErrorsCount
          .querySelector(".not-imported")
          .classList.toggle("not-imported-hidden");
      }

      detailsLoginsList.innerHTML = "";
      let fragment = document.createDocumentFragment();
      for (let index = 0; index < logins.length; index++) {
        const row = new ImportDetailsRow(index + 1, logins[index]);
        fragment.appendChild(row);
      }
      detailsLoginsList.appendChild(fragment);
      window.removeEventListener(
        "AboutLoginsChromeToContent",
        importReportDataHandler
      );
      document.dispatchEvent(
        new CustomEvent("AboutLoginsImportReportReady", { bubbles: true })
      );
      break;
  }
}

window.addEventListener("AboutLoginsChromeToContent", importReportDataHandler);