From 6efb9b41b4eecbfe7d9e0b63b481004c6b65e38a Mon Sep 17 00:00:00 2001 From: justcool393 Date: Fri, 14 Oct 2022 05:43:23 -0700 Subject: [PATCH] use createXhrWithFormKey in the lottery --- files/assets/js/comments_v.js | 2 -- files/assets/js/core.js | 6 ++---- files/assets/js/lottery.js | 12 ++++-------- 3 files changed, 6 insertions(+), 14 deletions(-) diff --git a/files/assets/js/comments_v.js b/files/assets/js/comments_v.js index 7512730aa..a830cdb79 100644 --- a/files/assets/js/comments_v.js +++ b/files/assets/js/comments_v.js @@ -297,8 +297,6 @@ document.onpaste = function(event) { } function handle_action(type, cid, thing) { - - const btns = document.getElementsByClassName(`action-${cid}`) for (const btn of btns) { diff --git a/files/assets/js/core.js b/files/assets/js/core.js index de0ccd506..c8b828c5c 100644 --- a/files/assets/js/core.js +++ b/files/assets/js/core.js @@ -74,13 +74,11 @@ function postPostToastNonShopActions(t, url, button1, button2, className) { } } -function createXhrWithFormKey(url, method="POST", form=null) { +function createXhrWithFormKey(url, method="POST", form=new FormData()) { const xhr = new XMLHttpRequest(); xhr.open(method, url); xhr.setRequestHeader('xhr', 'xhr'); - if (!form) { - form = new FormData(); - } + if (!form) form = new FormData(); form.append("formkey", formkey()); return [xhr, form]; // hacky but less stupid than what we were doing before } diff --git a/files/assets/js/lottery.js b/files/assets/js/lottery.js index b36c6266a..b758b3c6b 100644 --- a/files/assets/js/lottery.js +++ b/files/assets/js/lottery.js @@ -77,16 +77,12 @@ function endLotterySession() { // Composed function handleLotteryRequest(uri, method, callback = () => {}) { - const xhr = new XMLHttpRequest(); - const url = `/lottery/${uri}`; - xhr.open(method, url); - xhr.onload = handleLotteryResponse.bind(null, xhr, method, callback); - const form = new FormData(); form.append("formkey", formkey()); - form.append("quantity", purchaseQuantity) - - xhr.send(form); + form.append("quantity", purchaseQuantity); + const xhr = createXhrWithFormKey(`/lottery/${uri}`, method, form); + xhr[0].onload = handleLotteryResponse.bind(null, xhr[0], method, callback); + xhr[0].send(xhr[1]); } function handleLotteryResponse(xhr, method, callback) {