From c61213fae9afc666526d906237ac97f56b81bed2 Mon Sep 17 00:00:00 2001 From: Son NK <> Date: Fri, 24 Apr 2020 16:52:39 +0200 Subject: [PATCH] use ajax to switch on/off alias --- app/dashboard/templates/dashboard/index.html | 38 ++++++++++++++++++-- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/app/dashboard/templates/dashboard/index.html b/app/dashboard/templates/dashboard/index.html index 8c1f215b..67d16c58 100644 --- a/app/dashboard/templates/dashboard/index.html +++ b/app/dashboard/templates/dashboard/index.html @@ -124,7 +124,7 @@ style="padding-left: 0px" > - @@ -378,8 +378,40 @@ }); }); - $(".custom-switch-input").change(function (e) { - $(this).closest("form").submit(); + $(".custom-switch-input").change(async function (e) { + let aliasId = $(this).data("alias"); + let alias = $(this).parent().find(".alias").val(); + + + try { + let res = await fetch(`/api/aliases/${aliasId}/toggle`, { + method: "POST", + headers: { + "Content-Type": "application/json", + } + }); + + if (res.ok) { + let json = await res.json(); + + if (json.enabled) { + toastr.success(`${alias} is enabled`); + } else { + toastr.success(`${alias} is disabled`); + } + } else { + toastr.error("Sorry for the inconvenience! Could you refresh the page & retry please?", "Unknown Error"); + // reset to the original value + var oldValue = !$(this).prop("checked"); + $(this).prop("checked", oldValue); + } + } catch (e) { + toastr.error("Sorry for the inconvenience! Could you refresh the page & retry please?", "Unknown Error"); + // reset to the original value + var oldValue = !$(this).prop("checked"); + $(this).prop("checked", oldValue); + } + }) {% endblock %}