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

118 lines
2.6 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'
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-05-21 17:59:36 +03:00
methods: {
async upload () {
2023-05-31 00:50:43 +03:00
this.isLoading = true
2024-04-06 15:00:30 +03:00
this.baseFetch(`/templates/${this.templateId}/documents`, {
method: 'POST',
body: new FormData(this.$refs.form)
}).then((resp) => {
if (resp.ok) {
resp.json().then((data) => {
this.$emit('success', data)
this.$refs.input.value = ''
})
} else if (resp.status === 422) {
resp.json().then((data) => {
if (data.error === 'PDF encrypted') {
const formData = new FormData(this.$refs.form)
2023-05-21 17:59:36 +03:00
2024-04-06 15:00:30 +03:00
formData.append('password', prompt('Enter PDF password'))
2023-05-21 17:59:36 +03:00
2024-04-06 15:00:30 +03:00
this.baseFetch(`/templates/${this.templateId}/documents`, {
method: 'POST',
body: formData
}).then(async (resp) => {
if (resp.ok) {
this.$emit('success', await resp.json())
this.$refs.input.value = ''
} else {
2024-04-06 15:00:30 +03:00
alert('Wrong password')
}
})
2024-04-06 15:00:30 +03:00
}
2023-05-21 17:59:36 +03:00
})
2024-04-06 15:00:30 +03:00
}
}).finally(() => {
this.isLoading = false
})
2023-05-21 17:59:36 +03:00
}
}
}
</script>