Skip to content

Commit 0fe038d

Browse files
authored
Bug when editing submissions - file input (#357)
1 parent 31e5624 commit 0fe038d

File tree

1 file changed

+32
-51
lines changed

1 file changed

+32
-51
lines changed

client/components/forms/FileInput.vue

+32-51
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,35 @@
11
<template>
2-
<input-wrapper
3-
v-bind="inputWrapperProps"
4-
>
2+
<input-wrapper v-bind="inputWrapperProps">
53
<template #label>
64
<slot name="label" />
75
</template>
86

9-
<div class="flex w-full items-center justify-center transition-colors duration-40"
10-
:class="[{'!cursor-not-allowed':disabled, 'cursor-pointer':!disabled,
11-
[theme.fileInput.inputHover.light + ' dark:'+theme.fileInput.inputHover.dark]: uploadDragoverEvent,
12-
['hover:'+theme.fileInput.inputHover.light +' dark:hover:'+theme.fileInput.inputHover.dark]: !loading}, theme.fileInput.input]"
13-
@dragover.prevent="uploadDragoverEvent=true"
14-
@dragleave.prevent="uploadDragoverEvent=false"
15-
@drop.prevent="onUploadDropEvent"
16-
@click="openFileUpload"
17-
>
18-
<div
19-
v-if="loading"
20-
class="text-gray-600 dark:text-gray-400"
21-
>
7+
<div class="flex w-full items-center justify-center transition-colors duration-40" :class="[{
8+
'!cursor-not-allowed': disabled, 'cursor-pointer': !disabled,
9+
[theme.fileInput.inputHover.light + ' dark:' + theme.fileInput.inputHover.dark]: uploadDragoverEvent,
10+
['hover:' + theme.fileInput.inputHover.light + ' dark:hover:' + theme.fileInput.inputHover.dark]: !loading
11+
}, theme.fileInput.input]" @dragover.prevent="uploadDragoverEvent = true"
12+
@dragleave.prevent="uploadDragoverEvent = false" @drop.prevent="onUploadDropEvent" @click="openFileUpload">
13+
<div v-if="loading" class="text-gray-600 dark:text-gray-400">
2214
<Loader class="mx-auto h-6 w-6" />
2315
<p class="mt-2 text-center text-sm text-gray-500">
2416
Uploading your file...
2517
</p>
2618
</div>
2719
<template v-else>
2820
<div class="text-center">
29-
<input
30-
ref="actual-input"
31-
class="hidden"
32-
:multiple="multiple"
33-
type="file"
34-
:name="name"
35-
:accept="acceptExtensions"
36-
@change="manualFileUpload"
37-
>
21+
<input ref="actual-input" class="hidden" :multiple="multiple" type="file" :name="name"
22+
:accept="acceptExtensions" @change="manualFileUpload">
3823
<div v-if="files.length" class="flex flex-wrap items-center justify-center gap-4">
39-
<uploaded-file
40-
v-for="file in files"
41-
:key="file.url"
42-
:file="file"
43-
:theme="theme"
44-
@remove="clearFile(file)"
45-
/>
24+
<uploaded-file v-for="file in files" :key="file.url" :file="file" :theme="theme"
25+
@remove="clearFile(file)" />
4626
</div>
4727
<template v-else>
4828
<div class="text-gray-500 w-full flex justify-center">
4929
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5"
50-
stroke="currentColor" class="w-6 h-6"
51-
>
30+
stroke="currentColor" class="w-6 h-6">
5231
<path stroke-linecap="round" stroke-linejoin="round"
53-
d="M3 16.5v2.25A2.25 2.25 0 005.25 21h13.5A2.25 2.25 0 0021 18.75V16.5m-13.5-9L12 3m0 0l4.5 4.5M12 3v13.5"
54-
/>
32+
d="M3 16.5v2.25A2.25 2.25 0 005.25 21h13.5A2.25 2.25 0 0021 18.75V16.5m-13.5-9L12 3m0 0l4.5 4.5M12 3v13.5" />
5533
</svg>
5634
</div>
5735

