Files
docuseal/app/javascript/template_builder/upload.vue
T

147 lines
3.3 KiB
Vue
Raw Normal View History

2023-05-21 17:59:36 +03:00
<template>
2023-05-31 00:50:43 +03:00
<div>
<label
:for="inputId"
class="btn btn-outline w-full"
2023-07-01 00:15:15 +03:00
:class="{ 'btn-disabled': isLoading || isProcessing }"
2023-05-31 00:50:43 +03:00
>
2023-07-01 00:15:15 +03:00
<IconInnerShadowTop
v-if="isLoading || isProcessing"
width="20"
class="animate-spin"
/>
2023-05-31 00:50:43 +03:00
<IconUpload
2023-07-01 00:15:15 +03:00
v-else
2023-05-31 00:50:43 +03:00
width="20"
/>
2023-07-01 00:15:15 +03:00
<span v-if="isLoading">
2024-01-04 01:05:36 +02:00
{{ t('uploading_') }}
2023-07-01 00:15:15 +03:00
</span>
<span v-else-if="isProcessing">
2024-01-04 01:05:36 +02:00
{{ t('processing_') }}
2023-07-01 00:15:15 +03:00
</span>
<span v-else>
2024-01-04 01:05:36 +02:00
{{ t('add_document') }}
2023-07-01 00:15:15 +03:00
</span>
2023-05-31 00:50:43 +03:00
</label>
<form
ref="form"
2023-05-31 00:50:43 +03:00
class="hidden"
>
<input
:id="inputId"
ref="input"
name="files[]"
type="file"
2023-09-05 20:33:40 +03:00
:accept="acceptFileTypes"
multiple
@change="upload"
>
</form>
2023-05-31 00:50:43 +03:00
</div>
2023-05-21 17:59:36 +03:00
</template>
<script>
2023-07-01 00:15:15 +03:00
import { IconUpload, IconInnerShadowTop } from '@tabler/icons-vue'
2023-05-21 17:59:36 +03:00
export default {
name: 'DocumentsUpload',
2023-05-31 00:50:43 +03:00
components: {
2023-07-01 00:15:15 +03:00
IconUpload,
IconInnerShadowTop
2023-05-31 00:50:43 +03:00
},
2024-01-04 01:05:36 +02:00
inject: ['baseFetch', 't'],
2023-05-21 17:59:36 +03:00
props: {
2023-05-31 23:19:20 +03:00
templateId: {
2023-05-21 17:59:36 +03:00
type: [Number, String],
required: true
},
2023-09-05 20:33:40 +03:00
acceptFileTypes: {
type: String,
required: false,
default: 'image/*, application/pdf'
},
isDirectUpload: {
type: Boolean,
required: true,
default: false
2023-05-21 17:59:36 +03:00
}
},
emits: ['success'],
2023-05-31 00:50:43 +03:00
data () {
return {
2023-07-01 00:15:15 +03:00
isLoading: false,
isProcessing: false
2023-05-31 00:50:43 +03:00
}
},
computed: {
inputId () {
return 'el' + Math.random().toString(32).split('.')[1]
}
},
2023-06-24 18:31:12 +03:00
mounted () {
if (this.isDirectUpload) {
import('@rails/activestorage')
}
2023-06-24 18:31:12 +03:00
},
2023-05-21 17:59:36 +03:00
methods: {
async upload () {
2023-05-31 00:50:43 +03:00
this.isLoading = true
if (this.isDirectUpload) {
const { DirectUpload } = await import('@rails/activestorage')
2023-06-24 18:31:12 +03:00
const blobs = await Promise.all(
Array.from(this.$refs.input.files).map(async (file) => {
const upload = new DirectUpload(
file,
'/direct_uploads',
this.$refs.input
)
2023-05-21 17:59:36 +03:00
return new Promise((resolve, reject) => {
upload.create((error, blob) => {
if (error) {
console.error(error)
2023-05-21 17:59:36 +03:00
return reject(error)
} else {
return resolve(blob)
}
})
}).catch((error) => {
console.error(error)
2023-05-21 17:59:36 +03:00
})
})
).finally(() => {
this.isLoading = false
2023-05-21 17:59:36 +03:00
})
2023-07-01 00:15:15 +03:00
this.isProcessing = true
2023-05-21 17:59:36 +03:00
2023-08-19 21:05:54 +03:00
this.baseFetch(`/api/templates/${this.templateId}/documents`, {
method: 'POST',
body: JSON.stringify({ blobs }),
headers: { 'Content-Type': 'application/json' }
}).then(resp => resp.json()).then((data) => {
this.$emit('success', data)
this.$refs.input.value = ''
}).finally(() => {
this.isProcessing = false
})
} else {
2023-08-19 21:05:54 +03:00
this.baseFetch(`/api/templates/${this.templateId}/documents`, {
method: 'POST',
body: new FormData(this.$refs.form)
}).then(resp => resp.json()).then((data) => {
this.$emit('success', data)
this.$refs.input.value = ''
}).finally(() => {
this.isLoading = false
})
}
2023-05-21 17:59:36 +03:00
}
}
}
</script>