From e2be038af7f40ddcda8b14ca21c5ba493dc416a5 Mon Sep 17 00:00:00 2001 From: Jon Staab Date: Thu, 8 Feb 2024 10:07:00 -0800 Subject: [PATCH] Bring back sendPostRequest --- templates/authorizeRequest.handlebar | 47 +++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/templates/authorizeRequest.handlebar b/templates/authorizeRequest.handlebar index dcff0ea..b01f3f7 100644 --- a/templates/authorizeRequest.handlebar +++ b/templates/authorizeRequest.handlebar @@ -31,6 +31,51 @@ } } } + + function sendPostRequest(permissions) { + const url = '/requests/{{record.id}}'; + const password = document.getElementById('password').value; + let callbackUrl; + {{#if callbackUrl}} + callbackUrl = '{{callbackUrl}}'; + {{/if}} + const data = { + permissions, + password, + }; + + fetch(url, { + method: 'POST', // or 'PUT' + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(data), + }) + .then((response) => response.json()) + .then((data) => { + console.log('Success:', data); + + if (data.error) { + document.getElementById('error').innerText = data.error; + document.getElementById('error').classList.remove('hidden'); + return; + } + + // hide main content and show close message + document.getElementById('main').classList.add('hidden'); + document.getElementById('closeit').classList.remove('hidden'); + + // redirect to callback url + if (callbackUrl) { + const url = new URL(callbackUrl); + url.searchParams.append('pubkey', data.pubkey); + window.location.href = url.toString(); + } + }) + .catch((error) => { + console.error('Error:', error); + }); + }