Update saveActiveTimesheet to use tableUrl and unwrapRow

Ryan Danenberg committed commit476fc866caabc0916423dfa20793a6c7ed525ff7 Browse files
1 file changed+29−25
index.html+29−25View file
@@ -3191,40 +3191,42 @@
const tsPayload = cleanPayload(activeEditorTimesheet);
if (!isTempId(serverTsId)) {
- // PATCH /api/timesheets/:id
- const tsRes = await apiFetch(`/api/timesheets/${serverTsId}`, {
+ // PATCH /api/p/${PROJECT_ID}/tables/timesheets/rows/:id
+ const tsRes = await apiFetch(tableUrl('timesheets', serverTsId), {
method: 'PATCH',
body: JSON.stringify(tsPayload)
});
if (tsRes.ok) {
- const updatedTs = await tsRes.json();
+ const updatedTs = unwrapRow(await tsRes.json());
const idx = state.timesheets.findIndex(t => t._id === serverTsId);
- if (idx >= 0) Object.assign(state.timesheets[idx], updatedTs);
+ if (idx >= 0 && updatedTs) Object.assign(state.timesheets[idx], updatedTs);
}
} else {
- // POST /api/timesheets
- const tsRes = await apiFetch('/api/timesheets', {
+ // POST /api/p/${PROJECT_ID}/tables/timesheets/rows
+ const tsRes = await apiFetch(tableUrl('timesheets'), {
method: 'POST',
body: JSON.stringify(tsPayload)
});
if (tsRes.ok) {
- const createdTs = await tsRes.json();
- const oldTempId = activeEditorTimesheet._id;
- serverTsId = createdTs._id;
- activeEditorTimesheet._id = serverTsId;
-
- const idx = state.timesheets.findIndex(t => t._id === oldTempId);
- if (idx >= 0) state.timesheets[idx] = createdTs;
- state.timesheet_days.forEach(d => {
- if (d.timesheetId === oldTempId) d.timesheetId = serverTsId;
- });
- activeWorkedDays.forEach(d => {
- d.timesheetId = serverTsId;
- });
+ const createdTs = unwrapRow(await tsRes.json());
+ if (createdTs && createdTs._id) {
+ const oldTempId = activeEditorTimesheet._id;
+ serverTsId = createdTs._id;
+ activeEditorTimesheet._id = serverTsId;
+
+ const idx = state.timesheets.findIndex(t => t._id === oldTempId);
+ if (idx >= 0) state.timesheets[idx] = createdTs;
+ state.timesheet_days.forEach(d => {
+ if (d.timesheetId === oldTempId) d.timesheetId = serverTsId;
+ });
+ activeWorkedDays.forEach(d => {
+ d.timesheetId = serverTsId;
+ });
+ }
}
}
- // Batch save or update days to /api/timesheet_days
+ // Batch save or update days to /api/p/${PROJECT_ID}/tables/timesheet_days/rows
const dayPromises = [];
for (const d of activeWorkedDays) {
@@ -3239,10 +3241,11 @@
if (!isTempId(d._id)) {
dayPromises.push(
- apiFetch(`/api/timesheet_days/${d._id}`, {
+ apiFetch(tableUrl('timesheet_days', d._id), {
method: 'PATCH',
body: JSON.stringify(cleanPayload(dayPayload))
- }).then(r => r.ok ? r.json() : null).then(saved => {
+ }).then(r => r.ok ? r.json() : null).then(raw => {
+ const saved = unwrapRow(raw);
if (saved && saved._id) {
const idx = state.timesheet_days.findIndex(x => x._id === d._id);
if (idx >= 0) state.timesheet_days[idx] = saved;
@@ -3251,10 +3254,11 @@
);
} else {
dayPromises.push(
- apiFetch('/api/timesheet_days', {
+ apiFetch(tableUrl('timesheet_days'), {
method: 'POST',
body: JSON.stringify(dayPayload)
- }).then(r => r.ok ? r.json() : null).then(saved => {
+ }).then(r => r.ok ? r.json() : null).then(raw => {
+ const saved = unwrapRow(raw);
if (saved && saved._id) {
const tempId = d._id;
d._id = saved._id;
@@ -3269,7 +3273,7 @@
for (const od of oldDays) {
if (!isTempId(od._id) && !activeWorkedDays.some(ad => ad._id === od._id || ad.date === od.date)) {
dayPromises.push(
- apiFetch(`/api/timesheet_days/${od._id}`, {
+ apiFetch(tableUrl('timesheet_days', od._id), {
method: 'DELETE'
}).catch(() => null)
);