This commit is contained in:
gsinghpal
2026-03-17 13:32:08 -04:00
parent e56974d46f
commit 595dccc17d
11 changed files with 159 additions and 28 deletions

View File

@@ -39,6 +39,7 @@ export class FusionClockPortal extends Interaction {
}
if (this.locations.length > 0) {
this.selectedLocationId = this.locations[0].id;
this._restoreSelectedLocation();
}
// Start live clock
@@ -50,7 +51,15 @@ export class FusionClockPortal extends Interaction {
this._startTimer();
this._updateUIForClockIn({
location_name: this.el.dataset.locationName || "",
location_address: this.el.dataset.locationAddress || "",
});
const locId = parseInt(this.el.dataset.locationId || "0");
if (locId) {
this.selectedLocationId = locId;
this._saveSelectedLocation(locId);
}
} else if (this.locations.length > 1) {
this._detectNearestLocation();
}
this._updateDateDisplay();
@@ -121,7 +130,9 @@ export class FusionClockPortal extends Interaction {
document.querySelectorAll(".fclk-modal-item").forEach((item) => {
item.addEventListener("click", () => {
this.selectedLocationId = parseInt(item.dataset.id);
const locId = parseInt(item.dataset.id);
this.selectedLocationId = locId;
this._saveSelectedLocation(locId);
const nameEl = document.getElementById("fclk-location-name");
const addrEl = document.getElementById("fclk-location-address");
if (nameEl) nameEl.textContent = item.dataset.name;
@@ -132,6 +143,60 @@ export class FusionClockPortal extends Interaction {
});
}
// =========================================================================
// Nearest Location Detection
// =========================================================================
_detectNearestLocation() {
if (!navigator.geolocation) return;
navigator.geolocation.getCurrentPosition(
(pos) => this._selectNearestFromCoords(pos.coords.latitude, pos.coords.longitude),
async () => {
try {
const resp = await fetch("https://ipapi.co/json/");
if (resp.ok) {
const data = await resp.json();
if (data.latitude && data.longitude) {
this._selectNearestFromCoords(data.latitude, data.longitude);
}
}
} catch { /* silent */ }
},
{ enableHighAccuracy: true, timeout: 10000, maximumAge: 60000 }
);
}
_selectNearestFromCoords(lat, lng) {
let nearest = null;
let minDist = Infinity;
for (const loc of this.locations) {
if (!loc.latitude || !loc.longitude) continue;
const dist = this._haversine(lat, lng, loc.latitude, loc.longitude);
if (dist < minDist) {
minDist = dist;
nearest = loc;
}
}
if (!nearest) return;
this.selectedLocationId = nearest.id;
const nameEl = document.getElementById("fclk-location-name");
const addrEl = document.getElementById("fclk-location-address");
if (nameEl) nameEl.textContent = nearest.name;
if (addrEl) addrEl.textContent = nearest.address || "";
}
_haversine(lat1, lon1, lat2, lon2) {
const R = 6371000;
const toRad = (v) => (v * Math.PI) / 180;
const dLat = toRad(lat2 - lat1);
const dLon = toRad(lon2 - lon1);
const a =
Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(toRad(lat1)) * Math.cos(toRad(lat2)) *
Math.sin(dLon / 2) * Math.sin(dLon / 2);
return R * 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
}
// =========================================================================
// Clock Action
// =========================================================================
@@ -262,6 +327,7 @@ export class FusionClockPortal extends Interaction {
this._playSound("in");
this._showToast(result.message, "success");
this._saveState();
if (this.selectedLocationId) this._saveSelectedLocation(this.selectedLocationId);
} else if (result.action === "clock_out") {
this.isCheckedIn = false;
this._updateUIForClockOut(result);
@@ -302,6 +368,10 @@ export class FusionClockPortal extends Interaction {
const locEl = document.getElementById("fclk-location-name");
if (locEl) locEl.textContent = data.location_name;
}
if (data.location_address) {
const addrEl = document.getElementById("fclk-location-address");
if (addrEl) addrEl.textContent = data.location_address;
}
}
_updateUIForClockOut(data) {
@@ -522,6 +592,27 @@ export class FusionClockPortal extends Interaction {
} catch (e) {}
}
_saveSelectedLocation(locId) {
try {
localStorage.setItem("fclk_selected_location", String(locId));
} catch (e) {}
}
_restoreSelectedLocation() {
try {
const saved = localStorage.getItem("fclk_selected_location");
if (!saved) return;
const savedId = parseInt(saved);
const loc = this.locations.find((l) => l.id === savedId);
if (!loc) return;
this.selectedLocationId = savedId;
const nameEl = document.getElementById("fclk-location-name");
const addrEl = document.getElementById("fclk-location-address");
if (nameEl) nameEl.textContent = loc.name;
if (addrEl) addrEl.textContent = loc.address || "";
} catch (e) {}
}
// =========================================================================
// Reason Modal & Leave Request
// =========================================================================
@@ -607,7 +698,7 @@ export class FusionClockPortal extends Interaction {
if (result.is_checked_in && !this.isCheckedIn) {
this.isCheckedIn = true;
this.checkInTime = new Date(result.check_in + "Z");
this._updateUIForClockIn({ location_name: result.location_name });
this._updateUIForClockIn({ location_name: result.location_name, location_address: result.location_address || "" });
this._startTimer();
this._saveState();
} else if (result.is_checked_in && this.isCheckedIn) {