From f22ef3a7e58f4264a3ec660f053c3c531fb75c0d Mon Sep 17 00:00:00 2001 From: justcool393 Date: Fri, 14 Oct 2022 04:31:02 -0700 Subject: [PATCH] refactor post_toast_callback a bit and create createXhrWithFormKey function --- files/assets/js/award_modal.js | 15 ++----- files/assets/js/core.js | 77 +++++++++++----------------------- 2 files changed, 28 insertions(+), 64 deletions(-) diff --git a/files/assets/js/award_modal.js b/files/assets/js/award_modal.js index 0b130fa96..d02008bc1 100644 --- a/files/assets/js/award_modal.js +++ b/files/assets/js/award_modal.js @@ -86,12 +86,8 @@ function vote(type, id, dir) { } } } - - const xhr = new XMLHttpRequest(); - xhr.open("POST", "/vote/" + type.replace('-mobile','') + "/" + id + "/" + votedirection); - xhr.setRequestHeader('xhr', 'xhr'); - const form = new FormData() - form.append("formkey", formkey()); + + const xhr = createXhrWithFormKey("/vote/" + type.replace('-mobile','') + "/" + id + "/" + votedirection); xhr.send(form); } @@ -127,14 +123,9 @@ function pick(kind, canbuy1, canbuy2) { function buy(mb) { const kind = document.getElementById('kind').value; - const xhr = new XMLHttpRequest(); url = `/buy/${kind}` if (mb) url += "?mb=true" - xhr.open("POST", url); - xhr.setRequestHeader('xhr', 'xhr'); - const form = new FormData() - form.append("formkey", formkey()); - + const xhr = createXhrWithFormKey(url); if(typeof data === 'object' && data !== null) { for(let k of Object.keys(data)) { form.append(k, data[k]); diff --git a/files/assets/js/core.js b/files/assets/js/core.js index c7264f432..51d3a8379 100644 --- a/files/assets/js/core.js +++ b/files/assets/js/core.js @@ -1,5 +1,3 @@ -/*const bootstrap = require("./bootstrap");*/ - function isShopConfirmation(t) { return t.id.startsWith('buy1-') || t.id.startsWith('buy2-'); } @@ -76,14 +74,20 @@ function postPostToastNonShopActions(t, url, button1, button2, className) { } } -function postToast(t, url, button1, button2, className, extraActions, extraActionsError) { - prePostToastNonShopActions(t, url, button1, button2, className) - const xhr = new XMLHttpRequest(); - xhr.open("POST", url); - xhr.setRequestHeader('xhr', 'xhr'); - const form = new FormData() - form.append("formkey", formkey()); +function createXhrWithFormKey(url, method="POST", form=null) { + const xhr = new XMLHttpRequest(); + xhr.open(method, url); + xhr.setRequestHeader('xhr', 'xhr'); + if (!form) { + form = new FormData(); + } + form.append("formkey", formkey()); + return xhr; +} +function postToast(t, url, button1, button2, className, extraActions, extraActionsError) { + prePostToastNonShopActions(t, url, button1, button2, className); + const xhr = createXhrWithFormKey(url); xhr.onload = function() { postToastLoad(xhr, className, extraActions, extraActionsError) postPostToastNonShopActions(t, url, button1, button2, className) @@ -97,57 +101,26 @@ function post_toast(t, url, button1, button2, classname, extra_actions, extra_ac } function post_toast_callback(url, data, callback) { - const xhr = new XMLHttpRequest(); - xhr.open("POST", url); - xhr.setRequestHeader('xhr', 'xhr'); - const form = new FormData() - form.append("formkey", formkey()); - + const xhr = createXhrWithFormKey(url); if(typeof data === 'object' && data !== null) { for(let k of Object.keys(data)) { form.append(k, data[k]); } } - - form.append("formkey", formkey()); xhr.onload = function() { let result if (callback) result = callback(xhr); - if (xhr.status >= 200 && xhr.status < 300) { - var myToast = bootstrap.Toast.getOrCreateInstance(document.getElementById('toast-post-error')); - myToast.hide(); - - var myToast = bootstrap.Toast.getOrCreateInstance(document.getElementById('toast-post-success')); - myToast.show(); - - try { - if(typeof result == "string") { - document.getElementById('toast-post-success-text').innerText = result; - } else { - document.getElementById('toast-post-success-text').innerText = JSON.parse(xhr.response)["message"]; - } - } catch(e) { - } - - return true; - } else { - var myToast = bootstrap.Toast.getOrCreateInstance(document.getElementById('toast-post-success')); - myToast.hide(); - - var myToast = bootstrap.Toast.getOrCreateInstance(document.getElementById('toast-post-error')); - myToast.show(); - - try { - if(typeof result == "string") { - document.getElementById('toast-post-error-text').innerText = result; - } else { - document.getElementById('toast-post-error-text').innerText = JSON.parse(xhr.response)["error"]; - } - return false - } catch(e) {console.log(e)} - - return false; - } + let message; + let success = xhr.status >= 200 && xhr.status < 300; + if (typeof result == "string") { + message = result; + } else { + message = getMessageFromJsonData(success, JSON.parse(xhr.response)); + } + let oldToast = bootstrap.Toast.getOrCreateInstance(document.getElementById('toast-post-' + (success ? 'error': 'success'))); // intentionally reversed here: this is the old toast + oldToast.hide(); + showToast(success, message); + return success; }; xhr.send(form); }