From ddd7e9cfb01924ce7e95161b56abd67162e827c6 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Mon, 30 Aug 2021 16:05:25 -0300 Subject: head and body split, handle request params --- packages/backend/src/pages/ShowOrderDetails.tsx | 129 ++++++++++-------------- 1 file changed, 55 insertions(+), 74 deletions(-) (limited to 'packages/backend/src/pages/ShowOrderDetails.tsx') diff --git a/packages/backend/src/pages/ShowOrderDetails.tsx b/packages/backend/src/pages/ShowOrderDetails.tsx index 591b7ea..8ae08ab 100644 --- a/packages/backend/src/pages/ShowOrderDetails.tsx +++ b/packages/backend/src/pages/ShowOrderDetails.tsx @@ -18,83 +18,56 @@ * * @author Sebastian Javier Marchano (sebasjm) */ -import { Fragment, render, h, VNode } from 'preact'; -import { useEffect } from 'preact/hooks'; +import { Fragment, h, render, VNode } from 'preact'; import { render as renderToString } from 'preact-render-to-string'; +import "../css/pure-min.css"; +import "../css/style.css"; -export function ShowOrderDetails(): VNode { - useEffect(() => { - const longpollDelayMs = 60000; - const checkUrl = new URL("{{& order_status_url }}"); - checkUrl.searchParams.set("timeout_ms", longpollDelayMs.toString()); - function check() { - let retried = false; - function retryOnce() { - - if (!retried) { - retried = true; - check(); - } - } - const req = new XMLHttpRequest(); - req.onreadystatechange = function () { - if (req.readyState === XMLHttpRequest.DONE) { - if (req.status === 200) { - try { - const resp = JSON.parse(req.responseText); - if (resp.fulfillment_url) { - window.location.replace(resp.fulfillment_url); - } - } catch (e) { - console.error("could not parse response:", e); - } - } - if (req.status === 202) { - try { - const resp = JSON.parse(req.responseText); - if (resp.fulfillment_url) { - window.location.replace(resp.fulfillment_url); - } - } catch (e) { - console.error("could not parse response:", e); - } - } - if (req.status === 402) { - try { - const resp = JSON.parse(req.responseText); - if (resp.already_paid_order_id && resp.fulfillment_url) { - window.location.replace(resp.fulfillment_url); - } - } catch (e) { - console.error("could not parse response:", e); - } - } - setTimeout(retryOnce, 500); - } - }; - req.onerror = function () { - setTimeout(retryOnce, 500); - } - req.ontimeout = function () { - setTimeout(retryOnce, 500); - } - req.timeout = longpollDelayMs; - req.open("GET", checkUrl.href); - req.send(); - } - setTimeout(check, 500); - }) +/** + * This page creates a payment request QR code + * + * It will build into a mustache html template for server side rendering + * + * server side rendering params: + * - order_summary + * - contract_terms + * - refund_amount + * + * request params: + * - refund_amount + * - contract_terms + * - order_summary + */ + +interface Props { + order_summary?: string; + refund_amount?: string; + contract_terms?: string; +} + +function Head({ order_summary }: { order_summary?: string }): VNode { + return + + + + Status of your order for {order_summary ? order_summary : `{{ order_summary }}`} + +} + +export function ShowOrderDetails({ order_summary, refund_amount }: Props): VNode { return

Order details

- This is the default status page for your order for {`{{ order_summary }}`}. + This is the default status page for your order for {order_summary ? order_summary : `{{ order_summary }}`}.

Refund status

- The merchant has granted you refunds on the purchase of {`{{ refund_amount }}`}. + The merchant has granted you refunds on the purchase of {refund_amount ? refund_amount : `{{ refund_amount }}`}.

Full contract details

@@ -108,15 +81,20 @@ export function ShowOrderDetails(): VNode { } -function Title(): VNode { - return Status of your order for {`{order_summary}`} -} - export function mount(): void { try { - const params = new URL(window.location.href).searchParams + const fromLocation = new URL(window.location.href).searchParams + const os = fromLocation.get('order_summary') || undefined; + if (os) { + render(, document.head); + } + + const ra = fromLocation.get('refund_amount') || undefined; + const ct = fromLocation.get('contract_terms') || undefined; + render(, document.body); } catch (e) { @@ -125,6 +103,9 @@ export function mount(): void { } } -export function buildTimeRendering(): string { - return renderToString() +export function buildTimeRendering(): { head: string, body: string } { + return { + head: renderToString(), + body: renderToString() + } } -- cgit v1.2.3