Show dialog box in config endpoint before saving the configuration

This commit is contained in:
Iam54r1n4
2025-02-07 05:29:42 +00:00
parent 71654b16f3
commit 1b05461ff4

View File

@ -17,28 +17,59 @@
{% block javascripts%} {% block javascripts%}
<script src="https://cdn.jsdelivr.net/npm/jsoneditor@9.1.0/dist/jsoneditor.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/jsoneditor@9.1.0/dist/jsoneditor.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jsoneditor@9.1.0/dist/jsoneditor.min.css" /> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jsoneditor@9.1.0/dist/jsoneditor.min.css" />
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script> <script>
const container = document.getElementById("jsoneditor"); const container = document.getElementById("jsoneditor");
const editor = new JSONEditor(container, { const editor = new JSONEditor(container, {
mode: "tree", mode: "code",
modes: ["code", "tree"] modes: ["code", "tree"]
}); });
// Function to load JSON
function restoreJson() { function restoreJson() {
fetch("/api/json-file") fetch("{{ url_for('get_file') }}")
.then(response => response.json()) .then(response => response.json())
.then(json => editor.set(json)) .then(json => editor.set(json))
.catch(error => console.error("Error loading JSON:", error)); .catch(error => console.error("Error loading JSON:", error));
} }
// Function to ask the user before saving and save the JSON if confirmed
function saveJson() { function saveJson() {
fetch("/api/json-file", { Swal.fire({
title: 'Are you sure?',
text: 'Do you want to save the changes?',
icon: 'warning',
showCancelButton: true,
confirmButtonText: 'Yes, save it!',
cancelButtonText: 'Cancel',
reverseButtons: true
}).then((result) => {
if (result.isConfirmed) {
// If user confirms, save the JSON data
fetch("{{ url_for('set_file') }}", {
method: "POST", method: "POST",
headers: { "Content-Type": "application/json" }, headers: { "Content-Type": "application/json" },
body: JSON.stringify(editor.get()) body: JSON.stringify(editor.get())
}).then(() => alert("Saved successfully!")) }).then(() => {
.catch(error => console.error("Error saving JSON:", error)); Swal.fire(
'Saved!',
'Your changes have been saved.',
'success'
);
}).catch(error => {
Swal.fire(
'Error!',
'There was an error saving your data.',
'error'
);
console.error("Error saving JSON:", error);
});
} }
});
}
// Load JSON data when the page loads
restoreJson();
</script> </script>
{% endblock %} {% endblock %}