the settings code has been rewritten

This commit is contained in:
Данил 2022-10-02 12:19:20 +03:00
parent 3f51438c24
commit f5c1909f81

View file

@ -1,137 +1,142 @@
$.ajax({ $(document).ready(async function() {
url: "http://localhost:8080/api/get/settings", const data = await getData();
type: "GET",
success: function(data){ const $startupSwitch = $("#settings_startupSwitch");
if(data["autochangewallpaper"] === 1) { const $startupSwitchTogglerName = $("#settings_startupTogglerName");
$("#autosetWallpaperSwitch").attr("checked", "true");
$("#autosetWallpaperText").text("On"); const $wallpaperSwitch = $("#settings_autoSetWallpaperSwitch");
} const $wallpaperSwitchTogglerName = $("#settings_autoSetWallpaperTogglerName");
if(data["startup"] === 1) {
$("#startupSwitch").attr("checked", "true"); console.log(data)
$("#startupText").text("On");
} if(data["wallpaper"] === 1) {
$wallpaperSwitch.attr("checked", "true");
$wallpaperSwitchTogglerName.text("On");
}
if(data["startup"] === 1) {
$startupSwitch.attr("checked", "true");
$startupSwitchTogglerName.text("On");
} }
});
$("#autosetWallpaperSwitch").click(function(){ $wallpaperSwitch.click(function() {
$.ajax({ if(data["wallpaper"] === 1) {
url: "http://localhost:8080/api/get/settings", $.ajax({
type: "GET", url: "http://localhost:8080/api/update/settings",
success: function(data){ type: "POST",
if(data["autochangewallpaper"] === 1){ data: {
$.ajax({ "wallpaper": 0
url: "http://localhost:8080/api/update/settings", },
type: "POST", success: function(data) {
data: { if(data["status"]) {
autochangewallpaper: 0 $wallpaperSwitchTogglerName.text("Off");
}, $wallpaperSwitch.removeAttr("checked");
success: function(data){
if(data.status) {
$("#autosetWallpaperSwitch").removeAttr("checked");
$("#autosetWallpaperText").text("Off");
$(".toast-body").text(data.message); toast(data.message);
let toastLiveExample = document.getElementById('liveToast'); } else {
let toast = new bootstrap.Toast(toastLiveExample); toast("Failed to apply settings");
toast.show();
}
} }
}); }
} else { });
$.ajax({ } else {
url: "http://localhost:8080/api/update/settings", $.ajax({
type: "POST", url: "http://localhost:8080/api/update/settings",
data: { type: "POST",
autochangewallpaper: 1 data: {
}, "wallpaper": 1
success: function(data){ },
if(data.status) { success: function(data) {
$("#autosetWallpaperSwitch").attr("checked", "true"); if(data["status"]) {
$("#autosetWallpaperText").text("On"); $wallpaperSwitchTogglerName.text("On");
$wallpaperSwitch.attr("checked", "true");
$(".toast-body").text(data.message); toast(data.message);
let toastLiveExample = document.getElementById('liveToast'); } else {
let toast = new bootstrap.Toast(toastLiveExample); toast("Failed to apply settings");
toast.show();
} else {
$(".toast-body").text("Could not remove the program from autorun.");
let toastLiveExample = document.getElementById('liveToast');
let toast = new bootstrap.Toast(toastLiveExample);
toast.show();
}
} }
}); }
} });
} }
}); });
$startupSwitch.click(function() {
if(data["startup"] === 1) {
$.ajax({
url: "http://localhost:8080/api/update/settings",
type: "POST",
data: {
"startup": 0
},
success: async function(data) {
if(data["status"]) {
await editStartup(0);
$startupSwitchTogglerName.text("Off");
$startupSwitch.removeAttr("checked");
toast(data.message);
} else {
toast("Failed to apply settings");
}
}
});
} else {
$.ajax({
url: "http://localhost:8080/api/update/settings",
type: "POST",
data: {
"startup": 1
},
success: async function(data) {
if(data["status"]) {
await editStartup(1);
$startupSwitchTogglerName.text("On");
$startupSwitch.attr("checked", "true");
toast(data.message);
} else {
toast("Failed to apply settings");
}
}
});
}
});
$("#createLabelButton").click(function() {
$.ajax({
url: "http://localhost:8080/api/create/label",
type: "POST",
success: function (data) {
if (data["status"]) {
toast(data.message);
} else {
toast("Failed to create label");
}
}
});
});
}); });
$("#startupSwitch").click(function() {
$.ajax({
url: "http://localhost:8080/api/get/settings",
type: "GET",
success: function (data) {
if (data["startup"] === 1) {
$.ajax({
url: "http://localhost:8080/api/update/settings",
type: "POST",
data: {
startup: 0
},
success: function () {
$.ajax({
url: "http://localhost:8080/api/update/del/startup",
type: "POST",
success: function(data){
if(data.status) {
$("#startupSwitch").removeAttr("checked");
$("#startupText").text("Off");
$(".toast-body").text(data.message); function getData() {
let toastLiveExample = document.getElementById('liveToast'); return fetch("http://localhost:8080/api/get/settings").then(response => response.json()).then(data => {
let toast = new bootstrap.Toast(toastLiveExample); return data;
toast.show();
} else {
$(".toast-body").text("Failed to apply settings.");
let toastLiveExample = document.getElementById('liveToast');
let toast = new bootstrap.Toast(toastLiveExample);
toast.show();
}
}
});
}
});
} else {
$.ajax({
url: "http://localhost:8080/api/update/settings",
type: "POST",
data: {
startup: 1
},
success: function () {
$.ajax({
url: "http://localhost:8080/api/update/set/startup",
type: "POST",
success: function(data){
if(data.status) {
$("#startupSwitch").attr("checked", "true");
$("#startupText").text("On");
$(".toast-body").text(data.message);
let toastLiveExample = document.getElementById('liveToast');
let toast = new bootstrap.Toast(toastLiveExample);
toast.show();
} else {
$(".toast-body").text("Failed to apply settings.");
let toastLiveExample = document.getElementById('liveToast');
let toast = new bootstrap.Toast(toastLiveExample);
toast.show();
}
}
});
}
});
}
}
}); });
}); }
function toast(message) {
$(".toast-body").text(message);
let toastLiveExample = document.getElementById('liveToast');
let toast = new bootstrap.Toast(toastLiveExample);
toast.show();
}
function editStartup(i) {
return $.ajax({
url: "http://localhost:8080/api/update/startup",
type: "POST",
data: {
"startup": i
},
});
}