Skip to content

Commit

Permalink
feat(category): add upload for drag drop over category nav
Browse files Browse the repository at this point in the history
produced an error beforehand
  • Loading branch information
Yelinz authored and czosel committed Mar 20, 2024
1 parent 2773a08 commit d2e7df5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
2 changes: 1 addition & 1 deletion addon/components/category-nav/category.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{{on "dragenter" this.onDragEnter}}
{{on "dragleave" this.onDragLeave}}
{{on "dragover" this.onDragOver}}
{{on "drop" (perform this.onDrop)}}
{{on "drop" this.onDrop.perform}}
>
<div
class="uk-link-reset {{if this.isActive "active"}}"
Expand Down
24 changes: 14 additions & 10 deletions addon/components/category-nav/category.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { action } from "@ember/object";
import { inject as service } from "@ember/service";
import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";
import { dropTask } from "ember-concurrency";
import { task } from "ember-concurrency";

import { ErrorHandler } from "ember-alexandria/helpers/error-handler";

export default class CategoryNavCategoryComponent extends Component {
@service("alexandria-documents") documents;
Expand Down Expand Up @@ -81,18 +83,24 @@ export default class CategoryNavCategoryComponent extends Component {
event.stopPropagation();
}

@dropTask
*onDrop(event) {
onDrop = task({ drop: true }, async (event) => {
event.preventDefault();

if (!this.args.category.id) return;

this.dragCounter = 0;
this.isDragOver = false;

if (event.dataTransfer.files.length) {
return await this.documents.upload(
this.args.category,
event.dataTransfer.files,
);
}

const documentIds = event.dataTransfer.getData("text").split(",");

const success = yield Promise.all(
const success = await Promise.all(
documentIds.map(async (id) => {
const document = this.store.peekRecord("document", id);

Expand All @@ -112,11 +120,7 @@ export default class CategoryNavCategoryComponent extends Component {
} catch (error) {
document.category = previousCategory;

if (error.errors[0].status === "403") {
this.notification.danger(
this.intl.t("alexandria.errors.no-permission"),
);
}
new ErrorHandler(this, error).notify();

return false;
}
Expand Down Expand Up @@ -151,5 +155,5 @@ export default class CategoryNavCategoryComponent extends Component {
},
});
}
}
});
}
4 changes: 3 additions & 1 deletion addon/helpers/error-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export class ErrorHandler {
return this.notification.danger(firstError.detail);
}

return this.notification.danger(this.intl.t(...args));
if (args.length) {
return this.notification.danger(this.intl.t(...args));
}
}
}

0 comments on commit d2e7df5

Please sign in to comment.