Add check config changes during WebUI

This commit is contained in:
Alexey Khit
2023-09-04 12:05:17 +03:00
parent 05360ac284
commit 064ffef462
+15 -12
View File
@@ -29,39 +29,42 @@
<br> <br>
<div id="config"></div> <div id="config"></div>
<script> <script>
let dump;
ace.config.set('basePath', 'https://cdnjs.cloudflare.com/ajax/libs/ace/1.24.1/'); ace.config.set('basePath', 'https://cdnjs.cloudflare.com/ajax/libs/ace/1.24.1/');
const editor = ace.edit('config', { const editor = ace.edit('config', {
mode: 'ace/mode/yaml', mode: 'ace/mode/yaml',
}); });
document.getElementById('save').addEventListener('click', () => { document.getElementById('save').addEventListener('click', async () => {
fetch('api/config', { let r = await fetch('api/config', {cache: 'no-cache'});
method: 'POST', body: editor.getValue() if (r.ok && dump !== await r.text()) {
}).then(r => { alert('Config was changed from another place. Refresh the page and make changes again');
return;
}
r = await fetch('api/config', {method: 'POST', body: editor.getValue()});
if (r.ok) { if (r.ok) {
alert('OK'); alert('OK');
fetch('api/exit?code=100', {method: 'POST'}); fetch('api/exit?code=100', {method: 'POST'});
} else { } else {
r.text().then(alert); alert(await r.text());
} }
}); });
});
window.addEventListener('load', () => { window.addEventListener('load', async () => {
fetch('api/config', {cache: 'no-cache'}).then(r => { const r = await fetch('api/config', {cache: 'no-cache'});
if (r.status === 410) { if (r.status === 410) {
alert('Config file is not set'); alert('Config file is not set');
} else if (r.status === 404) { } else if (r.status === 404) {
editor.setValue(''); // config file not exist editor.setValue(''); // config file not exist
} else if (r.ok) { } else if (r.ok) {
r.text().then(data => { dump = await r.text();
editor.setValue(data); editor.setValue(dump);
});
} else { } else {
alert(`Unknown error: ${r.statusText} (${r.status})`); alert(`Unknown error: ${r.statusText} (${r.status})`);
} }
}); });
});
</script> </script>
</body> </body>
</html> </html>