-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormsContact.vue
More file actions
263 lines (239 loc) · 9.41 KB
/
Copy pathFormsContact.vue
File metadata and controls
263 lines (239 loc) · 9.41 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<script setup lang="ts">
import type { CmsElementForm } from "@shopware/composables"
import { ApiClientError } from "@shopware/api-client"
type CmsSlotWithContactConfig = CmsElementForm & {
translated?: {
config?: {
title?: {
value?: string
}
confirmationText?: {
value?: string
}
}
}
}
const props = defineProps<{
content?: CmsSlotWithContactConfig
}>()
// Composables
const { t } = useI18n()
const { getSalutations } = useSalutations()
const { apiClient } = useShopwareContext()
const { foreignKey } = useNavigationContext()
const { resolveApiErrors } = useApiErrorsResolver()
const { error } = useGlobalNotifications()
// State
const isLoading = ref(false)
const formSent = ref(false)
const formData = reactive({
salutationId: '',
firstName: '',
lastName: '',
email: '',
phone: '',
subject: '',
comment: '',
checkbox: false
})
// Computed
const title = computed(() => {
return props.content?.translated?.config?.title?.value ?? t('contact.headline')
})
const confirmationText = computed(() => {
return props.content?.translated?.config?.confirmationText?.value ?? t('contact.successMessage')
})
const salutationOptions = computed(() =>
getSalutations.value.map(salutation => ({
value: salutation.id,
label: salutation.displayName
}))
)
const isFormValid = computed(() => {
const requiredFields = [
formData.salutationId,
formData.firstName,
formData.lastName,
formData.email,
formData.phone,
formData.subject,
formData.comment
]
const allFieldsFilled = requiredFields.every(field => field.trim().length > 0)
const isEmailValid = /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(formData.email)
const isFirstNameValid = formData.firstName.trim().length >= 3
const isLastNameValid = formData.lastName.trim().length >= 3
const isPhoneValid = formData.phone.trim().length >= 3
const isSubjectValid = formData.subject.trim().length >= 3
const isCommentValid = formData.comment.trim().length >= 10
const isPrivacyAccepted = formData.checkbox
return allFieldsFilled &&
isEmailValid &&
isFirstNameValid &&
isLastNameValid &&
isPhoneValid &&
isSubjectValid &&
isCommentValid &&
isPrivacyAccepted &&
!isLoading.value
})
const handleSubmit = async () => {
if (!isFormValid.value) return
isLoading.value = true
try {
await apiClient.invoke("sendContactMail post /contact-form", {
body: {
...formData,
navigationId: foreignKey.value,
},
})
formSent.value = true
} catch (apiError) {
if (apiError instanceof ApiClientError) {
const errorMessages = resolveApiErrors(apiError.details.errors)
error(errorMessages.join(', ') || t('contact.unknownError'))
} else {
error(apiError instanceof Error ? apiError.message : t('contact.unknownError'))
}
} finally {
isLoading.value = false
}
}
</script>
<template>
<div>
<FoundationHeadline tag="h3" class="px-4 -ml-4 text-lg font-medium mb-2">
{{ title }}
</FoundationHeadline>
<div v-if="formSent" class="py-10 text-lg text-center">
{{ confirmationText }}
</div>
<form v-else @submit.prevent="handleSubmit">
<div class="space-y-6">
<div class="flex flex-col gap-4">
<!-- Salutation, First Name, Last Name Row -->
<div class="grid grid-cols-1 md:grid-cols-3 gap-4">
<!-- Salutation -->
<div class="w-full">
<FoundationLabel for="contact-salutation" class="block" required>
{{ $t('form.salutation') }}
<span aria-hidden="true">{{ $t('form.required') }}</span>
</FoundationLabel>
<FoundationSelect
id="contact-salutation"
v-model="formData.salutationId"
:placeholder="$t('form.chooseSalutation')"
:options="salutationOptions"
required
:disabled="isLoading"
name="salutation"
class="w-full"
/>
</div>
<!-- First Name -->
<ComponentTextInput
id="contact-first-name"
v-model="formData.firstName"
:label="$t('form.firstName')+$t('form.required')"
type="text"
:placeholder="$t('form.firstNamePlaceholder')"
required
:disabled="isLoading"
size="md"
bordered
/>
<!-- Last Name -->
<ComponentTextInput
id="contact-last-name"
v-model="formData.lastName"
:label="$t('form.lastName')+$t('form.required')"
type="text"
:placeholder="$t('form.lastNamePlaceholder')"
required
:disabled="isLoading"
size="md"
bordered
/>
</div>
<!-- Email and Phone Row -->
<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
<!-- Email -->
<ComponentTextInput
id="contact-email"
v-model="formData.email"
:label="$t('form.email')+$t('form.required')"
type="email"
:placeholder="$t('form.emailPlaceholder')"
required
:disabled="isLoading"
size="md"
bordered
/>
<!-- Phone -->
<ComponentTextInput
id="contact-phone"
v-model="formData.phone"
:label="$t('contact.phone')+$t('form.required')"
type="tel"
:placeholder="$t('contact.phonePlaceholder')"
required
:disabled="isLoading"
size="md"
bordered
/>
</div>
<!-- Subject -->
<ComponentTextInput
id="contact-subject"
v-model="formData.subject"
:label="$t('contact.subject')+$t('form.required')"
type="text"
:placeholder="$t('contact.subjectPlaceholder')"
required
:disabled="isLoading"
size="md"
bordered
/>
<!-- Message/Comment -->
<ComponentTextarea
id="contact-comment"
v-model="formData.comment"
:label="$t('contact.message')+$t('form.required')"
:placeholder="$t('contact.messagePlaceholder')"
:rows="5"
required
:disabled="isLoading"
bordered
/>
<!-- Data Protection Checkbox -->
<div class="flex items-center">
<FoundationCheckbox
id="contact-data-protection"
v-model="formData.checkbox"
:disabled="isLoading"
required
/>
<FoundationLabel for="contact-data-protection" class="ml-2 text-sm" required>
{{ $t('contact.dataProtection') }}
<span aria-hidden="true">{{ $t('form.required') }}</span>
</FoundationLabel>
</div>
<div class="text-sm">{{ $t('form.requiredFieldsInfo') }}</div>
<!-- Submit Button -->
<div class="flex flex-col sm:flex-row gap-3 pt-4">
<FoundationButton
type="submit"
color="secondary"
class="flex-1"
:loading="isLoading"
:disabled="!isFormValid"
@click="handleSubmit"
>
{{ $t('contact.submit') }}
</FoundationButton>
</div>
</div>
</div>
</form>
</div>
</template>