Skip to content

Commit

Permalink
fix(frontend): resolve fetched error message in file upload component
Browse files Browse the repository at this point in the history
  • Loading branch information
paulrobertlloyd committed Apr 9, 2024
1 parent 1a4d0ad commit dc6174a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 11 additions & 7 deletions packages/frontend/components/file-input/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { IndiekitError } from "@indiekit/error";
import { wrapElement } from "../../lib/utils/wrap-element.js";

export const FileInputFieldController = class extends HTMLElement {
Expand All @@ -8,6 +9,7 @@ export const FileInputFieldController = class extends HTMLElement {

/** @type {HTMLElement} */
this.$uploadProgress = this.querySelector(".file-input__progress");
/** @type {HTMLInputElement} */
this.$fileInputPath = this.querySelector(".file-input__path");
this.$fileInputPicker = this.querySelector(".file-input__picker");
this.$fileInputPickerTemplate = this.querySelector("#file-input-picker");
Expand Down Expand Up @@ -76,21 +78,23 @@ export const FileInputFieldController = class extends HTMLElement {
this.$fileInputPath.readOnly = true;

const endpointResponse = await fetch(this.endpoint, {
method: "POST",
body: formData,
method: "POST",
headers: {
Accept: "application/json",
},
});

if (endpointResponse.ok) {
this.$fileInputPath.value =
await endpointResponse.headers.get("location");
} else {
this.showErrorMessage(endpointResponse.statusText);
if (!endpointResponse.ok) {
throw await IndiekitError.fromFetch(endpointResponse);
}

this.$fileInputPath.value =
await endpointResponse.headers.get("location");
this.$fileInputPath.readOnly = false;
this.$uploadProgress.hidden = true;
} catch (error) {
this.showErrorMessage(error);
this.showErrorMessage(error.message);
this.$fileInputPath.readOnly = false;
this.$uploadProgress.hidden = true;
}
Expand Down
1 change: 1 addition & 0 deletions packages/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
},
"dependencies": {
"@accessible-components/tag-input": "^0.2.0",
"@indiekit/error": "^1.0.0-beta.8",
"@indiekit/util": "^1.0.0-beta.10",
"@rollup/plugin-commonjs": "^25.0.3",
"@rollup/plugin-node-resolve": "^15.0.0",
Expand Down

0 comments on commit dc6174a

Please sign in to comment.