Skip to content

Commit 7c05dee

Browse files
Fix: Add tus data to ecc-utils-design-form on upload (#388)
Co-authored-by: Salihu <salihudickson@gmail.com>
1 parent 05261a1 commit 7c05dee

2 files changed

Lines changed: 30 additions & 10 deletions

File tree

.changeset/afraid-pugs-rush.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@elixir-cloud/design": patch
3+
---
4+
5+
return data from tus upload on ecc-utils-design-form

packages/ecc-utils-design/src/components/form/form.ts

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -164,12 +164,12 @@ export default class EccUtilsDesignForm extends LitElement {
164164
private handleTusFileUpload = async (
165165
e: Event,
166166
field: Field
167-
): Promise<void> => {
167+
): Promise<Record<string, string> | null> => {
168168
const file = (e.target as HTMLInputElement).files?.[0];
169169

170170
if (!file) {
171171
console.error("No file selected for upload.");
172-
return;
172+
return null;
173173
}
174174

175175
try {
@@ -194,11 +194,20 @@ export default class EccUtilsDesignForm extends LitElement {
194194
this.requestUpdate();
195195
},
196196
onSuccess: () => {
197+
const data: any = {
198+
url: upload.url,
199+
file,
200+
name: "",
201+
};
202+
197203
if ("name" in upload.file) {
198204
console.log("Download %s from %s", upload.file.name, upload.url);
205+
data.name = upload.file.name;
199206
} else {
200207
console.log("Download file from %s", upload.url);
201208
}
209+
210+
return data;
202211
},
203212
});
204213

@@ -211,6 +220,8 @@ export default class EccUtilsDesignForm extends LitElement {
211220
} catch (error) {
212221
console.error("An error occurred while initializing the upload:", error);
213222
}
223+
224+
return null;
214225
};
215226

216227
renderInputTemplate(field: Field, path: string): TemplateResult {
@@ -248,7 +259,12 @@ export default class EccUtilsDesignForm extends LitElement {
248259
class="file-input"
249260
?disabled=${field.fieldOptions?.readonly}
250261
@change=${async (e: Event) => {
251-
await this.handleTusFileUpload(e, field);
262+
const data = await this.handleTusFileUpload(e, field);
263+
264+
if (data) {
265+
_.set(this.form, path, data);
266+
this.alertFieldChange(field.key, data);
267+
}
252268
}}
253269
/>
254270
<div class="progress-bar-container">
@@ -548,17 +564,19 @@ export default class EccUtilsDesignForm extends LitElement {
548564
if (field.type === "array") {
549565
return this.renderArrayTemplate(field, newPath);
550566
}
567+
if (field.type === "switch") {
568+
return this.renderSwitchTemplate(field, newPath);
569+
}
551570

552571
if (field.fieldOptions?.required) {
553572
if (
554573
!_.get(this.form, newPath) &&
555574
!this.requiredButEmpty.includes(field.key)
556575
) {
557576
// add to requiredButEmpty
558-
559-
// eslint-disable-next-line no-empty
560-
if (!this.hasUpdated && field.fieldOptions.default) {
561-
} else this.requiredButEmpty.push(field.key);
577+
if (this.hasUpdated || !field.fieldOptions.default) {
578+
this.requiredButEmpty.push(field.key);
579+
}
562580
} else if (_.get(this.form, newPath)) {
563581
// remove from requiredButEmpty
564582
this.requiredButEmpty = this.requiredButEmpty.filter(
@@ -567,9 +585,6 @@ export default class EccUtilsDesignForm extends LitElement {
567585
}
568586
}
569587

570-
if (field.type === "switch") {
571-
return this.renderSwitchTemplate(field, newPath);
572-
}
573588
return this.renderInputTemplate(field, newPath);
574589
}
575590

0 commit comments

Comments
 (0)