use vue for form submission
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
<template>
|
||||
<div
|
||||
class="flex cursor-pointer bg-red-100 absolute"
|
||||
:style="computedStyle"
|
||||
>
|
||||
<img
|
||||
v-if="field.type === 'image' && image"
|
||||
:src="image.url"
|
||||
>
|
||||
<img
|
||||
v-else-if="field.type === 'signature' && signature"
|
||||
:src="signature.url"
|
||||
>
|
||||
<div v-else-if="field.type === 'attachment'">
|
||||
<a
|
||||
v-for="(attachment, index) in attachments"
|
||||
:key="index"
|
||||
:href="attachment.url"
|
||||
>
|
||||
{{ attachment.filename }}
|
||||
</a>
|
||||
</div>
|
||||
<span v-else>
|
||||
{{ value }}
|
||||
</span>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
name: 'FieldArea',
|
||||
props: {
|
||||
field: {
|
||||
type: Object,
|
||||
required: true
|
||||
},
|
||||
value: {
|
||||
type: [Array, String, Number, Object],
|
||||
required: false,
|
||||
default: ''
|
||||
},
|
||||
attachmentsIndex: {
|
||||
type: Object,
|
||||
required: false,
|
||||
default: () => ({})
|
||||
},
|
||||
area: {
|
||||
type: Object,
|
||||
required: true
|
||||
}
|
||||
},
|
||||
computed: {
|
||||
image () {
|
||||
if (this.field.type === 'image') {
|
||||
return this.attachmentsIndex[this.value]
|
||||
} else {
|
||||
return null
|
||||
}
|
||||
},
|
||||
signature () {
|
||||
if (this.field.type === 'signature') {
|
||||
return this.attachmentsIndex[this.value]
|
||||
} else {
|
||||
return null
|
||||
}
|
||||
},
|
||||
attachments () {
|
||||
if (this.field.type === 'attachment') {
|
||||
return (this.value || []).map((uuid) => this.attachmentsIndex[uuid])
|
||||
} else {
|
||||
return []
|
||||
}
|
||||
},
|
||||
computedStyle () {
|
||||
const { x, y, w, h } = this.area
|
||||
|
||||
return {
|
||||
top: y * 100 + '%',
|
||||
left: x * 100 + '%',
|
||||
width: w * 100 + '%',
|
||||
height: h * 100 + '%'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
@@ -0,0 +1,77 @@
|
||||
<template>
|
||||
<template
|
||||
v-for="field in fields"
|
||||
:key="field.uuid"
|
||||
>
|
||||
<template
|
||||
v-for="(area, index) in field.areas"
|
||||
:key="index"
|
||||
>
|
||||
<Teleport :to="`#page-${area.attachment_uuid}-${area.page}`">
|
||||
<FieldArea
|
||||
:ref="setAreaRef"
|
||||
:field="field"
|
||||
:area="area"
|
||||
:attachments-index="attachmentsIndex"
|
||||
:value="values[field.uuid]"
|
||||
@click="$emit('focus-field', field)"
|
||||
/>
|
||||
</Teleport>
|
||||
</template>
|
||||
</template>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import FieldArea from './area'
|
||||
|
||||
export default {
|
||||
name: 'FieldAreas',
|
||||
components: {
|
||||
FieldArea
|
||||
},
|
||||
props: {
|
||||
fields: {
|
||||
type: Array,
|
||||
required: false,
|
||||
default: () => []
|
||||
},
|
||||
values: {
|
||||
type: Object,
|
||||
required: false,
|
||||
default: () => ({})
|
||||
},
|
||||
attachmentsIndex: {
|
||||
type: Object,
|
||||
required: false,
|
||||
default: () => ({})
|
||||
}
|
||||
},
|
||||
emits: ['focus-field'],
|
||||
data () {
|
||||
return {
|
||||
areaRefs: []
|
||||
}
|
||||
},
|
||||
beforeUpdate () {
|
||||
this.areaRefs = []
|
||||
},
|
||||
methods: {
|
||||
scrollIntoField (field) {
|
||||
this.areaRefs.find((area) => {
|
||||
if (area.field === field) {
|
||||
area.$el.scrollIntoView({ behavior: 'smooth', block: 'center' })
|
||||
|
||||
return true
|
||||
} else {
|
||||
return null
|
||||
}
|
||||
})
|
||||
},
|
||||
setAreaRef (el) {
|
||||
if (el) {
|
||||
this.areaRefs.push(el)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
@@ -0,0 +1,86 @@
|
||||
<template>
|
||||
<div>
|
||||
<template v-if="modelValue.length">
|
||||
<div
|
||||
v-for="(val, index) in modelValue"
|
||||
:key="index"
|
||||
>
|
||||
<a
|
||||
v-if="val"
|
||||
:href="attachmentsIndex[val].url"
|
||||
>
|
||||
{{ attachmentsIndex[val].filename }}
|
||||
</a>
|
||||
<input
|
||||
:value="val"
|
||||
type="hidden"
|
||||
:name="`values[${field.uuid}][]`"
|
||||
>
|
||||
<button
|
||||
v-if="modelValue"
|
||||
@click.prevent="removeAttachment(val)"
|
||||
>
|
||||
Remove
|
||||
</button>
|
||||
</div>
|
||||
</template>
|
||||
<template v-else>
|
||||
<input
|
||||
value=""
|
||||
type="hidden"
|
||||
:name="`values[${field.uuid}][]`"
|
||||
>
|
||||
</template>
|
||||
<FileDropzone
|
||||
:message="'Attachments'"
|
||||
:submission-slug="submissionSlug"
|
||||
@upload="onUpload"
|
||||
/>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import FileDropzone from './dropzone'
|
||||
|
||||
export default {
|
||||
name: 'AttachmentStep',
|
||||
components: {
|
||||
FileDropzone
|
||||
},
|
||||
props: {
|
||||
field: {
|
||||
type: Object,
|
||||
required: true
|
||||
},
|
||||
submissionSlug: {
|
||||
type: String,
|
||||
required: true
|
||||
},
|
||||
attachmentsIndex: {
|
||||
type: Object,
|
||||
required: false,
|
||||
default: () => ({})
|
||||
},
|
||||
modelValue: {
|
||||
type: Array,
|
||||
required: false,
|
||||
default: () => []
|
||||
}
|
||||
},
|
||||
emits: ['attached', 'update:model-value'],
|
||||
methods: {
|
||||
removeAttachment (uuid) {
|
||||
this.modelValue.splice(this.modelValue.indexOf(uuid), 1)
|
||||
|
||||
this.$emit('update:model-value', this.modelValue)
|
||||
},
|
||||
onUpload (attachments) {
|
||||
attachments.forEach((attachment) => {
|
||||
this.$emit('attached', attachment)
|
||||
})
|
||||
|
||||
this.$emit('update:model-value', [...this.modelValue, ...attachments.map(a => a.uuid)])
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
@@ -0,0 +1,54 @@
|
||||
<template>
|
||||
<div
|
||||
v-for="(option, index) in field.options"
|
||||
:key="index"
|
||||
>
|
||||
<label :for="field.uuid + option">
|
||||
<input
|
||||
:id="field.uuid + option"
|
||||
:ref="setInputRef"
|
||||
type="checkbox"
|
||||
:name="`values[${field.uuid}][]`"
|
||||
:value="option"
|
||||
:checked="modelValue.includes(option)"
|
||||
@change="onChange"
|
||||
>
|
||||
{{ option }}
|
||||
</label>
|
||||
</div>
|
||||
</template>
|
||||
<script>
|
||||
export default {
|
||||
name: 'SheckboxStep',
|
||||
props: {
|
||||
field: {
|
||||
type: Object,
|
||||
required: true
|
||||
},
|
||||
modelValue: {
|
||||
type: Array,
|
||||
required: false,
|
||||
default: () => []
|
||||
}
|
||||
},
|
||||
emits: ['update:model-value'],
|
||||
data () {
|
||||
return {
|
||||
inputRefs: []
|
||||
}
|
||||
},
|
||||
beforeUpdate () {
|
||||
this.inputRefs = []
|
||||
},
|
||||
methods: {
|
||||
setInputRef (el) {
|
||||
if (el) {
|
||||
this.inputRefs.push(el)
|
||||
}
|
||||
},
|
||||
onChange () {
|
||||
this.$emit('update:model-value', this.inputRefs.filter(e => e.checked).map(e => e.value))
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
@@ -0,0 +1,70 @@
|
||||
<template>
|
||||
<div>
|
||||
<p>
|
||||
Form completed - thanks!
|
||||
</p>
|
||||
<button @click.prevent="sendCopyToEmail">
|
||||
<span v-if="isSendingCopy">
|
||||
Sending
|
||||
</span>
|
||||
<span>
|
||||
Send copy to email
|
||||
</span>
|
||||
</button>
|
||||
<button @click.prevent="download">
|
||||
<span v-if="isDownloading">
|
||||
Downloading
|
||||
</span>
|
||||
<span>
|
||||
Download copy
|
||||
</span>
|
||||
</button>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
name: 'FormCompleted',
|
||||
props: {
|
||||
submissionSlug: {
|
||||
type: String,
|
||||
required: true
|
||||
}
|
||||
},
|
||||
data () {
|
||||
return {
|
||||
isSendingCopy: false,
|
||||
isDownloading: false
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
sendCopyToEmail () {
|
||||
this.isSendingCopy = true
|
||||
|
||||
fetch(`/send_submission_email.json?submission_slug=${this.submissionSlug}`, {
|
||||
method: 'POST'
|
||||
}).finally(() => {
|
||||
this.isSendingCopy = false
|
||||
})
|
||||
},
|
||||
download () {
|
||||
this.isDownloading = true
|
||||
|
||||
fetch(`/submissions/${this.submissionSlug}/download`).then(async (response) => {
|
||||
const blob = new Blob([await response.text()], { type: `${response.headers.get('content-type')};charset=utf-8;` })
|
||||
const url = URL.createObjectURL(blob)
|
||||
const link = document.createElement('a')
|
||||
|
||||
link.href = url
|
||||
link.setAttribute('download', response.headers.get('content-disposition').split('"')[1])
|
||||
|
||||
link.click()
|
||||
|
||||
URL.revokeObjectURL(url)
|
||||
}).finally(() => {
|
||||
this.isDownloading = false
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
@@ -0,0 +1,112 @@
|
||||
<template>
|
||||
<div
|
||||
class="flex h-20 w-full"
|
||||
@dragover.prevent
|
||||
@drop.prevent="onDropFiles"
|
||||
>
|
||||
<label
|
||||
:for="inputId"
|
||||
class="w-full"
|
||||
>
|
||||
Upload
|
||||
{{ message }}
|
||||
</label>
|
||||
<input
|
||||
:id="inputId"
|
||||
ref="input"
|
||||
:multiple="multiple"
|
||||
:accept="accept"
|
||||
type="file"
|
||||
class="hidden"
|
||||
@change="onSelectFiles"
|
||||
>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import { DirectUpload } from '@rails/activestorage'
|
||||
|
||||
export default {
|
||||
name: 'FileDropzone',
|
||||
props: {
|
||||
message: {
|
||||
type: String,
|
||||
required: true
|
||||
},
|
||||
submissionSlug: {
|
||||
type: String,
|
||||
required: true
|
||||
},
|
||||
accept: {
|
||||
type: String,
|
||||
required: false,
|
||||
default: '*/*'
|
||||
},
|
||||
multiple: {
|
||||
type: Boolean,
|
||||
required: false,
|
||||
default: false
|
||||
}
|
||||
},
|
||||
emits: ['upload'],
|
||||
computed: {
|
||||
inputId () {
|
||||
return 'el' + Math.random().toString(32).split('.')[1]
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
onDropFiles (e) {
|
||||
this.uploadFiles(e.dataTransfer.files)
|
||||
},
|
||||
onSelectFiles (e) {
|
||||
e.preventDefault()
|
||||
|
||||
this.uploadFiles(this.$refs.input.files).then(() => {
|
||||
this.$refs.input.value = ''
|
||||
})
|
||||
},
|
||||
async uploadFiles (files) {
|
||||
const blobs = await Promise.all(
|
||||
Array.from(files).map(async (file) => {
|
||||
const upload = new DirectUpload(
|
||||
file,
|
||||
'/direct_uploads',
|
||||
this.$refs.input
|
||||
)
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
upload.create((error, blob) => {
|
||||
if (error) {
|
||||
console.error(error)
|
||||
|
||||
return reject(error)
|
||||
} else {
|
||||
return resolve(blob)
|
||||
}
|
||||
})
|
||||
}).catch((error) => {
|
||||
console.error(error)
|
||||
})
|
||||
})
|
||||
)
|
||||
|
||||
return await Promise.all(
|
||||
blobs.map((blob) => {
|
||||
return fetch('/api/attachments', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
name: 'attachments',
|
||||
blob_signed_id: blob.signed_id,
|
||||
submission_slug: this.submissionSlug
|
||||
}),
|
||||
headers: { 'Content-Type': 'application/json' }
|
||||
}).then(resp => resp.json()).then((data) => {
|
||||
return data
|
||||
})
|
||||
})).then((result) => {
|
||||
this.$emit('upload', result)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
@@ -0,0 +1,269 @@
|
||||
<template>
|
||||
<FieldAreas
|
||||
ref="areas"
|
||||
:fields="fields"
|
||||
:values="values"
|
||||
:attachments-index="attachmentsIndex"
|
||||
@focus-field="goToField"
|
||||
/>
|
||||
<button
|
||||
v-if="currentStep !== 0"
|
||||
@click="goToField(fields[currentStep - 1], true)"
|
||||
>
|
||||
Back
|
||||
</button>
|
||||
{{ currentField.type }}
|
||||
<form
|
||||
v-if="!isCompleted"
|
||||
ref="form"
|
||||
:action="submitPath"
|
||||
method="post"
|
||||
@submit.prevent="submitStep"
|
||||
>
|
||||
<input
|
||||
type="hidden"
|
||||
name="authenticity_token"
|
||||
:value="authenticityToken"
|
||||
>
|
||||
<input
|
||||
v-if="currentStep === fields.length - 1"
|
||||
type="hidden"
|
||||
name="completed"
|
||||
value="true"
|
||||
>
|
||||
<input
|
||||
value="put"
|
||||
name="_method"
|
||||
type="hidden"
|
||||
>
|
||||
<div>
|
||||
<template v-if="currentField.type === 'text'">
|
||||
<label :for="currentField.uuid">{{ currentField.name || 'Text' }}</label>
|
||||
<div>
|
||||
<input
|
||||
:id="currentField.uuid"
|
||||
v-model="values[currentField.uuid]"
|
||||
autofocus
|
||||
class="text-xl"
|
||||
:required="currentField.required"
|
||||
type="text"
|
||||
:name="`values[${currentField.uuid}]`"
|
||||
>
|
||||
</div>
|
||||
</template>
|
||||
<template v-else-if="currentField.type === 'date'">
|
||||
<label :for="currentField.uuid">{{ currentField.name || 'Date' }}</label>
|
||||
<div>
|
||||
<input
|
||||
:id="currentField.uuid"
|
||||
v-model="values[currentField.uuid]"
|
||||
class="text-xl"
|
||||
autofocus
|
||||
:required="currentField.required"
|
||||
type="date"
|
||||
:name="`values[${currentField.uuid}]`"
|
||||
>
|
||||
</div>
|
||||
</template>
|
||||
<template v-else-if="currentField.type === 'select'">
|
||||
<label :for="currentField.uuid">{{ currentField.name || 'Date' }}</label>
|
||||
<select
|
||||
:id="currentField.uuid"
|
||||
v-model="values[currentField.uuid]"
|
||||
:required="currentField.required"
|
||||
:name="`values[${currentField.uuid}]`"
|
||||
>
|
||||
<option
|
||||
value=""
|
||||
disabled
|
||||
:selected="!values[currentField.uuid]"
|
||||
>
|
||||
Select your option
|
||||
</option>
|
||||
<option
|
||||
v-for="(option, index) in currentField.options"
|
||||
:key="index"
|
||||
:select="values[currentField.uuid] == option"
|
||||
:value="option"
|
||||
>
|
||||
{{ option }}
|
||||
</option>
|
||||
</select>
|
||||
</template>
|
||||
<template v-else-if="currentField.type === 'radio'">
|
||||
<div
|
||||
v-for="(option, index) in currentField.options"
|
||||
:key="index"
|
||||
>
|
||||
<label :for="currentField.uuid + option">
|
||||
<input
|
||||
:id="currentField.uuid + option"
|
||||
v-model="values[currentField.uuid]"
|
||||
type="radio"
|
||||
:name="`values[${currentField.uuid}]`"
|
||||
:value="option"
|
||||
>
|
||||
{{ option }}
|
||||
</label>
|
||||
</div>
|
||||
</template>
|
||||
<CheckboxStep
|
||||
v-else-if="currentField.type === 'checkbox'"
|
||||
v-model="values[currentField.uuid]"
|
||||
:field="currentField"
|
||||
/>
|
||||
<ImageStep
|
||||
v-else-if="currentField.type === 'image'"
|
||||
v-model="values[currentField.uuid]"
|
||||
:field="currentField"
|
||||
:attachments-index="attachmentsIndex"
|
||||
:submission-slug="submissionSlug"
|
||||
@attached="attachments.push($event)"
|
||||
/>
|
||||
<SignatureStep
|
||||
v-else-if="currentField.type === 'signature'"
|
||||
ref="currentStep"
|
||||
v-model="values[currentField.uuid]"
|
||||
:field="currentField"
|
||||
:attachments-index="attachmentsIndex"
|
||||
:submission-slug="submissionSlug"
|
||||
@attached="attachments.push($event)"
|
||||
/>
|
||||
<AttachmentStep
|
||||
v-else-if="currentField.type === 'attachment'"
|
||||
v-model="values[currentField.uuid]"
|
||||
:field="currentField"
|
||||
:attachments-index="attachmentsIndex"
|
||||
:submission-slug="submissionSlug"
|
||||
@attached="attachments.push($event)"
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<button type="submit">
|
||||
<span v-if="isSubmitting">
|
||||
Submitting...
|
||||
</span>
|
||||
<span v-else>
|
||||
Submit
|
||||
</span>
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
<FormCompleted
|
||||
v-else
|
||||
:submission-slug="submissionSlug"
|
||||
/>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import FieldAreas from './areas'
|
||||
import ImageStep from './image_step'
|
||||
import SignatureStep from './signature_step'
|
||||
import AttachmentStep from './attachment_step'
|
||||
import CheckboxStep from './checkbox_step'
|
||||
import FormCompleted from './completed'
|
||||
|
||||
export default {
|
||||
name: 'FlowForm',
|
||||
components: {
|
||||
FieldAreas,
|
||||
ImageStep,
|
||||
SignatureStep,
|
||||
AttachmentStep,
|
||||
CheckboxStep,
|
||||
FormCompleted
|
||||
},
|
||||
props: {
|
||||
submissionSlug: {
|
||||
type: String,
|
||||
required: true
|
||||
},
|
||||
attachments: {
|
||||
type: Array,
|
||||
required: false,
|
||||
default: () => []
|
||||
},
|
||||
fields: {
|
||||
type: Array,
|
||||
required: false,
|
||||
default: () => []
|
||||
},
|
||||
authenticityToken: {
|
||||
type: String,
|
||||
required: true
|
||||
},
|
||||
values: {
|
||||
type: Object,
|
||||
required: false,
|
||||
default: () => ({})
|
||||
}
|
||||
},
|
||||
data () {
|
||||
return {
|
||||
isCompleted: false,
|
||||
currentStep: 0,
|
||||
isSubmitting: false
|
||||
}
|
||||
},
|
||||
computed: {
|
||||
currentField () {
|
||||
return this.fields[this.currentStep]
|
||||
},
|
||||
attachmentsIndex () {
|
||||
return this.attachments.reduce((acc, a) => {
|
||||
acc[a.uuid] = a
|
||||
|
||||
return acc
|
||||
}, {})
|
||||
},
|
||||
submitPath () {
|
||||
return `/l/${this.submissionSlug}`
|
||||
}
|
||||
},
|
||||
mounted () {
|
||||
this.currentStep = Math.min(
|
||||
this.fields.indexOf([...this.fields].reverse().find((field) => !!this.values[field.uuid])) + 1,
|
||||
this.fields.length - 1
|
||||
)
|
||||
},
|
||||
methods: {
|
||||
goToField (field, scrollToArea = false) {
|
||||
this.currentStep = this.fields.indexOf(field)
|
||||
|
||||
this.$nextTick(() => {
|
||||
if (scrollToArea) {
|
||||
this.$refs.areas.scrollIntoField(field)
|
||||
}
|
||||
|
||||
this.$refs.form.querySelector('input[type="date"], input[type="text"], select')?.focus()
|
||||
})
|
||||
},
|
||||
async submitStep () {
|
||||
this.isSubmitting = true
|
||||
|
||||
const stepPromise = this.currentField.type === 'signature'
|
||||
? this.$refs.currentStep.submit
|
||||
: () => Promise.resolve({})
|
||||
|
||||
await stepPromise()
|
||||
|
||||
return fetch(this.submitPath, {
|
||||
method: 'POST',
|
||||
body: new FormData(this.$refs.form)
|
||||
}).then(response => {
|
||||
const nextField = this.fields[this.currentStep + 1]
|
||||
|
||||
if (nextField) {
|
||||
this.goToField(this.fields[this.currentStep + 1], true)
|
||||
} else {
|
||||
this.isCompleted = true
|
||||
}
|
||||
}).catch(error => {
|
||||
console.error('Error submitting form:', error)
|
||||
}).finally(() => {
|
||||
this.isSubmitting = false
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
@@ -0,0 +1,67 @@
|
||||
<template>
|
||||
<div>
|
||||
<img
|
||||
v-if="modelValue"
|
||||
class="w-80"
|
||||
:src="attachmentsIndex[modelValue].url"
|
||||
>
|
||||
<button
|
||||
v-if="modelValue"
|
||||
@click.prevent="remove"
|
||||
>
|
||||
Remove
|
||||
</button>
|
||||
<input
|
||||
:value="modelValue"
|
||||
type="hidden"
|
||||
:name="`values[${field.uuid}]`"
|
||||
>
|
||||
<FileDropzone
|
||||
:message="'Image'"
|
||||
:submission-slug="submissionSlug"
|
||||
:accept="'image/*'"
|
||||
@upload="onImageUpload"
|
||||
/>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import FileDropzone from './dropzone'
|
||||
export default {
|
||||
name: 'ImageStep',
|
||||
components: {
|
||||
FileDropzone
|
||||
},
|
||||
props: {
|
||||
field: {
|
||||
type: Object,
|
||||
required: true
|
||||
},
|
||||
submissionSlug: {
|
||||
type: String,
|
||||
required: true
|
||||
},
|
||||
attachmentsIndex: {
|
||||
type: Object,
|
||||
required: false,
|
||||
default: () => ({})
|
||||
},
|
||||
modelValue: {
|
||||
type: String,
|
||||
required: false,
|
||||
default: ''
|
||||
}
|
||||
},
|
||||
emits: ['attached', 'update:model-value'],
|
||||
methods: {
|
||||
remove () {
|
||||
this.$emit('update:model-value', '')
|
||||
},
|
||||
onImageUpload (attachments) {
|
||||
this.$emit('attached', attachments[0])
|
||||
|
||||
this.$emit('update:model-value', attachments[0].uuid)
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
@@ -0,0 +1,101 @@
|
||||
<template>
|
||||
<div>
|
||||
<input
|
||||
:value="modelValue"
|
||||
type="hidden"
|
||||
:name="`values[${field.uuid}]`"
|
||||
>
|
||||
<img
|
||||
v-if="modelValue"
|
||||
:src="attachmentsIndex[modelValue].url"
|
||||
>
|
||||
<canvas
|
||||
v-show="!modelValue"
|
||||
ref="canvas"
|
||||
/>
|
||||
<button
|
||||
v-if="modelValue"
|
||||
@click.prevent="remove"
|
||||
>
|
||||
Redraw
|
||||
</button>
|
||||
<button
|
||||
v-else
|
||||
@click.prevent="clear"
|
||||
>
|
||||
Clear
|
||||
</button>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import SignaturePad from 'signature_pad'
|
||||
import { DirectUpload } from '@rails/activestorage'
|
||||
|
||||
export default {
|
||||
name: 'SignatureStep',
|
||||
props: {
|
||||
field: {
|
||||
type: Object,
|
||||
required: true
|
||||
},
|
||||
submissionSlug: {
|
||||
type: String,
|
||||
required: true
|
||||
},
|
||||
attachmentsIndex: {
|
||||
type: Object,
|
||||
required: false,
|
||||
default: () => ({})
|
||||
},
|
||||
modelValue: {
|
||||
type: String,
|
||||
required: false,
|
||||
default: ''
|
||||
}
|
||||
},
|
||||
emits: ['attached', 'update:model-value'],
|
||||
mounted () {
|
||||
this.pad = new SignaturePad(this.$refs.canvas)
|
||||
},
|
||||
methods: {
|
||||
remove () {
|
||||
this.$emit('update:model-value', '')
|
||||
},
|
||||
clear () {
|
||||
this.pad.clear()
|
||||
},
|
||||
submit () {
|
||||
if (this.modelValue) {
|
||||
return Promise.resolve({})
|
||||
}
|
||||
|
||||
return new Promise((resolve) => {
|
||||
this.$refs.canvas.toBlob((blob) => {
|
||||
const file = new File([blob], 'signature.png', { type: 'image/png' })
|
||||
|
||||
new DirectUpload(
|
||||
file,
|
||||
'/direct_uploads'
|
||||
).create((_error, data) => {
|
||||
fetch('/api/attachments', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
submission_slug: this.submissionSlug,
|
||||
blob_signed_id: data.signed_id,
|
||||
name: 'signatures'
|
||||
}),
|
||||
headers: { 'Content-Type': 'application/json' }
|
||||
}).then((resp) => resp.json()).then((attachment) => {
|
||||
this.$emit('update:model-value', attachment.uuid)
|
||||
this.$emit('attached', attachment)
|
||||
|
||||
return resolve(attachment)
|
||||
})
|
||||
})
|
||||
}, 'image/png')
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
Reference in New Issue
Block a user