Newer
Older
veriq / frontend / assets / me.js
/* VerIQ cabinet: active result + connected accounts */
(function () {
  "use strict";

  var $ = function (id) { return document.getElementById(id); };
  var esc = function (s) {
    return String(s).replace(/[&<>"']/g, function (c) {
      return { "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&#39;" }[c];
    });
  };

  fetch("/api/me").then(function (r) { return r.json(); }).then(function (d) {
    if (!d || !d.identity) { window.location.href = "/login"; return; }

    $("whoami").innerHTML = "Logged in as <b style=\"color:var(--accent)\">" +
      esc(d.identity) + "</b>";

    // active result
    var ab = $("activeBody");
    if (d.active_result) {
      var r = d.active_result;
      ab.innerHTML =
        '<div style="display:flex;align-items:baseline;gap:14px;flex-wrap:wrap">' +
        '<span style="font-size:44px;font-weight:700">IQ ' + esc(r.iq) + "</span>" +
        '<span style="color:var(--muted)">higher than ' + esc(r.percentile) +
        "% of test takers</span></div>" +
        '<div style="margin-top:8px;color:var(--muted);font-size:14px">' +
        "completed " + esc(r.completed_at) +
        (r.identity ? " · as " + esc(r.identity) : "") + "</div>" +
        '<div style="margin-top:12px"><a class="btn ghost" href="/result/' +
        esc(r.result_id) + '">Open result page →</a> ' +
        '<a class="btn ghost" href="/v/' + esc(r.result_id) +
        '">Public verify link →</a></div>';
    } else {
      ab.innerHTML = '<p style="color:var(--muted)">No result yet — take the test.</p>';
    }

    // connected identities
    var box = $("identities");
    var provs = {};
    (d.identities || []).forEach(function (i) { provs[i.provider] = i.username; });
    var rows = [];
    ["x", "ig"].forEach(function (p) {
      var label = p === "x" ? "X (Twitter)" : "Instagram";
      if (provs[p]) {
        rows.push('<div style="display:flex;justify-content:space-between;gap:10px;' +
          'padding:10px 12px;border:1px solid var(--line);border-radius:8px">' +
          "<span><b>" + label + "</b> · @" + esc(provs[p]) + "</span>" +
          '<span style="color:var(--good)">✓ connected</span></div>');
      } else {
        rows.push('<div style="display:flex;justify-content:space-between;gap:10px;' +
          'padding:10px 12px;border:1px dashed var(--line);border-radius:8px;' +
          'color:var(--muted)"><span><b>' + label + "</b></span><span>not connected</span></div>");
      }
    });
    box.innerHTML = rows.join("");

    // link buttons: hide already-connected, disable unconfigured providers
    if (provs.x) { $("linkX").style.display = "none"; }
    if (provs.ig) { $("linkIg").style.display = "none"; }
    if (d.ig_configured === false) {
      $("linkIg").style.display = "none";
    }
    if (d.x_configured === false) {
      $("linkX").style.display = "none";
    }
  }).catch(function () {
    window.location.href = "/login";
  });
})();