forked from projectcaluma/ember-emeis
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: show user export button (projectcaluma#412)
- Loading branch information
1 parent
bb0f8f2
commit ecd31a0
Showing
16 changed files
with
146 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import Service from "@ember/service"; | ||
|
||
export default class FetchService extends Service {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
<SectionTitle @model="users" /> | ||
<SectionTitle @model="users" @onExport={{perform this.export}} /> | ||
|
||
{{outlet}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from "ember-emeis/controllers/users"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from "ember-emeis/services/fetch"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import Service from "@ember/service"; | ||
import { isEmpty } from "@ember/utils"; | ||
|
||
const cleanObject = (obj) => { | ||
return Object.entries(obj).reduce((clean, [key, value]) => { | ||
return { | ||
...clean, | ||
...(isEmpty(value) ? {} : { [key]: value }), | ||
}; | ||
}, {}); | ||
}; | ||
|
||
export default class FetchService extends Service { | ||
async fetch(resource, init = {}) { | ||
init.headers = cleanObject({ | ||
...this.headers, | ||
...(init.headers || {}), | ||
}); | ||
|
||
const response = await fetch(resource, init); | ||
if (!response.ok) { | ||
if (response.status === 401) { | ||
// handle unauthorized case | ||
return; | ||
} | ||
|
||
const contentType = response.headers.get("content-type"); | ||
let body = ""; | ||
|
||
if (/^application\/(vnd\.api+)?json$/.test(contentType)) { | ||
body = await response.json(); | ||
} else if (contentType === "text/plain") { | ||
body = await response.text(); | ||
} | ||
|
||
// throw an error containing a human readable message | ||
throw { | ||
response, | ||
body, | ||
error: new Error( | ||
`Fetch request to URL ${response.url} returned ${response.status} ${response.statusText}:\n\n${body}` | ||
), | ||
}; | ||
} | ||
|
||
return response; | ||
} | ||
|
||
get headers() { | ||
return { | ||
// authorization: "Bearer 123123123", | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { setupTest } from "ember-qunit"; | ||
import { module, test } from "qunit"; | ||
|
||
module("Unit | Controller | users", function (hooks) { | ||
setupTest(hooks); | ||
|
||
// TODO: Replace this with your real tests. | ||
test("it exists", function (assert) { | ||
const controller = this.owner.lookup("controller:users"); | ||
assert.ok(controller); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8785,6 +8785,11 @@ file-entry-cache@^6.0.1: | |
dependencies: | ||
flat-cache "^3.0.4" | ||
|
||
file-saver@^2.0.5: | ||
version "2.0.5" | ||
resolved "https://registry.yarnpkg.com/file-saver/-/file-saver-2.0.5.tgz#d61cfe2ce059f414d899e9dd6d4107ee25670c38" | ||
integrity sha512-P9bmyZ3h/PRG+Nzga+rbdI4OEpNDzAVyy74uVO9ATgzLK6VtAsYybF/+TOCvrc0MO793d6+42lLyZTw7/ArVzA== | ||
|
||
[email protected]: | ||
version "1.0.0" | ||
resolved "https://registry.yarnpkg.com/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz#553a7b8446ff6f684359c445f1e37a05dacc33dd" | ||
|