Files
docuseal/app/javascript/submission_form/form.vue
T

718 lines
22 KiB
Vue
Raw Normal View History

2023-05-25 22:47:52 +03:00
<template>
<FieldAreas
ref="areas"
2023-06-16 00:04:05 +03:00
:steps="stepFields"
2023-05-25 22:47:52 +03:00
:values="values"
:attachments-index="attachmentsIndex"
2023-07-31 00:48:51 +03:00
:with-label="!isAnonymousChecboxes"
2023-06-16 00:04:05 +03:00
:current-step="currentStepFields"
2023-07-22 14:31:13 +03:00
@focus-step="[saveStep(), goToStep($event, false, true), currentField.type !== 'checkbox' ? isFormVisible = true : '']"
2023-05-25 22:47:52 +03:00
/>
2023-07-17 22:48:01 +03:00
<button
v-if="!isFormVisible"
2023-08-12 12:34:02 +03:00
class="btn btn-neutral flex text-white absolute rounded-none border-x-0 md:border md:rounded-full bottom-0 w-full md:mb-4 text-base"
2023-07-17 22:48:01 +03:00
@click.prevent="isFormVisible = true"
>
2023-08-30 02:00:55 +03:00
{{ t('submit_form') }}
2023-07-17 22:48:01 +03:00
<IconArrowsDiagonal
class="absolute right-0 mr-4"
:width="20"
:height="20"
/>
</button>
<div
2023-07-22 14:31:13 +03:00
v-show="isFormVisible"
2023-07-17 22:48:01 +03:00
id="form_container"
class="shadow-md bg-base-100 absolute bottom-0 md:bottom-4 w-full border-base-200 border p-4 rounded"
2023-08-28 23:43:33 +03:00
:style="{ backgroundColor: backgroundColor }"
2023-07-17 22:48:01 +03:00
>
<button
2023-06-16 23:39:55 +03:00
v-if="!isCompleted"
2023-07-17 22:48:01 +03:00
class="absolute right-0 mr-2 mt-2 top-0 hidden md:block"
title="Minimize"
@click.prevent="isFormVisible = false"
2023-05-25 22:47:52 +03:00
>
2023-07-17 22:48:01 +03:00
<IconArrowsDiagonalMinimize2
:width="20"
:height="20"
/>
</button>
<div>
<form
v-if="!isCompleted"
ref="form"
:action="submitPath"
method="post"
class="md:mx-16"
@submit.prevent="submitStep"
2023-06-16 23:39:55 +03:00
>
2023-07-17 22:48:01 +03:00
<input
type="hidden"
name="authenticity_token"
:value="authenticityToken"
>
<input
value="put"
name="_method"
type="hidden"
>
<div class="md:mt-4">
<div v-if="['cells', 'text'].includes(currentField.type)">
2023-10-16 00:41:14 +03:00
<TextStep
:key="currentField.uuid"
v-model="values[currentField.uuid]"
:field="currentField"
@focus="$refs.areas.scrollIntoField(currentField)"
2023-07-17 22:48:01 +03:00
/>
2023-06-16 23:39:55 +03:00
</div>
2023-10-26 21:26:08 +03:00
<DateStep
v-else-if="currentField.type === 'date'"
:key="currentField.uuid"
v-model="values[currentField.uuid]"
:field="currentField"
@focus="$refs.areas.scrollIntoField(currentField)"
/>
2023-07-17 22:48:01 +03:00
<div v-else-if="currentField.type === 'select'">
<label
v-if="currentField.name"
:for="currentField.uuid"
class="label text-2xl mb-2"
>{{ currentField.name }}
2023-08-30 02:00:55 +03:00
<template v-if="!currentField.required">({{ t('optional') }})</template>
2023-07-17 22:48:01 +03:00
</label>
<div
v-else
class="py-1"
/>
<select
2023-06-16 23:39:55 +03:00
:id="currentField.uuid"
:required="currentField.required"
2023-07-17 22:48:01 +03:00
class="select base-input !text-2xl w-full text-center font-normal"
2023-06-16 23:39:55 +03:00
:name="`values[${currentField.uuid}]`"
2023-07-17 22:48:01 +03:00
@change="values[currentField.uuid] = $event.target.value"
2023-06-16 23:39:55 +03:00
@focus="$refs.areas.scrollIntoField(currentField)"
>
2023-07-17 22:48:01 +03:00
<option
value=""
:selected="!values[currentField.uuid]"
>
2023-08-30 02:00:55 +03:00
{{ t('select_your_option') }}
2023-07-17 22:48:01 +03:00
</option>
<option
v-for="(option, index) in currentField.options"
:key="index"
:selected="values[currentField.uuid] == option"
:value="option"
>
{{ option }}
</option>
</select>
2023-06-16 23:39:55 +03:00
</div>
2023-07-17 22:48:01 +03:00
<div v-else-if="currentField.type === 'radio'">
<label
v-if="currentField.name"
:for="currentField.uuid"
class="label text-2xl mb-2"
>{{ currentField.name }}
2023-08-30 02:00:55 +03:00
<template v-if="!currentField.required">({{ t('optional') }})</template>
2023-07-17 22:48:01 +03:00
</label>
<div class="flex w-full">
<div class="space-y-3.5 mx-auto">
<div
v-for="(option, index) in currentField.options"
:key="index"
>
<label
:for="currentField.uuid + option"
class="flex items-center space-x-3"
>
<input
:id="currentField.uuid + option"
v-model="values[currentField.uuid]"
type="radio"
class="base-radio !h-7 !w-7"
:name="`values[${currentField.uuid}]`"
:value="option"
:required="currentField.required"
>
<span class="text-xl">
{{ option }}
</span>
</label>
</div>
</div>
</div>
</div>
<MultiSelectStep
v-else-if="currentField.type === 'multiple'"
2023-10-16 00:51:08 +03:00
:key="currentField.uuid"
2023-07-17 22:48:01 +03:00
v-model="values[currentField.uuid]"
:field="currentField"
2023-06-30 01:26:13 +03:00
/>
2023-07-17 22:48:01 +03:00
<div
v-else-if="currentField.type === 'checkbox'"
2023-07-31 00:48:51 +03:00
class="flex w-full max-h-44 overflow-y-auto"
2023-05-25 22:47:52 +03:00
>
2023-07-17 22:48:01 +03:00
<input
type="hidden"
name="cast_boolean"
value="true"
2023-06-16 23:39:55 +03:00
>
2023-07-17 22:48:01 +03:00
<div
class="space-y-3.5 mx-auto"
2023-06-16 23:39:55 +03:00
>
2023-07-31 00:48:51 +03:00
<template v-if="isAnonymousChecboxes">
2023-08-12 12:34:02 +03:00
<span class="text-xl">
2023-08-30 02:00:55 +03:00
{{ t('complete_hightlighted_checkboxes_and_click') }} <span class="font-semibold">{{ stepFields.length === currentStep + 1 ? t('submit') : t('next') }}</span>.
2023-08-12 12:34:02 +03:00
</span>
2023-08-09 21:10:07 +03:00
<input
v-for="field in currentStepFields"
:key="field.uuid"
type="hidden"
:name="`values[${field.uuid}]`"
:value="!!values[field.uuid]"
>
2023-07-31 00:48:51 +03:00
</template>
<template v-else>
<div
v-for="(field, index) in currentStepFields"
:key="field.uuid"
2023-06-16 23:39:55 +03:00
>
2023-07-31 00:48:51 +03:00
<label
:for="field.uuid"
class="flex items-center space-x-3"
2023-06-16 23:39:55 +03:00
>
2023-07-31 00:48:51 +03:00
<input
type="hidden"
:name="`values[${field.uuid}]`"
:value="!!values[field.uuid]"
>
<input
:id="field.uuid"
type="checkbox"
class="base-checkbox !h-7 !w-7"
2023-10-07 09:04:02 +03:00
:oninvalid="`this.setCustomValidity('${t('please_check_the_box_to_continue')}')`"
:onchange="`this.setCustomValidity(validity.valueMissing ? '${t('please_check_the_box_to_continue')}' : '');`"
:required="field.required"
2023-07-31 00:48:51 +03:00
:checked="!!values[field.uuid]"
@click="[$refs.areas.scrollIntoField(field), values[field.uuid] = !values[field.uuid]]"
>
<span class="text-xl">
{{ field.name || field.type + ' ' + (index + 1) }}
</span>
</label>
</div>
</template>
2023-06-16 23:39:55 +03:00
</div>
</div>
2023-07-17 22:48:01 +03:00
<ImageStep
v-else-if="currentField.type === 'image'"
2023-10-16 00:51:08 +03:00
:key="currentField.uuid"
2023-07-17 22:48:01 +03:00
v-model="values[currentField.uuid]"
:field="currentField"
:is-direct-upload="isDirectUpload"
:attachments-index="attachmentsIndex"
:submitter-slug="submitterSlug"
@attached="[attachments.push($event), $refs.areas.scrollIntoField(currentField)]"
/>
<SignatureStep
v-else-if="currentField.type === 'signature'"
ref="currentStep"
2023-10-16 00:51:08 +03:00
:key="currentField.uuid"
2023-07-17 22:48:01 +03:00
v-model="values[currentField.uuid]"
:field="currentField"
:previous-value="previousSignatureValue"
2023-07-17 22:48:01 +03:00
:is-direct-upload="isDirectUpload"
:attachments-index="attachmentsIndex"
:submitter-slug="submitterSlug"
@attached="attachments.push($event)"
@start="$refs.areas.scrollIntoField(currentField)"
@minimize="isFormVisible = false"
/>
2023-09-20 00:13:14 +03:00
<InitialsStep
v-else-if="currentField.type === 'initials'"
ref="currentStep"
2023-10-16 00:51:08 +03:00
:key="currentField.uuid"
2023-09-20 00:13:14 +03:00
v-model="values[currentField.uuid]"
:field="currentField"
:previous-value="previousInitialsValue"
2023-09-20 00:13:14 +03:00
:is-direct-upload="isDirectUpload"
:attachments-index="attachmentsIndex"
:submitter-slug="submitterSlug"
@attached="attachments.push($event)"
@start="$refs.areas.scrollIntoField(currentField)"
@focus="$refs.areas.scrollIntoField(currentField)"
@minimize="isFormVisible = false"
/>
2023-07-17 22:48:01 +03:00
<AttachmentStep
v-else-if="currentField.type === 'file'"
2023-10-16 00:51:08 +03:00
:key="currentField.uuid"
2023-07-17 22:48:01 +03:00
v-model="values[currentField.uuid]"
:is-direct-upload="isDirectUpload"
:field="currentField"
:attachments-index="attachmentsIndex"
:submitter-slug="submitterSlug"
@attached="[attachments.push($event), $refs.areas.scrollIntoField(currentField)]"
/>
2023-09-05 01:00:21 +03:00
<PhoneStep
v-else-if="currentField.type === 'phone'"
ref="currentStep"
2023-10-16 00:51:08 +03:00
:key="currentField.uuid"
2023-09-05 01:00:21 +03:00
v-model="values[currentField.uuid]"
:field="currentField"
:default-value="submitter.phone"
:submitter-slug="submitterSlug"
@focus="$refs.areas.scrollIntoField(currentField)"
@submit="submitStep"
/>
2023-06-16 23:39:55 +03:00
</div>
2023-07-17 22:48:01 +03:00
<div class="mt-6 md:mt-8">
<button
2023-10-15 21:41:47 +03:00
ref="submitButton"
2023-07-17 22:48:01 +03:00
type="submit"
class="base-button w-full flex justify-center"
:disabled="isButtonDisabled"
2023-05-25 22:47:52 +03:00
>
2023-07-17 22:48:01 +03:00
<span class="flex">
<IconInnerShadowTop
v-if="isSubmitting"
class="mr-1 animate-spin"
/>
<span v-if="stepFields.length === currentStep + 1">
2023-08-30 02:00:55 +03:00
{{ t('submit') }}
2023-07-17 22:48:01 +03:00
</span>
<span v-else>
2023-08-30 02:00:55 +03:00
{{ t('next') }}
2023-07-17 22:48:01 +03:00
</span><span
v-if="isSubmitting"
class="w-6 flex justify-start mr-1"
><span>...</span></span>
2023-06-20 00:24:39 +03:00
</span>
2023-07-17 22:48:01 +03:00
</button>
2023-10-15 21:41:47 +03:00
<div
v-if="showFillAllRequiredFields"
class="text-center mt-1"
>
{{ t('please_fill_all_required_fields') }}
</div>
2023-07-17 22:48:01 +03:00
</div>
</form>
<FormCompleted
v-else
:is-demo="isDemo"
2023-09-03 20:44:54 +03:00
:attribution="attribution"
2023-09-29 01:24:39 +03:00
:completed-button="completedButton"
2023-08-12 12:34:02 +03:00
:with-confetti="withConfetti"
2023-09-05 01:00:21 +03:00
:can-send-email="canSendEmail && !!submitter.email"
2023-07-17 22:48:01 +03:00
:submitter-slug="submitterSlug"
/>
<div class="flex justify-center">
<div class="flex items-center mt-5 mb-1">
<a
v-for="(step, index) in stepFields"
:key="step[0].uuid"
href="#"
class="inline border border-base-300 h-3 w-3 rounded-full mx-1"
2023-10-15 21:41:47 +03:00
:class="{ 'bg-base-300': index === currentStep, 'bg-base-content': (index < currentStep && stepFields[index].every((f) => !f.required || ![null, undefined, ''].includes(values[f.uuid]))) || isCompleted, 'bg-white': index > currentStep }"
2023-07-17 22:48:01 +03:00
@click.prevent="isCompleted ? '' : [saveStep(), goToStep(step, true)]"
/>
</div>
2023-06-15 01:05:04 +03:00
</div>
</div>
2023-06-16 23:39:55 +03:00
</div>
2023-05-25 22:47:52 +03:00
</template>
<script>
import FieldAreas from './areas'
import ImageStep from './image_step'
import SignatureStep from './signature_step'
2023-09-20 00:13:14 +03:00
import InitialsStep from './initials_step'
2023-05-25 22:47:52 +03:00
import AttachmentStep from './attachment_step'
2023-06-16 00:04:05 +03:00
import MultiSelectStep from './multi_select_step'
2023-09-05 01:00:21 +03:00
import PhoneStep from './phone_step'
2023-10-16 00:41:14 +03:00
import TextStep from './text_step'
2023-10-26 21:26:08 +03:00
import DateStep from './date_step'
2023-05-25 22:47:52 +03:00
import FormCompleted from './completed'
2023-10-26 21:26:08 +03:00
import { IconInnerShadowTop, IconArrowsDiagonal, IconArrowsDiagonalMinimize2 } from '@tabler/icons-vue'
2023-08-30 02:00:55 +03:00
import { t } from './i18n'
2023-05-25 22:47:52 +03:00
export default {
2023-05-31 23:19:20 +03:00
name: 'SubmissionForm',
2023-05-25 22:47:52 +03:00
components: {
FieldAreas,
ImageStep,
SignatureStep,
AttachmentStep,
2023-09-20 00:13:14 +03:00
InitialsStep,
2023-06-16 00:04:05 +03:00
MultiSelectStep,
2023-06-20 00:24:39 +03:00
IconInnerShadowTop,
2023-10-26 21:26:08 +03:00
DateStep,
2023-07-17 22:48:01 +03:00
IconArrowsDiagonal,
2023-10-16 00:41:14 +03:00
TextStep,
2023-09-05 01:00:21 +03:00
PhoneStep,
2023-07-17 22:48:01 +03:00
IconArrowsDiagonalMinimize2,
2023-05-25 22:47:52 +03:00
FormCompleted
},
2023-08-12 12:34:02 +03:00
provide () {
return {
2023-08-30 02:00:55 +03:00
baseUrl: this.baseUrl,
t: this.t
2023-08-12 12:34:02 +03:00
}
},
2023-05-25 22:47:52 +03:00
props: {
2023-09-03 14:05:31 +03:00
submitter: {
type: Object,
2023-06-13 00:29:07 +03:00
required: true
},
2023-06-30 20:31:17 +03:00
canSendEmail: {
type: Boolean,
required: false,
default: false
},
2023-05-25 22:47:52 +03:00
attachments: {
type: Array,
required: false,
default: () => []
},
2023-10-25 02:06:47 +03:00
onComplete: {
type: Function,
required: false,
default () {
return () => {}
}
},
2023-08-12 12:34:02 +03:00
withConfetti: {
type: Boolean,
required: false,
default: false
},
baseUrl: {
type: String,
required: false,
default: ''
},
2023-05-25 22:47:52 +03:00
fields: {
type: Array,
required: false,
default: () => []
},
authenticityToken: {
type: String,
2023-08-12 12:34:02 +03:00
required: false,
default: ''
2023-05-25 22:47:52 +03:00
},
2023-08-28 23:43:33 +03:00
backgroundColor: {
type: String,
required: false,
default: ''
},
isDirectUpload: {
type: Boolean,
2023-08-12 12:34:02 +03:00
required: false,
default: false
},
2023-10-07 10:23:11 +03:00
allowToSkip: {
type: Boolean,
required: false,
2023-10-21 12:09:50 +03:00
default: false
2023-10-07 10:23:11 +03:00
},
2023-09-22 21:50:39 +03:00
goToLast: {
type: Boolean,
required: false,
default: true
},
2023-07-12 01:24:25 +03:00
isDemo: {
type: Boolean,
2023-08-12 12:34:02 +03:00
required: false,
2023-07-12 01:24:25 +03:00
default: false
},
2023-09-03 20:44:54 +03:00
attribution: {
type: Boolean,
required: false,
default: true
},
2023-05-25 22:47:52 +03:00
values: {
type: Object,
required: false,
default: () => ({})
2023-09-29 01:24:39 +03:00
},
completedButton: {
type: Object,
required: false,
default: () => ({})
2023-05-25 22:47:52 +03:00
}
},
data () {
return {
isCompleted: false,
2023-07-17 22:48:01 +03:00
isFormVisible: true,
2023-10-15 21:41:47 +03:00
showFillAllRequiredFields: false,
2023-05-25 22:47:52 +03:00
currentStep: 0,
2023-09-05 01:35:48 +03:00
isSubmitting: false,
2023-09-27 23:28:33 +03:00
submittedValues: {},
2023-09-05 01:35:48 +03:00
recalculateButtonDisabledKey: ''
2023-05-25 22:47:52 +03:00
}
},
computed: {
2023-06-16 00:04:05 +03:00
currentStepFields () {
return this.stepFields[this.currentStep]
},
2023-09-03 14:05:31 +03:00
submitterSlug () {
return this.submitter.slug
},
previousSignatureValue () {
const signatureField = [...this.fields].reverse().find((field) => field.type === 'signature' && !!this.values[field.uuid])
return this.values[signatureField?.uuid]
},
previousInitialsValue () {
const initialsField = [...this.fields].reverse().find((field) => field.type === 'initials' && !!this.values[field.uuid])
return this.values[initialsField?.uuid]
},
2023-07-31 00:48:51 +03:00
isAnonymousChecboxes () {
return this.currentField.type === 'checkbox' && this.currentStepFields.every((e) => !e.name) && this.currentStepFields.length > 4
},
2023-06-24 17:15:36 +03:00
isButtonDisabled () {
2023-09-05 01:35:48 +03:00
if (this.recalculateButtonDisabledKey) {
return this.isSubmitting ||
2023-06-24 17:15:36 +03:00
(this.currentField.required && ['image', 'file'].includes(this.currentField.type) && !this.values[this.currentField.uuid]?.length) ||
2023-09-20 00:13:14 +03:00
(this.currentField.required && this.currentField.type === 'signature' && !this.values[this.currentField.uuid]?.length && this.$refs.currentStep && !this.$refs.currentStep.isSignatureStarted) ||
(this.currentField.required && this.currentField.type === 'initials' && !this.values[this.currentField.uuid]?.length && this.$refs.currentStep && !this.$refs.currentStep.isInitialsStarted)
2023-09-05 01:35:48 +03:00
} else {
return false
}
2023-06-24 17:15:36 +03:00
},
2023-05-25 22:47:52 +03:00
currentField () {
2023-06-16 00:04:05 +03:00
return this.currentStepFields[0]
2023-06-13 00:29:07 +03:00
},
2023-06-16 00:04:05 +03:00
stepFields () {
2023-10-12 00:06:33 +03:00
return this.fields.filter((f) => !f.readonly).reduce((acc, f) => {
2023-06-16 00:04:05 +03:00
const prevStep = acc[acc.length - 1]
if (f.type === 'checkbox' && Array.isArray(prevStep) && prevStep[0].type === 'checkbox') {
prevStep.push(f)
} else {
acc.push([f])
}
return acc
}, [])
},
2023-05-25 22:47:52 +03:00
attachmentsIndex () {
return this.attachments.reduce((acc, a) => {
acc[a.uuid] = a
return acc
}, {})
},
submitPath () {
2023-06-13 00:29:07 +03:00
return `/s/${this.submitterSlug}`
2023-05-25 22:47:52 +03:00
}
},
mounted () {
2023-09-27 23:28:33 +03:00
this.submittedValues = JSON.parse(JSON.stringify(this.values))
2023-11-05 01:41:54 +02:00
this.fields.forEach((field) => {
if (field.default_value && !field.readonly) {
this.values[field.uuid] = field.default_value
}
})
2023-09-22 21:50:39 +03:00
if (this.goToLast) {
2023-11-05 01:41:54 +02:00
const requiredEmptyStepIndex = this.stepFields.indexOf(this.stepFields.find((fields) => fields.some((f) => f.required && !this.submittedValues[f.uuid])))
const lastFilledStepIndex = this.stepFields.indexOf([...this.stepFields].reverse().find((fields) => fields.some((f) => !!this.submittedValues[f.uuid]))) + 1
2023-10-15 21:41:47 +03:00
const indexesList = [this.stepFields.length - 1]
if (requiredEmptyStepIndex !== -1) {
indexesList.push(requiredEmptyStepIndex)
}
if (lastFilledStepIndex !== -1) {
indexesList.push(lastFilledStepIndex)
}
this.currentStep = Math.min(...indexesList)
2023-09-22 21:50:39 +03:00
}
2023-07-03 00:03:29 +03:00
if (/iPhone|iPad|iPod/i.test(navigator.userAgent)) {
2023-08-12 12:34:02 +03:00
this.$nextTick(() => {
const root = this.$root.$el.parentNode.getRootNode()
const scrollbox = root.getElementById('scrollbox')
const parent = root.body || root.querySelector('div')
2023-07-03 00:03:29 +03:00
2023-08-12 12:34:02 +03:00
parent.style.overflow = 'hidden'
scrollbox.classList.add('h-full', 'overflow-y-auto')
scrollbox.parentNode.classList.add('h-screen', 'overflow-y-auto')
scrollbox.parentNode.style.maxHeight = '-webkit-fill-available'
})
2023-07-03 00:03:29 +03:00
}
2023-09-05 01:35:48 +03:00
this.$nextTick(() => {
this.recalculateButtonDisabledKey = Math.random()
2023-09-10 12:55:41 +03:00
2023-09-27 23:28:33 +03:00
Promise.all([
this.maybeTrackEmailClick(),
this.maybeTrackSmsClick()
]).finally(() => {
2023-09-10 20:29:34 +03:00
this.trackViewForm()
})
2023-09-05 01:35:48 +03:00
})
2023-05-25 22:47:52 +03:00
},
methods: {
2023-08-30 02:00:55 +03:00
t,
2023-09-10 12:55:41 +03:00
maybeTrackEmailClick () {
const queryParams = new URLSearchParams(window.location.search)
if (queryParams.has('t')) {
2023-09-10 20:29:34 +03:00
const t = queryParams.get('t')
queryParams.delete('t')
const newUrl = [window.location.pathname, queryParams.toString()].filter(Boolean).join('?')
window.history.replaceState({}, document.title, newUrl)
return fetch(this.baseUrl + '/api/submitter_email_clicks', {
2023-09-10 12:55:41 +03:00
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
2023-09-10 20:29:34 +03:00
t,
2023-09-10 12:55:41 +03:00
submitter_slug: this.submitterSlug
})
})
2023-09-10 20:29:34 +03:00
} else {
return Promise.resolve({})
2023-09-10 12:55:41 +03:00
}
},
2023-09-27 23:28:33 +03:00
maybeTrackSmsClick () {
const queryParams = new URLSearchParams(window.location.search)
if (queryParams.has('c')) {
const c = queryParams.get('c')
queryParams.delete('c')
const newUrl = [window.location.pathname, queryParams.toString()].filter(Boolean).join('?')
window.history.replaceState({}, document.title, newUrl)
return fetch(this.baseUrl + '/api/submitter_sms_clicks', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
c,
submitter_slug: this.submitterSlug
})
})
} else {
return Promise.resolve({})
}
},
2023-09-10 12:55:41 +03:00
trackViewForm () {
fetch(this.baseUrl + '/api/submitter_form_views', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
submitter_slug: this.submitterSlug
})
})
},
2023-06-16 00:04:05 +03:00
goToStep (step, scrollToArea = false, clickUpload = false) {
this.currentStep = this.stepFields.indexOf(step)
2023-10-15 21:41:47 +03:00
this.showFillAllRequiredFields = false
2023-05-25 22:47:52 +03:00
this.$nextTick(() => {
2023-09-05 01:35:48 +03:00
this.recalculateButtonDisabledKey = Math.random()
2023-05-25 22:47:52 +03:00
if (scrollToArea) {
2023-06-16 00:04:05 +03:00
this.$refs.areas.scrollIntoField(step[0])
2023-05-25 22:47:52 +03:00
}
this.$refs.form.querySelector('input[type="date"], input[type="text"], select')?.focus()
2023-06-15 01:05:04 +03:00
2023-07-22 14:31:13 +03:00
if (clickUpload && !this.values[this.currentField.uuid] && ['file', 'image'].includes(this.currentField.type)) {
2023-06-15 01:05:04 +03:00
this.$refs.form.querySelector('input[type="file"]')?.click()
}
2023-05-25 22:47:52 +03:00
})
},
2023-07-08 20:51:37 +03:00
saveStep (formData) {
2023-06-27 00:57:29 +03:00
if (this.isCompleted) {
return Promise.resolve({})
} else {
2023-08-12 12:34:02 +03:00
return fetch(this.baseUrl + this.submitPath, {
2023-06-27 00:57:29 +03:00
method: 'POST',
2023-07-08 20:51:37 +03:00
body: formData || new FormData(this.$refs.form)
2023-06-27 00:57:29 +03:00
})
}
2023-06-20 00:24:39 +03:00
},
2023-05-25 22:47:52 +03:00
async submitStep () {
this.isSubmitting = true
2023-09-20 00:13:14 +03:00
const stepPromise = ['signature', 'phone', 'initials'].includes(this.currentField.type)
2023-05-25 22:47:52 +03:00
? this.$refs.currentStep.submit
: () => Promise.resolve({})
2023-09-06 22:21:15 +03:00
stepPromise().then(async () => {
2023-09-27 23:28:33 +03:00
const emptyRequiredField = this.stepFields.find((fields, index) => {
2023-10-21 12:09:50 +03:00
return index < this.currentStep && fields[0].required && (fields[0].type === 'phone' || !this.allowToSkip) && !this.submittedValues[fields[0].uuid]
2023-09-27 23:28:33 +03:00
})
2023-09-05 01:00:21 +03:00
const formData = new FormData(this.$refs.form)
2023-10-15 21:41:47 +03:00
const isLastStep = this.currentStep === this.stepFields.length - 1
2023-09-05 01:00:21 +03:00
2023-10-15 21:41:47 +03:00
if (isLastStep && !emptyRequiredField) {
2023-09-05 01:00:21 +03:00
formData.append('completed', 'true')
}
2023-05-25 22:47:52 +03:00
2023-09-06 22:21:15 +03:00
await this.saveStep(formData).then(async (response) => {
2023-09-05 01:00:21 +03:00
if (response.status === 422) {
const data = await response.json()
2023-07-08 20:51:37 +03:00
2023-09-05 01:00:21 +03:00
alert(data.error || 'Value is invalid')
2023-07-08 20:51:37 +03:00
2023-09-05 01:00:21 +03:00
return Promise.reject(new Error(data.error))
}
2023-05-25 22:47:52 +03:00
2023-09-27 23:28:33 +03:00
this.submittedValues[this.currentField.uuid] = this.values[this.currentField.uuid]
2023-10-15 21:41:47 +03:00
if (isLastStep) {
this.isSecondWalkthrough = true
}
const nextStep = (isLastStep && emptyRequiredField) || this.stepFields[this.currentStep + 1]
2023-09-05 01:00:21 +03:00
if (nextStep) {
2023-09-27 23:28:33 +03:00
this.goToStep(nextStep, true)
2023-10-15 21:41:47 +03:00
if (emptyRequiredField === nextStep) {
this.showFillAllRequiredFields = true
}
2023-09-05 01:00:21 +03:00
} else {
this.isCompleted = true
2023-10-25 02:06:47 +03:00
const respData = await response.text()
if (respData) {
this.onComplete(JSON.parse(respData))
}
2023-09-05 01:00:21 +03:00
}
}).catch(error => {
console.error('Error submitting form:', error)
}).finally(() => {
this.isSubmitting = false
})
2023-05-25 22:47:52 +03:00
}).catch(error => {
2023-09-05 01:00:21 +03:00
console.log(error)
2023-05-25 22:47:52 +03:00
}).finally(() => {
this.isSubmitting = false
})
}
}
}
</script>