Skip to content

Commit 1fa50d9

Browse files
authored
Merge pull request wavelog#1921 from int2001/buffering_qsos
Buffering qsos when (temporary) offline
2 parents ca8ce2f + 988c011 commit 1fa50d9

File tree

1 file changed

+65
-11
lines changed

1 file changed

+65
-11
lines changed

assets/js/sections/qso.js

+65-11
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ $("#qso_input").off('submit').on('submit', function (e) {
111111
url: base_url + 'index.php/qso' + manual_addon,
112112
method: 'POST',
113113
type: 'post',
114+
timeout: 10000,
114115
data: $(this).serialize(),
115116
success: function (resdata) {
116117
result = JSON.parse(resdata);
@@ -122,14 +123,9 @@ $("#qso_input").off('submit').on('submit', function (e) {
122123
$("#noticer").addClass("alert alert-info");
123124
$("#noticer").html("QSO Added");
124125
$("#noticer").show();
125-
reset_fields();
126-
htmx.trigger("#qso-last-table", "qso_event")
127-
$("#saveQso").html(saveQsoButtonText).prop("disabled", false);
128-
$("#callsign").val("");
126+
prepare_next_qso(saveQsoButtonText);
129127
$("#noticer").fadeOut(2000);
130-
var triggerEl = document.querySelector('#myTab a[href="#qso"]')
131-
bootstrap.Tab.getInstance(triggerEl).show() // Select tab by name
132-
$("#callsign").trigger("focus");
128+
processBacklog(); // If we have success with the live-QSO, we could also process the backlog
133129
} else {
134130
$("#noticer").removeClass("");
135131
$("#noticer").addClass("alert alert-warning");
@@ -139,17 +135,69 @@ $("#qso_input").off('submit').on('submit', function (e) {
139135
}
140136
},
141137
error: function () {
138+
saveToBacklog(JSON.stringify(this.data),manual_addon);
139+
prepare_next_qso(saveQsoButtonText);
142140
$("#noticer").removeClass("");
143-
$("#noticer").addClass("alert alert-warning");
144-
$("#noticer").html("Timeout while adding QSO. NOT added");
141+
$("#noticer").addClass("alert alert-info");
142+
$("#noticer").html("QSO Added to Backlog");
145143
$("#noticer").show();
146-
$("#saveQso").html(saveQsoButtonText).prop("disabled", false);
144+
$("#noticer").fadeOut(5000);
147145
}
148146
});
149147
}
150148
return false;
151149
});
152150

151+
function prepare_next_qso(saveQsoButtonText) {
152+
reset_fields();
153+
htmx.trigger("#qso-last-table", "qso_event")
154+
$("#saveQso").html(saveQsoButtonText).prop("disabled", false);
155+
$("#callsign").val("");
156+
var triggerEl = document.querySelector('#myTab a[href="#qso"]')
157+
bootstrap.Tab.getInstance(triggerEl).show() // Select tab by name
158+
$("#callsign").trigger("focus");
159+
}
160+
161+
var processingBL=false;
162+
163+
async function processBacklog() {
164+
if (!processingBL) {
165+
processingBL=true;
166+
const Qsobacklog = JSON.parse(localStorage.getItem('qso-backlog')) || [];
167+
for (const entry of [...Qsobacklog]) {
168+
try {
169+
await $.ajax({url: base_url + 'index.php/qso' + entry.manual_addon, method: 'POST', type: 'post', data: JSON.parse(entry.data),
170+
success: function(resdata) {
171+
Qsobacklog.splice(Qsobacklog.findIndex(e => e.id === entry.id), 1);
172+
},
173+
error: function() {
174+
entry.attempts++;
175+
}});
176+
} catch (error) {
177+
entry.attempts++;
178+
}
179+
}
180+
localStorage.setItem('qso-backlog', JSON.stringify(Qsobacklog));
181+
processingBL=false;
182+
}
183+
}
184+
185+
function saveToBacklog(formData,manual_addon) {
186+
const backlog = JSON.parse(localStorage.getItem('qso-backlog')) || [];
187+
const entry = {
188+
id: Date.now(),
189+
timestamp: new Date().toISOString(),
190+
data: formData,
191+
manual_addon: manual_addon,
192+
attempts: 0
193+
};
194+
backlog.push(entry);
195+
localStorage.setItem('qso-backlog', JSON.stringify(backlog));
196+
}
197+
198+
window.addEventListener('beforeunload', processBacklog()); // process possible QSO-Backlog on unload of page
199+
window.addEventListener('pagehide', processBacklog()); // process possible QSO-Backlog on Hide of page (Mobile-Browsers)
200+
153201
$('#reset_time').on("click", function () {
154202
var now = new Date();
155203
$('#start_time').attr('value', ("0" + now.getUTCHours()).slice(-2) + ':' + ("0" + now.getUTCMinutes()).slice(-2) + ':' + ("0" + now.getUTCSeconds()).slice(-2));
@@ -1855,14 +1903,20 @@ $(document).ready(function () {
18551903
set_timers();
18561904
updateStateDropdown('#dxcc_id', '#stateInputLabel', '#location_us_county', '#stationCntyInputQso');
18571905

1858-
// Clear the localStorage for the qrg units, except the quicklogCallsign
1906+
// Clear the localStorage for the qrg units, except the quicklogCallsign and a possible backlog
18591907
let quicklogCallsign = localStorage.getItem('quicklogCallsign');
1908+
let QsoBacklog = localStorage.getItem('qso-backlog');
1909+
18601910
localStorage.clear();
18611911
if (quicklogCallsign) {
18621912
localStorage.setItem('quicklogCallsign', quicklogCallsign);
18631913
}
18641914
set_qrg();
18651915

1916+
if (QsoBacklog) {
1917+
localStorage.setItem('qso-backlog', QsoBacklog);
1918+
}
1919+
18661920
$("#locator").popover({ placement: 'top', title: 'Gridsquare Formatting', content: "Enter multiple (4-digit) grids separated with commas. For example: IO77,IO78" })
18671921
.focus(function () {
18681922
$('#locator').popover('show');

0 commit comments

Comments
 (0)