Skip to content

Commit c38410c

Browse files
committed
Enhance file upload functionality: Implemented local handling of file uploads in the new record flow to prevent premature API calls. Added support for displaying file upload counts in modular lists and ensured proper upload handling after record creation. Updated related components for improved user experience and error handling.
1 parent d0def78 commit c38410c

6 files changed

Lines changed: 284 additions & 93 deletions

File tree

dt-assets/build/components/index.es.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { E as e, C as s, x as D, B as o, D as l, a as i, b as n, d as r, e as c, f as p, u, w as C, A as M, g as T, h as m, i as x, j as S, y as d, k as g, t as B, r as h, l as v, m as A, n as b, o as F, p as L, z as f, q as y, c as U, v as j } from "./index-C6N3z3OQ.js";
1+
import { E as e, C as s, x as D, B as o, D as l, a as i, b as n, d as r, e as c, f as p, u, w as C, A as M, g as T, h as m, i as x, j as S, y as d, k as g, t as B, r as h, l as v, m as A, n as b, o as F, p as L, z as f, q as y, c as U, v as j } from "./index-v5iDo3ut.js";
22
export {
33
e as ApiService,
44
s as ComponentService,

dt-assets/build/components/index.js

Lines changed: 83 additions & 45 deletions
Large diffs are not rendered by default.

dt-assets/build/components/index.umd.cjs

Lines changed: 83 additions & 45 deletions
Large diffs are not rendered by default.

dt-assets/js/modular-list.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,6 +1321,17 @@
13211321
}
13221322
} else if (field_settings.type === 'image') {
13231323
values = [`<img src='${field_value.thumb}' class='list-image'>`];
1324+
} else if (field_settings.type === 'file_upload') {
1325+
const fileCount = Array.isArray(field_value)
1326+
? field_value.length
1327+
: Array.isArray(field_value?.values)
1328+
? field_value.values.length
1329+
: 0;
1330+
if (fileCount > 0) {
1331+
values = [
1332+
`${window.SHAREDFUNCTIONS.escapeHTML(String(fileCount))} ${fileCount === 1 ? 'file' : 'files'}`,
1333+
];
1334+
}
13241335
}
13251336
} else if (
13261337
!field_value &&

dt-assets/js/new-record.js

Lines changed: 102 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,20 @@ jQuery(function ($) {
1616
window.post_type_fields =
1717
window.new_record_localized.post_type_settings.fields;
1818

19+
// New record flow: keep file uploads local until record is created.
20+
// This prevents API upload attempts against non-existent post IDs.
21+
const fileUploadComponents = Array.from(
22+
document.querySelectorAll('dt-file-upload'),
23+
);
24+
fileUploadComponents.forEach((component) => {
25+
// Keep configured auto/manual UI behavior, but clear API bindings so
26+
// uploads are deferred until after the record is created.
27+
component.removeAttribute('post-type');
28+
component.removeAttribute('post-id');
29+
component.removeAttribute('meta-key');
30+
component.removeAttribute('key-prefix');
31+
});
32+
1933
// focus first field in the form
2034
document.querySelector('.form-fields [name]').focus();
2135

@@ -199,6 +213,7 @@ jQuery(function ($) {
199213

200214
// build form values
201215
const form = event.target;
216+
const pendingFileUploads = [];
202217
Array.from(form.querySelectorAll('*')).forEach((el) => {
203218
// skip fields like `field_name[query]` that are from typeaheads
204219
// and skip values not from web components
@@ -210,6 +225,22 @@ jQuery(function ($) {
210225
return;
211226
}
212227

228+
// file_upload fields on new record are uploaded only after create succeeds
229+
if (el.tagName === 'DT-FILE-UPLOAD') {
230+
const files =
231+
typeof el.getPendingFilesForUpload === 'function'
232+
? Array.from(el.getPendingFilesForUpload())
233+
: Array.from(el.stagedFiles || []);
234+
if (files.length > 0) {
235+
pendingFileUploads.push({
236+
fieldKey: el.name.trim(),
237+
files,
238+
component: el,
239+
});
240+
}
241+
return;
242+
}
243+
213244
if (el.value) {
214245
new_post[el.name.trim()] =
215246
window.DtWebComponents.ComponentService.convertValue(
@@ -254,11 +285,80 @@ jQuery(function ($) {
254285

255286
window.componentService.api
256287
.createPost(window.new_record_localized.post_type, new_post)
257-
.then((response) => {
288+
.then(async (response) => {
289+
const createdPostId = parseInt(
290+
response?.ID || response?.id || response?.post_id,
291+
10,
292+
);
293+
294+
console.groupCollapsed('[new-record] upload debug');
295+
console.log('post_type', window.new_record_localized.post_type);
296+
console.log('createPost response', response);
297+
console.log('resolved createdPostId', createdPostId);
298+
console.log(
299+
'pending file upload fields',
300+
pendingFileUploads.map((upload) => ({
301+
fieldKey: upload.fieldKey,
302+
files: upload.files.map((file) => ({
303+
name: file.name,
304+
size: file.size,
305+
type: file.type,
306+
})),
307+
})),
308+
);
309+
console.groupEnd();
310+
311+
if (pendingFileUploads.length > 0) {
312+
if (!createdPostId) {
313+
console.error(
314+
'[new-record] missing createdPostId from createPost response',
315+
response,
316+
);
317+
throw new Error(
318+
'Unable to determine created record ID for file uploads.',
319+
);
320+
}
321+
for (const upload of pendingFileUploads) {
322+
const keyPrefix = `${window.new_record_localized.post_type}/${createdPostId}/${upload.fieldKey}`;
323+
console.log('[new-record] uploadFiles request', {
324+
postType: window.new_record_localized.post_type,
325+
postId: createdPostId,
326+
fieldKey: upload.fieldKey,
327+
keyPrefix,
328+
fileCount: upload.files.length,
329+
});
330+
try {
331+
await window.componentService.api.uploadFiles(
332+
window.new_record_localized.post_type,
333+
createdPostId,
334+
upload.files,
335+
upload.fieldKey,
336+
keyPrefix,
337+
);
338+
} catch (uploadError) {
339+
console.error('[new-record] uploadFiles failed', {
340+
fieldKey: upload.fieldKey,
341+
error: uploadError,
342+
args: uploadError?.args,
343+
});
344+
upload.component.setAttribute(
345+
'error',
346+
uploadError?.message || 'Upload failed',
347+
);
348+
throw uploadError;
349+
}
350+
}
351+
}
352+
258353
window.location = response.permalink;
259354
})
260355
.catch(function (error) {
261-
const message = error.responseJSON?.message || error.responseText;
356+
console.error('[new-record] submit failed', error);
357+
const message =
358+
error?.message ||
359+
error?.responseJSON?.message ||
360+
error?.responseText ||
361+
'Unable to save record.';
262362
$('.js-create-post-button')
263363
.removeClass('loading')
264364
.addClass('alert')

dt-core/admin/css/dt-settings.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,20 @@
8181
width: auto;
8282
height: auto;
8383
min-width: 33%;
84+
max-height: 90vh;
8485
background: #fefefe;
8586
border: 1px solid #cacaca;
8687
-webkit-backface-visibility: hidden;
8788
backface-visibility: hidden;
89+
overflow: hidden;
8890
}
8991
.modal-back {
9092
transform: rotateY(180deg);
9193
}
9294
.dt-admin-modal-box-content {
9395
padding: 1rem;
96+
max-height: calc(90vh - 2rem);
97+
overflow-y: auto;
9498
}
9599
.tile-rundown-elements {
96100
margin-top: -1px;

0 commit comments

Comments
 (0)