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

132 lines
3.0 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
2024-07-09 23:47:46 +03:00
id="add_document_button"
2023-05-31 00:50:43 +03:00
:for="inputId"
2025-03-18 17:34:11 +02:00
class="btn btn-outline w-full add-document-button"
2025-05-09 00:01:50 +03:00
:class="{ 'btn-disabled': isLoading }"
2023-05-31 00:50:43 +03:00
>
2023-07-01 00:15:15 +03:00
<IconInnerShadowTop
2025-05-09 00:01:50 +03:00
v-if="isLoading"
2023-07-01 00:15:15 +03:00
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>
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
}
},
2025-04-25 21:19:28 +03:00
emits: ['success', 'error'],
2023-05-31 00:50:43 +03:00
data () {
return {
2025-05-09 00:01:50 +03:00
isLoading: false
2023-05-31 00:50:43 +03:00
}
},
computed: {
inputId () {
return 'el' + Math.random().toString(32).split('.')[1]
2025-04-25 21:19:28 +03:00
},
uploadUrl () {
return `/templates/${this.templateId}/documents`
2023-05-31 00:50:43 +03:00
}
},
2023-05-21 17:59:36 +03:00
methods: {
async upload () {
2023-05-31 00:50:43 +03:00
this.isLoading = true
2025-04-25 21:19:28 +03:00
this.baseFetch(this.uploadUrl, {
2024-04-06 15:00:30 +03:00
method: 'POST',
2025-04-25 21:19:28 +03:00
headers: { Accept: 'application/json' },
2024-04-06 15:00:30 +03:00
body: new FormData(this.$refs.form)
}).then((resp) => {
if (resp.ok) {
resp.json().then((data) => {
this.$emit('success', data)
this.$refs.input.value = ''
2025-05-09 00:01:50 +03:00
this.isLoading = false
2024-04-06 15:00:30 +03:00
})
} else if (resp.status === 422) {
resp.json().then((data) => {
2024-09-18 19:47:47 +03:00
if (data.status === 'pdf_encrypted') {
2024-04-06 15:00:30 +03:00
const formData = new FormData(this.$refs.form)
2023-05-21 17:59:36 +03:00
2024-09-18 19:47:47 +03:00
formData.append('password', prompt(this.t('enter_pdf_password')))
2023-05-21 17:59:36 +03:00
2025-04-25 21:19:28 +03:00
this.baseFetch(this.uploadUrl, {
2024-04-06 15:00:30 +03:00
method: 'POST',
body: formData
}).then(async (resp) => {
if (resp.ok) {
this.$emit('success', await resp.json())
this.$refs.input.value = ''
2025-05-09 00:01:50 +03:00
this.isLoading = false
} else {
2024-09-18 19:47:47 +03:00
alert(this.t('wrong_password'))
2025-04-25 21:19:28 +03:00
this.$emit('error', await resp.json().error)
2025-05-09 00:01:50 +03:00
this.isLoading = false
}
})
2025-04-25 21:19:28 +03:00
} else {
this.$emit('error', data.error)
2025-05-09 00:01:50 +03:00
this.isLoading = false
2024-04-06 15:00:30 +03:00
}
2023-05-21 17:59:36 +03:00
})
2025-04-25 21:19:28 +03:00
} else {
resp.json().then((data) => {
this.$emit('error', data.error)
2025-05-09 00:01:50 +03:00
this.isLoading = false
2025-04-25 21:19:28 +03:00
})
2024-04-06 15:00:30 +03:00
}
2025-05-09 00:01:50 +03:00
}).catch(() => {
2024-04-06 15:00:30 +03:00
this.isLoading = false
})
2023-05-21 17:59:36 +03:00
}
}
}
</script>