Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOURCE FILE4 * =============================================================================5 * Project : MacLustr Identity6 * File : integrations/demo-marketplace/public/app.js7 * Author : Simon-Pierre Boucher | https://spboucher.ai8 * Contact : admin@spboucher.ai9 * Copyright : (c) 2026 Simon-Pierre Boucher10 * License : None specified (no license file in this repository)11 * =============================================================================12 * END OF SP BOUCHER SOURCE HEADER13 * =============================================================================14 */1516// Browser code of the demo. It holds no key: it talks to the Brocante server, which talks to MacLustr Identity.17import { openVerification } from "/vendor/maclustr-identity-browser.js";1819const $ = (id) => document.getElementById(id);20const api = async (path, options = {}) => {21 const res = await fetch(path, { ...options, headers: { "Content-Type": "application/json", ...(options.headers ?? {}) }, body: options.body ? JSON.stringify(options.body) : undefined });22 return { status: res.status, data: await res.json().catch(() => ({})) };23};24const el = (tag, props = {}, ...children) => {25 const node = Object.assign(document.createElement(tag), props);26 node.append(...children);27 return node;28};29const money = (n) => new Intl.NumberFormat("en-CA", { style: "currency", currency: "CAD" }).format(n);3031const MESSAGES = {32 identity_verification_required: "Publishing is reserved for sellers with a verified identity. Your draft is saved: verify your identity, then publish.",33 identity_verification_pending: "Your verification is being reviewed by a person. Your draft is saved; publishing will open once the review is complete.",34 identity_unavailable: "The identity service cannot be reached right now, so publishing stays closed. Your draft is saved. Try again in a moment.",35};36const STATUS = {37 unverified: ["no", "Identity not verified", "Verify your identity once to publish listings."],38 verified: ["ok", "Identity verified", "You can publish listings."],39 manual_review: ["wait", "Verification under review", "A person is reviewing your verification. This can take a little time."],40 inconclusive: ["no", "Verification not conclusive", "The check could not conclude. You can try again."],41 rejected: ["no", "Verification refused", "The check was refused. You can try again with a valid document."],42 revoked: ["no", "Verification revoked", "Your previous verification is no longer valid. Verify again to publish."],43};4445let me = null;4647function show(node, text) {48 node.textContent = text ?? "";49 node.hidden = !text;50}5152async function loadPublic() {53 const { data } = await api("/api/public/listings");54 const list = $("public");55 list.replaceChildren();56 if (!data.listings?.length) list.append(el("li", { className: "muted", textContent: "Nothing published yet." }));57 for (const l of data.listings ?? []) {58 const seller = el("span", { className: "seller" }, `Sold by ${l.seller}`);59 if (l.seller_identity_verified) seller.append(el("span", { className: "badge", textContent: "Identity verified", title: "See the explanation below the listings" }));60 list.append(el("li", {}, el("div", {}, el("strong", { textContent: l.title }), el("br"), seller), el("span", { className: "price", textContent: money(l.price) })));61 }62}6364function render() {65 $("login").hidden = Boolean(me);66 $("seller").hidden = !me;67 const session = $("session");68 session.replaceChildren();69 if (!me) return;70 const out = el("button", { className: "btn", type: "button", textContent: "Switch seller" });71 out.addEventListener("click", async () => {72 await api("/api/logout", { method: "POST" });73 me = null;74 render();75 });76 session.append(`Signed in as ${me.name}`, out);7778 const [cls, label, help] = STATUS[me.identity.status] ?? STATUS.unverified;79 const expired = me.identity.status === "verified" && !me.identity_verified;80 $("status-line").className = `status ${expired ? "no" : cls}`;81 $("status-line").textContent = expired ? "Verification expired" : label;82 $("status-help").textContent = expired ? "Verify again to keep publishing." : help;83 $("verify").hidden = me.identity_verified || me.identity.status === "manual_review";8485 const mine = $("mine");86 mine.replaceChildren();87 if (!me.listings.length) mine.append(el("li", { className: "muted", textContent: "No listing yet. Start with a draft above." }));88 for (const l of me.listings) {89 const right = el("div", { className: "row" }, el("span", { className: `pill ${l.status}`, textContent: l.status }));90 if (l.status === "draft") {91 const b = el("button", { className: "btn", type: "button", textContent: "Publish" });92 b.addEventListener("click", () => publish(l.id));93 right.append(b);94 }95 mine.append(el("li", {}, el("span", {}, el("strong", { textContent: l.title }), ` — ${money(l.price)}`), right));96 }97}9899async function reload() {100 const { status, data } = await api("/api/me");101 me = status === 200 ? data : null;102 render();103 await loadPublic();104}105106async function publish(id) {107 show($("publish-message"), "");108 const { status, data } = await api(`/api/listings/${id}/publish`, { method: "POST" });109 if (status !== 200) show($("publish-message"), MESSAGES[data.error] ?? "This listing could not be published.");110 await reload();111}112113/** After the hosted page closes, the server record changes when the webhook arrives. Wait for it, then ask the server to catch up. */114async function waitForDecision(previous) {115 for (let i = 0; i < 12; i++) {116 await new Promise((r) => setTimeout(r, 500));117 await reload();118 if (me && me.identity.status !== previous) return;119 }120 await api("/api/identity/refresh", { method: "POST" });121 await reload();122}123124let verifying = false;125// The button is marked busy instead of disabled: focus has to come back to it when the dialog closes.126function setVerifying(on) {127 verifying = on;128 $("verify").setAttribute("aria-disabled", String(on));129}130131$("verify").addEventListener("click", async () => {132 if (verifying) return;133 setVerifying(true);134 show($("flow-message"), "");135 const { status, data } = await api("/api/identity/session", { method: "POST" });136 if (status !== 200) {137 show($("flow-message"), "The identity service cannot be reached right now. Nothing was started; try again in a moment.");138 setVerifying(false);139 return;140 }141 const previous = me.identity.status;142 openVerification({143 url: data.url,144 mode: "modal",145 title: "Identity verification for Brocante",146 onComplete: async () => {147 // The page says it is done; the decision that counts is the one our server holds.148 show($("flow-message"), "Thank you. Checking the result...");149 await waitForDecision(previous);150 show($("flow-message"), "");151 setVerifying(false);152 },153 onCancel: () => {154 show($("flow-message"), "Verification was not completed. You can start again whenever you want.");155 setVerifying(false);156 },157 onError: () => show($("flow-message"), "The verification page reported a problem. You can close it and try again."),158 });159});160161$("refresh").addEventListener("click", async () => {162 const { status } = await api("/api/identity/refresh", { method: "POST" });163 show($("flow-message"), status === 503 ? "The identity service cannot be reached right now." : "");164 await reload();165});166167$("new-listing").addEventListener("submit", async (e) => {168 e.preventDefault();169 const form = new FormData(e.target);170 const { status } = await api("/api/listings", { method: "POST", body: { title: form.get("title"), price: Number(form.get("price")) } });171 if (status === 201) e.target.reset();172 await reload();173});174175(async () => {176 const { data } = await api("/api/users");177 for (const u of data.users) {178 const b = el("button", { className: "btn", type: "button", textContent: u.name });179 b.addEventListener("click", async () => {180 await api("/api/login", { method: "POST", body: { user: u.id } });181 await reload();182 });183 $("users").append(b);184 }185 await reload();186})();187