@@ -79,7 +57,7 @@
7957
import { inputProps, useFormInput } from './useFormInput.js'
8058
import InputWrapper from './components/InputWrapper.vue'
8159
import UploadedFile from './components/UploadedFile.vue'
82-
import {storeFile} from "~/lib/file-uploads.js"
60+
import { storeFile } from "~/lib/file-uploads.js"
8361
8462
export default {
8563
name: 'FileInput',
@@ -93,7 +71,7 @@ export default {
9371
moveToFormAssets: { type: Boolean, default: false }
9472
},
9573
96-
setup (props, context) {
74+
setup(props, context) {
9775
return {
9876
...useFormInput(props, context)
9977
}
@@ -106,10 +84,10 @@ export default {
10684
}),
10785
10886
computed: {
109-
currentUrl () {
87+
currentUrl() {
11088
return this.form[this.name]
11189
},
112-
acceptExtensions () {
90+
acceptExtensions() {
11391
if (!this.accept) {
11492
return null
11593
}
@@ -126,13 +104,13 @@ export default {
126104
watch: {
127105
files: {
128106
deep: true,
129-
handler (files) {
107+
handler(files) {
130108
this.compVal = files.map((file) => file.url)
131109
}
132110
}
133111
},
134112
135-
async created () {
113+
async created() {
136114
if (typeof this.compVal === 'string' || this.compVal instanceof String) {
137115
await this.getFileFromUrl(this.compVal).then((fileObj) => {
138116
this.files = [{
@@ -157,34 +135,34 @@ export default {
157135
},
158136
159137
methods: {
160-
clearAll () {
138+
clearAll() {
161139
this.files = []
162140
},
163-
clearFile (index) {
141+
clearFile(index) {
164142
this.files.splice(index, 1)
165143
},
166-
onUploadDropEvent (e) {
144+
onUploadDropEvent(e) {
167145
this.uploadDragoverEvent = false
168146
this.droppedFiles(e.dataTransfer.files)
169147
},
170-
droppedFiles (droppedFiles) {
148+
droppedFiles(droppedFiles) {
171149
if (!droppedFiles || this.disabled) return
172150
173151
for (let i = 0; i < droppedFiles.length; i++) {
174152
this.uploadFileToServer(droppedFiles.item(i))
175153
}
176154
},
177-
openFileUpload () {
155+
openFileUpload() {
178156
if (this.disabled || !this.$refs['actual-input']) return
179157
this.$refs['actual-input'].click()
180158
},
181-
manualFileUpload (e) {
159+
manualFileUpload(e) {
182160
const files = e.target.files
183161
for (let i = 0; i < files.length; i++) {
184162
this.uploadFileToServer(files.item(i))
185163
}
186164
},
187-
uploadFileToServer (file) {
165+
uploadFileToServer(file) {
188166
if (this.disabled) return
189167
this.loading = true
190168
storeFile(file)
@@ -224,15 +202,18 @@ export default {
224202
this.loading = false
225203
})
226204
},
227-
async getFileFromUrl (url, defaultType = 'image/jpeg') {
205+
async getFileFromUrl(url, defaultType = 'image/jpeg') {
206+
if (typeof url === 'object') {
207+
url = url?.file_url
208+
}
228209
const response = await fetch(url)
229210
const data = await response.blob()
230211
const name = url.replace(/^.*(\\|\/|\:)/, '')
231212
return new File([data], name, {
232213
type: data.type || defaultType
233214
})
234215
},
235-
getFileSrc (file) {
216+
getFileSrc(file) {
236217
if (file.type && file.type.split('/')[0] === 'image') {
237218
return URL.createObjectURL(file)
238219
}

0 commit comments

Comments
 (0)