|
1 | 1 | <script>
|
| 2 | + import { createEventDispatcher } from 'svelte'; |
2 | 3 | import Button from 'ui/button.svelte';
|
3 | 4 | import FileInput from 'ui/file-input.svelte';
|
4 |
| - import * as api from '@/utils/api.js'; |
5 | 5 | import { API_HOST_BROWSER } from '@/constants/env.js';
|
6 | 6 | import maxSizeMB from '@/constants/backend/file-upload-limit.js';
|
7 | 7 |
|
8 | 8 | export let value = null;
|
9 |
| - let promise = null; |
10 |
| - let blobURL = null; |
11 | 9 | let error = null;
|
12 | 10 |
|
13 |
| - async function handleImageUpload(event) { |
14 |
| - let file = event.target.files[0]; |
| 11 | + function validateUpload(event) { |
| 12 | + const file = event.target.files[0]; |
15 | 13 | if (file.size > maxSizeMB * 1024 * 1024) {
|
16 | 14 | error = `Selected file is too large (${maxSizeMB} MB max).`;
|
17 | 15 | return;
|
18 | 16 | }
|
19 |
| -
|
20 |
| - blobURL = URL.createObjectURL(file, { type: file.type }); |
21 |
| - const formData = new FormData(); |
22 |
| - formData.append('file', file); |
23 |
| -
|
24 |
| - promise = api.post('/file', { |
25 |
| - data: formData, |
26 |
| - }); |
27 |
| -
|
28 |
| - try { |
29 |
| - const resp = await promise; |
30 |
| - if (resp.ok) { |
31 |
| - value = (await resp.json()).id; |
32 |
| - error = null; |
33 |
| - } else { |
34 |
| - console.error(await resp.text()); |
35 |
| - error = 'Upload failed. Try again.'; |
36 |
| - } |
37 |
| - } catch (e) { |
38 |
| - console.error(e); |
39 |
| - error = 'Upload failed. Try again.'; |
40 |
| - } |
| 17 | + dispatch('resize-image', file); |
41 | 18 | }
|
42 | 19 |
|
43 | 20 | function clearValue() {
|
44 | 21 | value = null;
|
45 |
| - promise = null; |
46 |
| - blobURL = null; |
47 | 22 | error = null;
|
48 | 23 | }
|
| 24 | +
|
| 25 | + const dispatch = createEventDispatcher(); |
49 | 26 | </script>
|
50 | 27 |
|
51 | 28 | {#if value != null}
|
|
54 | 31 | <svg src="images/icons/trash-2.svg" class="icon" />
|
55 | 32 | <span class="text">delete</span>
|
56 | 33 | </Button>
|
57 |
| -{:else if promise != null} |
58 |
| - {#await promise.then(resp => resp.json()).catch(() => promise = null)} |
59 |
| - <div class="loading-wrapper shadow-1"> |
60 |
| - <img |
61 |
| - src={blobURL || '/images/create-project/placeholder.svg'} |
62 |
| - alt="Project image" |
63 |
| - /> |
64 |
| - <div class="status-overlay"> |
65 |
| - <div class="lds-ellipsis"> |
66 |
| - <div></div><div></div><div></div><div></div> |
67 |
| - </div> |
68 |
| - </div> |
69 |
| - </div> |
70 |
| - {:then image} |
71 |
| - <img src="{API_HOST_BROWSER}/file/{image.id}" alt="Project image" class="shadow-1 mr" /> |
72 |
| - <Button isDanger on:click={clearValue} tooltip="Delete image"> |
73 |
| - <svg src="images/icons/trash-2.svg" class="icon" /> |
74 |
| - <span class="text">delete</span> |
75 |
| - </Button> |
76 |
| - {/await} |
77 | 34 | {:else}
|
78 | 35 | <div class="options">
|
79 |
| - <FileInput on:change={handleImageUpload} accept="image/*" /> |
| 36 | + <FileInput on:change={validateUpload} accept="image/*" /> |
80 | 37 | {#if error}
|
81 | 38 | <p class="error">{error}</p>
|
82 | 39 | {/if}
|
|
0 commit comments