Add tableUrl helper and update tryKallaxSync and syncPostKallax

Ryan Danenberg committed commit0786a2e94e726d32893a63ef7f5ad00e99b668f0 Browse files
1 file changed+17−7
index.html+17−7View file
@@ -1485,7 +1485,15 @@
function unwrapRow(data) {
if (!data || typeof data !== 'object') return data;
- return data.row || data.item || data;
+ if (data.row) return data.row;
+ if (data.item) return data.item;
+ if (data.data && typeof data.data === 'object' && !Array.isArray(data.data)) return data.data;
+ return data;
+ }
+
+ function tableUrl(tableName, id) {
+ const base = `/api/p/${PROJECT_ID}/tables/${tableName}/rows`;
+ return id ? `${base}/${id}` : base;
}
function isTempId(id) {
@@ -1506,16 +1514,18 @@
async function tryKallaxSync() {
try {
const [projRes, assignRes, tsRes, daysRes] = await Promise.allSettled([
- apiFetch(`/api/p/${PROJECT_ID}/tables/projects/rows`).then(r => r.ok ? r.json() : null),
- apiFetch(`/api/p/${PROJECT_ID}/tables/assignments/rows`).then(r => r.ok ? r.json() : null),
- apiFetch(`/api/p/${PROJECT_ID}/tables/timesheets/rows`).then(r => r.ok ? r.json() : null),
- apiFetch(`/api/p/${PROJECT_ID}/tables/timesheet_days/rows`).then(r => r.ok ? r.json() : null)
+ apiFetch(tableUrl('projects')).then(r => r.ok ? r.json() : null),
+ apiFetch(tableUrl('assignments')).then(r => r.ok ? r.json() : null),
+ apiFetch(tableUrl('timesheets')).then(r => r.ok ? r.json() : null),
+ apiFetch(tableUrl('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;
+ if (Array.isArray(res.value.rows)) return res.value.rows;
+ if (Array.isArray(res.value.data)) return res.value.data;
}
return null;
};
@@ -1555,13 +1565,13 @@
try {
const payload = cleanPayload(data);
if (data._id && !isTempId(data._id)) {
- const updateRes = await apiFetch(`/api/p/${PROJECT_ID}/tables/${endpoint}/rows/${data._id}`, {
+ const updateRes = await apiFetch(tableUrl(endpoint, data._id), {
method: 'PATCH',
body: JSON.stringify(payload)
});
if (updateRes.ok) return unwrapRow(await updateRes.json());
}
- const createRes = await apiFetch(`/api/p/${PROJECT_ID}/tables/${endpoint}/rows`, {
+ const createRes = await apiFetch(tableUrl(endpoint), {
method: 'POST',
body: JSON.stringify(payload)
});