Skip to content

Commit 6ac82fd

Browse files
committed
try to load locations instant
1 parent 1eee747 commit 6ac82fd

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

renderer.js

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@ $(document).ready(function() {
6969
console.log(x);
7070
});
7171

72-
input_key.addEventListener('input', () => {
72+
input_key.addEventListener('change', () => {
7373
getStations();
7474
});
75-
input_url.addEventListener('input', () => {
75+
input_url.addEventListener('change', () => {
7676
getStations();
7777
});
7878
if (cfg.wavelog_key != "" && cfg.wavelog_url != "") {
@@ -222,15 +222,30 @@ function updateUtcTime() {
222222
}
223223

224224
async function getStations() {
225-
let x = await fetch(cfg.wavelog_url + '/api/station_info/' + cfg.wavelog_key, {
226-
method: 'GET',
227-
rejectUnauthorized: false,
228-
headers: {
229-
Accept: 'application.json',
230-
'Content-Type': 'application/json',
231-
},
232-
});
233-
fillDropdown(await x.json());
225+
let select = $('#wavelog_id');
226+
select.empty();
227+
select.prop('disabled', true);
228+
try {
229+
let x = await fetch($('#wavelog_url').val().trim() + '/api/station_info/' + $('#wavelog_key').val().trim(), {
230+
method: 'GET',
231+
rejectUnauthorized: false,
232+
headers: {
233+
Accept: 'application/json',
234+
'Content-Type': 'application/json',
235+
},
236+
});
237+
238+
if (!x.ok) {
239+
throw new Error(`HTTP error! Status: ${x.status}`);
240+
}
241+
242+
let data = await x.json();
243+
fillDropdown(data);
244+
245+
} catch (error) {
246+
select.append(new Option('Failed to load stations', '0'));
247+
console.error('Could not load station locations:', error.message);
248+
}
234249
}
235250

236251
function fillDropdown(data) {
@@ -244,5 +259,9 @@ function fillDropdown(data) {
244259
select.append(new Option(optionText, optionValue));
245260
});
246261

247-
select.val(cfg.wavelog_id);
262+
if (cfg.wavelog_id && data.some(station => station.station_id == cfg.wavelog_id)) {
263+
select.val(cfg.wavelog_id);
264+
} else {
265+
select.val(data.length > 0 ? data[0].station_id : null);
266+
}
248267
}

0 commit comments

Comments
 (0)