Skip to content

Commit

Permalink
feat(user): filter export (#722)
Browse files Browse the repository at this point in the history
  • Loading branch information
czosel authored Oct 12, 2023
1 parent d84d949 commit 2b53e45
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 18 deletions.
16 changes: 0 additions & 16 deletions addon/controllers/users.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,8 @@
import Controller from "@ember/controller";
import { inject as service } from "@ember/service";
import { task } from "ember-concurrency";
import { saveAs } from "file-saver";

import { handleTaskErrors } from "ember-emeis/-private/decorators";

export default class UsersController extends Controller {
@service notification;
@service intl;
@service fetch;

@task
@handleTaskErrors
*export() {
const response = yield this.fetch.fetch(`/api/v1/users/export`);

const filename = response.headers
.get("content-disposition")
.match(/filename="(.*)"/)[1];

saveAs(yield response.blob(), filename);
}
}
23 changes: 23 additions & 0 deletions addon/controllers/users/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { action } from "@ember/object";
import { inject as service } from "@ember/service";
import { tracked } from "@glimmer/tracking";
import { task } from "ember-concurrency";
import { saveAs } from "file-saver";

import { confirmTask } from "../../decorators/confirm-task";

Expand All @@ -12,6 +13,7 @@ export default class UsersIndexController extends PaginationController {
@service emeisOptions;
@service notification;
@service intl;
@service fetch;

@tracked filter_active = true;

Expand Down Expand Up @@ -53,4 +55,25 @@ export default class UsersIndexController extends PaginationController {
yield model.destroyRecord();
this.notification.success(this.intl.t("emeis.form.delete-success"));
}

@task
@handleTaskErrors
*export() {
const filters = Object.fromEntries(
Object.entries(this.filters).map(([key, value]) => [
`filter[${key}]`,
value,
])
);
const queryParams = new URLSearchParams(filters).toString();
const response = yield this.fetch.fetch(
`/api/v1/users/export?${queryParams}`
);

const filename = response.headers
.get("content-disposition")
.match(/filename="(.*)"/)[1];

saveAs(yield response.blob(), filename);
}
}
2 changes: 0 additions & 2 deletions addon/templates/users.hbs
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
<SectionTitle @model="users" @onExport={{perform this.export}} />

{{outlet}}
2 changes: 2 additions & 0 deletions addon/templates/users/index.hbs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<SectionTitle @model="users" @onExport={{perform this.export}} />

<DataTable
@modelName="user"
@page={{this.page}}
Expand Down

0 comments on commit 2b53e45

Please sign in to comment.