-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
FormieForm.vue
179 lines (144 loc) · 5.75 KB
/
FormieForm.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<template>
<div class="shadow rounded-md bg-white overflow-hidden">
<form v-if="form" ref="form" novalidate @submit.prevent="onSubmit">
<alert v-model="alertState" :success-text="form.settings.submitActionMessageHtml" :error-text="form.settings.errorMessageHtml" />
<div v-if="form.settings.displayFormTitle" class="text-center">
<h2 class="text-xl leading-6 font-semibold text-gray-900 my-4 sm:my-6">
{{ form.title }}
</h2>
<hr>
</div>
<div v-if="form.settings.displayPageTabs" class="px-4 sm:px-6 border-b border-gray-200">
<nav class="-mb-px flex space-x-8" aria-label="Tabs">
<a v-for="(page, index) in form.pages" :key="index" href="#" :class="index == activePage ? 'border-indigo-500 text-indigo-600 whitespace-nowrap py-4 px-1 border-b-2 font-medium text-sm' : 'border-transparent text-gray-500 hover:text-gray-700 hover:border-gray-300 whitespace-nowrap py-4 px-1 border-b-2 font-medium text-sm'" @click.prevent="onTabClick(index)">
{{ page.name }}
</a>
</nav>
</div>
<div v-if="form.settings.displayPageProgress" class="mt-4 px-4 sm:px-6">
<div class="flex h-5 overflow-hidden text-xs font-medium text-white rounded-md bg-gray-200">
<div class="flex flex-col justify-center text-center bg-indigo-500" :style="'width: ' + stepPercentage + '%'" role="progressbar" :aria-valuenow="stepPercentage" aria-valuemin="0" aria-valuemax="100">
<span class="">{{ stepPercentage }}%</span>
</div>
</div>
</div>
<formie-page
v-for="(page, index) in form.pages" :key="index"
:ref="'page-' + index"
v-model="activePage"
:page="page"
:page-index="index"
:form="form"
:loading="loading"
/>
</form>
<div v-else-if="form === null" class="bg-white p-24 text-center text-red-500">
Unable to find form "{{ handle }}".
</div>
<div v-else class="bg-white p-24 text-center text-red-500">
<div v-if="error" class="text-red-500">{{ error }}</div>
<div v-else class="loading loading-lg"></div>
</div>
</div>
</template>
<script>
import Pristine from 'pristinejs';
import { FormQuery } from '@graphql/forms';
import { getFormMutation, getMutationVariables } from '@utils/mutations';
import { resetValidation, runValidation, applyServerValidation } from '@utils/validation';
import Alert from '@components/Alert.vue';
import FormiePage from '@components/FormiePage.vue';
export default {
components: {
Alert,
FormiePage,
},
props: {
handle: {
type: String,
required: true,
},
},
data() {
return {
form: false,
activePage: 0,
error: false,
loading: false,
alertState: null,
validator: null,
};
},
computed: {
stepPercentage() {
return parseInt(((this.activePage + 1) / this.form.pages.length) * 100, 10);
},
},
apollo: {
form: {
query: FormQuery,
variables() {
return { handle: this.handle };
},
error(error) {
this.error = error.message;
},
// Figure out why fragments don't work without this??
fetchPolicy: 'no-cache',
},
},
methods: {
onTabClick(index) {
this.activePage = index;
},
onSubmit(e) {
this.loading = true;
this.alertState = false;
// Always validate the current page only, not the entire form
const $form = this.$refs.form;
const $page = this.$refs[`page-${this.activePage}`].$el;
// Setup a validator
this.validator = new Pristine($form);
// Clear out validation errors
resetValidation($form);
const isLastPage = this.activePage === this.form.pages.length - 1;
const valid = runValidation(this.validator, $page);
// Validate the form - for each page
if (!valid) {
this.loading = false;
this.alertState = 'error';
return;
}
// Are we on the last page, or on a multi-step page?
if (!isLastPage) {
this.loading = false;
return this.activePage += 1;
}
// Prepare an object from FormData
const formData = getMutationVariables(this.form, $form);
// Generate a mutation string, with all the input types sorted
const formMutation = getFormMutation(this.form);
this.$apollo.mutate({
mutation: formMutation,
variables: formData,
}).then(({ data }) => {
this.loading = false;
const response = data[`save_${this.handle}_Submission`];
this.onSuccess(response);
}).catch((error) => {
this.loading = false;
this.alertState = 'error';
// Apply server-side errors
applyServerValidation(this.validator, $form, error);
console.error(error);
});
},
onSuccess(response) {
if (this.form.settings.submitAction === 'message') {
this.alertState = 'success';
}
this.$refs.form.reset();
},
},
};
</script>