From 747209a8caee855196d1167a0ff2088115282ea8 Mon Sep 17 00:00:00 2001 From: aryanshridhar Date: Wed, 14 Apr 2021 19:31:21 +0530 Subject: [PATCH] realm_emoji: Shift realm_emoji request call in a function. Relocated the post request in a new `add_realm_emoji` function to built an better abstraction for `settings_emoji`. --- static/js/settings_emoji.js | 63 ++++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 29 deletions(-) diff --git a/static/js/settings_emoji.js b/static/js/settings_emoji.js index 8d8f5e8af8..da88b8848f 100644 --- a/static/js/settings_emoji.js +++ b/static/js/settings_emoji.js @@ -184,7 +184,39 @@ export function set_up() { e.stopPropagation(); const emoji_status = $("#admin-emoji-status"); const emoji = {}; - const formData = new FormData(); + + function submit_custom_emoji_request() { + $("#admin_emoji_submit").prop("disabled", true); + const formData = new FormData(); + for (const [i, file] of Array.prototype.entries.call($("#emoji_file_input")[0].files)) { + formData.append("file-" + i, file); + } + + channel.post({ + url: "/json/realm/emoji/" + encodeURIComponent(emoji.name), + data: formData, + cache: false, + processData: false, + contentType: false, + success() { + $("#admin-emoji-status").hide(); + ui_report.success( + $t_html({defaultMessage: "Custom emoji added!"}), + emoji_status, + ); + $("form.admin-emoji-form input[type='text']").val(""); + $("#admin_emoji_submit").prop("disabled", false); + emoji_widget.clear(); + }, + error(xhr) { + $("#admin-emoji-status").hide(); + const errors = JSON.parse(xhr.responseText).msg; + xhr.responseText = JSON.stringify({msg: errors}); + ui_report.error($t_html({defaultMessage: "Failed"}), xhr, emoji_status); + $("#admin_emoji_submit").prop("disabled", false); + }, + }); + } for (const obj of $(this).serializeArray()) { emoji[obj.name] = obj.value; @@ -197,34 +229,7 @@ export function set_up() { ); return; } - $("#admin_emoji_submit").prop("disabled", true); - for (const [i, file] of Array.prototype.entries.call($("#emoji_file_input")[0].files)) { - formData.append("file-" + i, file); - } - channel.post({ - url: "/json/realm/emoji/" + encodeURIComponent(emoji.name), - data: formData, - cache: false, - processData: false, - contentType: false, - success() { - $("#admin-emoji-status").hide(); - ui_report.success( - $t_html({defaultMessage: "Custom emoji added!"}), - emoji_status, - ); - $("form.admin-emoji-form input[type='text']").val(""); - $("#admin_emoji_submit").prop("disabled", false); - emoji_widget.clear(); - }, - error(xhr) { - $("#admin-emoji-status").hide(); - const errors = JSON.parse(xhr.responseText).msg; - xhr.responseText = JSON.stringify({msg: errors}); - ui_report.error($t_html({defaultMessage: "Failed"}), xhr, emoji_status); - $("#admin_emoji_submit").prop("disabled", false); - }, - }); + submit_custom_emoji_request(); }); }