|
1 | 1 | <script> |
| 2 | + import { downloadBlob } from '$lib/common/component_utilities'; |
2 | 3 | import { displayStandardErrorAlert, getAlertErrorFromResponse } from '$lib/common/errors'; |
3 | 4 | import ConfirmActionButton from '$lib/components/common/ConfirmActionButton.svelte'; |
4 | 5 | import { onMount } from 'svelte'; |
5 | 6 |
|
6 | 7 | /** @type {Array<import('fractal-components/types/api').Resource>} */ |
7 | 8 | let resources = $state([]); |
8 | 9 | /** @type {import('$lib/components/common/StandardErrorAlert.svelte').default|undefined} */ |
9 | | - let searchErrorAlert; |
| 10 | + let resourcesErrorAlert; |
10 | 11 |
|
11 | 12 | async function loadResources() { |
12 | | - searchErrorAlert?.hide(); |
| 13 | + resourcesErrorAlert?.hide(); |
13 | 14 | const url = new URL('/api/admin/v2/resource', window.location.origin); |
14 | 15 | const response = await fetch(url); |
15 | 16 | if (!response.ok) { |
16 | | - searchErrorAlert = displayStandardErrorAlert( |
| 17 | + resourcesErrorAlert = displayStandardErrorAlert( |
17 | 18 | await getAlertErrorFromResponse(response), |
18 | | - 'searchError' |
| 19 | + 'resourcesError' |
19 | 20 | ); |
20 | 21 | return; |
21 | 22 | } |
|
37 | 38 | } |
38 | 39 | } |
39 | 40 |
|
| 41 | + /** |
| 42 | + * @param {number} resourceId |
| 43 | + */ |
| 44 | + async function exportToFile(resourceId) { |
| 45 | + const response = await fetch(`/api/admin/v2/resource/${resourceId}`); |
| 46 | + if (response.ok) { |
| 47 | + /** @type {import('fractal-components/types/api').Resource} */ |
| 48 | + const resource = await response.json(); |
| 49 | + downloadBlob(JSON.stringify(resource, null, 2), `${resource.name}.json`, 'application/json'); |
| 50 | + } else { |
| 51 | + resourcesErrorAlert = displayStandardErrorAlert( |
| 52 | + await getAlertErrorFromResponse(response), |
| 53 | + 'resourcesError' |
| 54 | + ); |
| 55 | + } |
| 56 | + } |
| 57 | +
|
40 | 58 | onMount(async () => { |
41 | 59 | await loadResources(); |
42 | 60 | }); |
|
78 | 96 | <a href="/v2/admin/resources/{resource.id}/edit" class="btn btn-primary"> |
79 | 97 | <i class="bi bi-pencil"></i> Edit |
80 | 98 | </a> |
| 99 | + <button |
| 100 | + type="button" |
| 101 | + class="btn btn-outline-primary" |
| 102 | + onclick={() => exportToFile(resource.id)} |
| 103 | + > |
| 104 | + <i class="bi bi-download"></i> Export to file |
| 105 | + </button> |
81 | 106 | <ConfirmActionButton |
82 | 107 | modalId={'confirmDeleteResource' + resource.id} |
83 | 108 | style="danger" |
|
93 | 118 | </tbody> |
94 | 119 | </table> |
95 | 120 |
|
96 | | - <div id="searchError" class="mt-3"></div> |
| 121 | + <div id="resourcesError" class="mt-3"></div> |
97 | 122 | </div> |
98 | 123 | </div> |
0 commit comments