Skip to content

Commit

Permalink
add tus upload data to ecc-utils-form object
Browse files Browse the repository at this point in the history
  • Loading branch information
Salihu committed Nov 18, 2024
1 parent 589a150 commit 84b6f9c
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions packages/ecc-utils-design/src/components/form/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,12 @@ export default class EccUtilsDesignForm extends LitElement {
private handleTusFileUpload = async (
e: Event,
field: Field
): Promise<void> => {
): Promise<Record<string, string> | null> => {
const file = (e.target as HTMLInputElement).files?.[0];

if (!file) {
console.error("No file selected for upload.");
return;
return null;
}

try {
Expand All @@ -194,11 +194,20 @@ export default class EccUtilsDesignForm extends LitElement {
this.requestUpdate();
},
onSuccess: () => {
const data: any = {
url: upload.url,
file,
name: "",
};

if ("name" in upload.file) {
console.log("Download %s from %s", upload.file.name, upload.url);
data.name = upload.file.name;
} else {
console.log("Download file from %s", upload.url);
}

return data;
},
});

Expand All @@ -211,6 +220,8 @@ export default class EccUtilsDesignForm extends LitElement {
} catch (error) {
console.error("An error occurred while initializing the upload:", error);
}

return null;
};

renderInputTemplate(field: Field, path: string): TemplateResult {
Expand Down Expand Up @@ -248,7 +259,12 @@ export default class EccUtilsDesignForm extends LitElement {
class="file-input"
?disabled=${field.fieldOptions?.readonly}
@change=${async (e: Event) => {
await this.handleTusFileUpload(e, field);
const data = await this.handleTusFileUpload(e, field);
if (data) {
_.set(this.form, path, data);
this.alertFieldChange(field.key, data);
}
}}
/>
<div class="progress-bar-container">
Expand Down

0 comments on commit 84b6f9c

Please sign in to comment.