Update index.html

Ryan Danenberg committed commit7f7a60ce6c588187b39c809a488a3f183fdac97a Browse files
1 file changed+49−17
index.html+49−17View file
@@ -1402,7 +1402,7 @@
// -------------------------------------------------------------
function getAuthHeaders(customHeaders = {}) {
const headers = { ...customHeaders };
- if (authToken) {
+ if (authToken && !authToken.startsWith('demo-token-')) {
headers['Authorization'] = `Bearer ${authToken}`;
}
return headers;
@@ -1419,7 +1419,21 @@
});
}
- // Kallax API Integration (Graceful Sync with fallback)
+ function isTempId(id) {
+ if (!id || typeof id !== 'string') return true;
+ return id.startsWith('draft_') || id.startsWith('proj_') || id.startsWith('assign_') || id.startsWith('day_') || id.startsWith('temp_');
+ }
+
+ function cleanPayload(obj, omitKeys = ['_id', '_createdAt', '_updatedAt', 'ownerId']) {
+ if (!obj || typeof obj !== 'object') return obj;
+ const copy = { ...obj };
+ for (const k of omitKeys) {
+ delete copy[k];
+ }
+ return copy;
+ }
+
+ // Kallax API Integration (Bidirectional live sync with offline fallback)
async function tryKallaxSync() {
try {
const [projRes, assignRes, tsRes, daysRes] = await Promise.allSettled([
@@ -1428,45 +1442,63 @@
apiFetch('/api/timesheets').then(r => r.ok ? r.json() : null),
apiFetch('/api/timesheet_days').then(r => r.ok ? r.json() : null)
]);
+
+ const getItems = (res) => {
+ if (res.status === 'fulfilled' && res.value) {
+ if (Array.isArray(res.value)) return res.value;
+ if (Array.isArray(res.value.items)) return res.value.items;
+ }
+ return null;
+ };
+
let changed = false;
- if (projRes.status === 'fulfilled' && projRes.value?.items?.length) {
- state.projects = projRes.value.items;
+ const pItems = getItems(projRes);
+ if (pItems !== null) {
+ state.projects = pItems;
changed = true;
}
- if (assignRes.status === 'fulfilled' && assignRes.value?.items?.length) {
- state.assignments = assignRes.value.items;
+ const aItems = getItems(assignRes);
+ if (aItems !== null) {
+ state.assignments = aItems;
changed = true;
}
- if (tsRes.status === 'fulfilled' && tsRes.value?.items?.length) {
- state.timesheets = tsRes.value.items;
+ const tItems = getItems(tsRes);
+ if (tItems !== null) {
+ state.timesheets = tItems;
changed = true;
}
- if (daysRes.status === 'fulfilled' && daysRes.value?.items?.length) {
- state.timesheet_days = daysRes.value.items;
+ const dItems = getItems(daysRes);
+ if (dItems !== null) {
+ state.timesheet_days = dItems;
changed = true;
}
- if (changed) saveStore();
+
+ if (changed) {
+ saveStore();
+ renderAll();
+ }
} catch (err) {
- console.log("Operating in offline/local storage mode:", err);
+ console.warn("Operating in offline/local storage mode:", err);
}
}
async function syncPostKallax(endpoint, data) {
try {
- if (data._id && !data._id.startsWith('draft_')) {
+ const payload = cleanPayload(data);
+ if (data._id && !isTempId(data._id)) {
const updateRes = await apiFetch(`/api/${endpoint}/${data._id}`, {
- method: 'PUT',
- body: JSON.stringify(data)
+ method: 'PATCH',
+ body: JSON.stringify(payload)
});
if (updateRes.ok) return await updateRes.json();
}
const createRes = await apiFetch(`/api/${endpoint}`, {
method: 'POST',
- body: JSON.stringify(data)
+ body: JSON.stringify(payload)
});
if (createRes.ok) {
const resData = await createRes.json();
- if (resData._id && data._id && data._id.startsWith('draft_')) {
+ if (resData._id && data._id && isTempId(data._id)) {
data._id = resData._id;
}
return resData;