485 lines
12 KiB
Vue
485 lines
12 KiB
Vue
<script setup lang="ts">
|
|
import { freeAssessmentContentBundle } from '~/data/free-assessment-content'
|
|
|
|
const props = withDefaults(
|
|
defineProps<{
|
|
variant?: 'page' | 'embedded'
|
|
}>(),
|
|
{ variant: 'page' }
|
|
)
|
|
|
|
const { locale } = useSiteLocale()
|
|
const content = useLocalized(freeAssessmentContentBundle)
|
|
const freeAssessmentForm = computed(() => content.value.form)
|
|
|
|
const emit = defineEmits<{
|
|
submitted: []
|
|
}>()
|
|
|
|
const form = reactive({
|
|
firstName: '',
|
|
lastName: '',
|
|
email: '',
|
|
condition: '',
|
|
reports: '',
|
|
message: ''
|
|
})
|
|
|
|
type FormErrors = {
|
|
firstName?: string
|
|
email?: string
|
|
condition?: string
|
|
}
|
|
|
|
const formErrors = reactive<FormErrors>({})
|
|
const submitting = ref(false)
|
|
const submitError = ref('')
|
|
const submitSuccess = ref('')
|
|
const showSuccessPopup = ref(false)
|
|
const SUCCESS_RESET_DELAY_MS = 5000
|
|
let successResetTimer: ReturnType<typeof setTimeout> | null = null
|
|
|
|
const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
|
|
|
|
function resetFormFields() {
|
|
form.firstName = ''
|
|
form.lastName = ''
|
|
form.email = ''
|
|
form.condition = ''
|
|
form.reports = ''
|
|
form.message = ''
|
|
submitSuccess.value = ''
|
|
submitError.value = ''
|
|
showSuccessPopup.value = false
|
|
}
|
|
|
|
function scheduleSuccessReset() {
|
|
if (successResetTimer) clearTimeout(successResetTimer)
|
|
successResetTimer = setTimeout(() => {
|
|
resetFormFields()
|
|
successResetTimer = null
|
|
}, SUCCESS_RESET_DELAY_MS)
|
|
}
|
|
|
|
onUnmounted(() => {
|
|
if (successResetTimer) clearTimeout(successResetTimer)
|
|
})
|
|
|
|
function clearFieldError(field: keyof FormErrors) {
|
|
delete formErrors[field]
|
|
}
|
|
|
|
function validateForm(): boolean {
|
|
const messages = freeAssessmentForm.value.validation
|
|
formErrors.firstName = undefined
|
|
formErrors.email = undefined
|
|
formErrors.condition = undefined
|
|
|
|
let valid = true
|
|
|
|
if (!form.firstName.trim()) {
|
|
formErrors.firstName = messages.firstNameRequired
|
|
valid = false
|
|
}
|
|
|
|
const email = form.email.trim()
|
|
if (!email) {
|
|
formErrors.email = messages.emailRequired
|
|
valid = false
|
|
} else if (!emailPattern.test(email)) {
|
|
formErrors.email = messages.emailInvalid
|
|
valid = false
|
|
}
|
|
|
|
if (!form.condition.trim()) {
|
|
formErrors.condition = messages.conditionRequired
|
|
valid = false
|
|
}
|
|
|
|
return valid
|
|
}
|
|
|
|
async function onSubmit() {
|
|
if (!validateForm()) return
|
|
|
|
submitting.value = true
|
|
submitError.value = ''
|
|
submitSuccess.value = ''
|
|
|
|
try {
|
|
const res = await $fetch<{ code: number; message: string }>('/api/forms/assessment', {
|
|
method: 'POST',
|
|
body: {
|
|
lang: locale.value,
|
|
firstName: form.firstName.trim(),
|
|
lastName: form.lastName.trim(),
|
|
email: form.email.trim(),
|
|
condition: form.condition.trim(),
|
|
reports: form.reports,
|
|
message: form.message.trim(),
|
|
},
|
|
})
|
|
|
|
if (res.code !== 200) {
|
|
submitError.value = res.message
|
|
return
|
|
}
|
|
|
|
submitSuccess.value = freeAssessmentForm.value.submitSuccess
|
|
showSuccessPopup.value = true
|
|
scheduleSuccessReset()
|
|
} catch {
|
|
submitError.value = locale.value === 'zh' ? '提交失败,请稍后重试。' : 'Submission failed. Please try again.'
|
|
} finally {
|
|
submitting.value = false
|
|
}
|
|
}
|
|
</script>
|
|
|
|
<template>
|
|
<div class="fa-form-wrap" :class="{ 'fa-form-wrap--embedded': props.variant === 'embedded' }">
|
|
<form
|
|
class="fa-form"
|
|
:class="{ 'fa-form--embedded': props.variant === 'embedded', 'is-disabled': !!showSuccessPopup }"
|
|
:lang="locale"
|
|
novalidate
|
|
@submit.prevent="onSubmit"
|
|
>
|
|
<div class="fa-form__row fa-form__row--3">
|
|
<label class="fa-field">
|
|
<span class="fa-field__label">
|
|
{{ freeAssessmentForm.fields.firstName.label }}
|
|
<span
|
|
v-if="freeAssessmentForm.fields.firstName.required"
|
|
class="fa-field__req"
|
|
aria-hidden="true"
|
|
>*</span>
|
|
</span>
|
|
<input
|
|
v-model="form.firstName"
|
|
type="text"
|
|
class="fa-field__input"
|
|
:class="{ 'fa-field__input--error': formErrors.firstName }"
|
|
autocomplete="given-name"
|
|
aria-required="true"
|
|
:aria-invalid="formErrors.firstName ? 'true' : undefined"
|
|
:aria-describedby="formErrors.firstName ? 'fa-first-name-error' : undefined"
|
|
@input="clearFieldError('firstName')"
|
|
/>
|
|
<p v-if="formErrors.firstName" id="fa-first-name-error" class="fa-field__error" role="alert">
|
|
{{ formErrors.firstName }}
|
|
</p>
|
|
</label>
|
|
<label class="fa-field">
|
|
<span class="fa-field__label">
|
|
{{ freeAssessmentForm.fields.lastName.label }}
|
|
</span>
|
|
<input
|
|
v-model="form.lastName"
|
|
type="text"
|
|
class="fa-field__input"
|
|
autocomplete="family-name"
|
|
/>
|
|
</label>
|
|
<label class="fa-field">
|
|
<span class="fa-field__label">
|
|
{{ freeAssessmentForm.fields.email.label }}
|
|
<span
|
|
v-if="freeAssessmentForm.fields.email.required"
|
|
class="fa-field__req"
|
|
aria-hidden="true"
|
|
>*</span>
|
|
</span>
|
|
<input
|
|
v-model="form.email"
|
|
type="email"
|
|
class="fa-field__input"
|
|
:class="{ 'fa-field__input--error': formErrors.email }"
|
|
autocomplete="email"
|
|
inputmode="email"
|
|
aria-required="true"
|
|
:aria-invalid="formErrors.email ? 'true' : undefined"
|
|
:aria-describedby="formErrors.email ? 'fa-email-error' : undefined"
|
|
@input="clearFieldError('email')"
|
|
/>
|
|
<p v-if="formErrors.email" id="fa-email-error" class="fa-field__error" role="alert">
|
|
{{ formErrors.email }}
|
|
</p>
|
|
</label>
|
|
</div>
|
|
|
|
<label class="fa-field fa-field--full">
|
|
<span class="fa-field__label">
|
|
{{ freeAssessmentForm.fields.condition.label }}
|
|
<span
|
|
v-if="freeAssessmentForm.fields.condition.required"
|
|
class="fa-field__req"
|
|
aria-hidden="true"
|
|
>*</span>
|
|
</span>
|
|
<textarea
|
|
v-model="form.condition"
|
|
class="fa-field__textarea"
|
|
:class="{ 'fa-field__textarea--error': formErrors.condition }"
|
|
rows="4"
|
|
:placeholder="freeAssessmentForm.fields.condition.placeholder"
|
|
aria-required="true"
|
|
:aria-invalid="formErrors.condition ? 'true' : undefined"
|
|
:aria-describedby="formErrors.condition ? 'fa-condition-error' : undefined"
|
|
@input="clearFieldError('condition')"
|
|
/>
|
|
<p v-if="formErrors.condition" id="fa-condition-error" class="fa-field__error" role="alert">
|
|
{{ formErrors.condition }}
|
|
</p>
|
|
</label>
|
|
|
|
<label class="fa-field fa-field--full">
|
|
<span class="fa-field__label">
|
|
{{ freeAssessmentForm.fields.reports.label }}
|
|
</span>
|
|
<select v-model="form.reports" class="fa-field__select">
|
|
<option
|
|
v-for="opt in freeAssessmentForm.fields.reports.options"
|
|
:key="opt.value"
|
|
:value="opt.value"
|
|
>
|
|
{{ opt.label }}
|
|
</option>
|
|
</select>
|
|
</label>
|
|
|
|
<label class="fa-field fa-field--full">
|
|
<span class="fa-field__label">
|
|
{{ freeAssessmentForm.fields.message.label }}
|
|
</span>
|
|
<textarea
|
|
v-model="form.message"
|
|
class="fa-field__textarea"
|
|
rows="4"
|
|
/>
|
|
</label>
|
|
|
|
<p v-if="submitError" class="fa-form__submit-error" role="alert">{{ submitError }}</p>
|
|
|
|
<div class="fa-form__actions">
|
|
<button type="submit" class="btn fa-form__submit" :disabled="submitting">
|
|
{{ submitting ? (locale === 'zh' ? '提交中…' : 'Submitting…') : freeAssessmentForm.submitLabel }}
|
|
</button>
|
|
</div>
|
|
</form>
|
|
|
|
<Teleport to="body">
|
|
<Transition name="fa-modal">
|
|
<div v-if="showSuccessPopup" class="fa-modal" role="dialog" aria-modal="true" aria-labelledby="fa-success-title">
|
|
<button type="button" class="fa-modal__backdrop" aria-label="Close success popup" @click="showSuccessPopup = false" />
|
|
<div class="fa-modal__panel fa-modal__panel--success">
|
|
<button type="button" class="fa-modal__close" aria-label="Close" @click="showSuccessPopup = false">
|
|
<span aria-hidden="true">×</span>
|
|
</button>
|
|
<h2 id="fa-success-title" class="fa-modal__title">{{ locale === 'zh' ? '提交成功' : 'Submitted successfully' }}</h2>
|
|
<p class="fa-modal__success-text">{{ submitSuccess }}</p>
|
|
<p class="fa-modal__success-hint">{{ locale === 'zh' ? '约 5 秒后自动关闭' : 'Will close automatically in about 5 seconds' }}</p>
|
|
</div>
|
|
</div>
|
|
</Transition>
|
|
</Teleport>
|
|
</div>
|
|
</template>
|
|
|
|
<style scoped>
|
|
.fa-form {
|
|
background: #fff;
|
|
border: 1px solid #e3ecf5;
|
|
border-radius: var(--radius);
|
|
padding: 40px 44px 44px;
|
|
box-shadow: var(--shadow);
|
|
}
|
|
.fa-form.is-disabled {
|
|
pointer-events: none;
|
|
}
|
|
.fa-form--embedded {
|
|
background: transparent;
|
|
border: none;
|
|
border-radius: 0;
|
|
padding: 0;
|
|
box-shadow: none;
|
|
}
|
|
.fa-modal {
|
|
position: fixed;
|
|
inset: 0;
|
|
z-index: 1100;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
padding: 24px 16px;
|
|
}
|
|
.fa-modal__backdrop {
|
|
position: absolute;
|
|
inset: 0;
|
|
background: rgba(15, 39, 64, 0.55);
|
|
border: none;
|
|
cursor: pointer;
|
|
}
|
|
.fa-modal__panel {
|
|
position: relative;
|
|
width: 100%;
|
|
max-width: 720px;
|
|
background: #fff;
|
|
border: 1px solid #e3ecf5;
|
|
border-radius: var(--radius);
|
|
padding: 32px 36px 36px;
|
|
box-shadow: var(--shadow-lg);
|
|
}
|
|
.fa-modal__panel--success {
|
|
text-align: center;
|
|
}
|
|
.fa-modal__close {
|
|
position: absolute;
|
|
top: 12px;
|
|
right: 14px;
|
|
width: 36px;
|
|
height: 36px;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
font-size: 28px;
|
|
line-height: 1;
|
|
color: #0f2740;
|
|
background: transparent;
|
|
border: none;
|
|
border-radius: 8px;
|
|
cursor: pointer;
|
|
}
|
|
.fa-modal__title {
|
|
margin: 0 0 16px;
|
|
font-size: 24px;
|
|
font-weight: 800;
|
|
color: #0f2740;
|
|
}
|
|
.fa-modal__success-text {
|
|
margin: 0;
|
|
font-size: 16px;
|
|
line-height: 1.7;
|
|
color: #36506b;
|
|
}
|
|
.fa-modal__success-hint {
|
|
margin: 14px 0 0;
|
|
font-size: 13px;
|
|
color: #64748b;
|
|
}
|
|
.fa-form__row {
|
|
display: grid;
|
|
gap: 20px;
|
|
margin-bottom: 20px;
|
|
}
|
|
.fa-form__row--3 {
|
|
grid-template-columns: repeat(3, 1fr);
|
|
}
|
|
.fa-field {
|
|
display: flex;
|
|
flex-direction: column;
|
|
gap: 8px;
|
|
}
|
|
.fa-field--full {
|
|
margin-bottom: 20px;
|
|
}
|
|
.fa-field__label {
|
|
font-size: 14px;
|
|
font-weight: 600;
|
|
color: #0f2740;
|
|
}
|
|
.fa-field__req {
|
|
color: #e53935;
|
|
margin-left: 2px;
|
|
}
|
|
.fa-field__error {
|
|
margin: 0;
|
|
font-size: 13px;
|
|
line-height: 1.45;
|
|
color: #c62828;
|
|
}
|
|
.fa-field__input,
|
|
.fa-field__textarea,
|
|
.fa-field__select {
|
|
width: 100%;
|
|
font-family: inherit;
|
|
font-size: 15px;
|
|
color: var(--ink);
|
|
background: #fff;
|
|
border: 1px solid #e2eaf2;
|
|
border-radius: 8px;
|
|
padding: 11px 14px;
|
|
transition: border-color 0.18s ease, box-shadow 0.18s ease;
|
|
}
|
|
.fa-field__input--error,
|
|
.fa-field__textarea--error {
|
|
border-color: #e53935;
|
|
}
|
|
.fa-field__input--error:focus,
|
|
.fa-field__textarea--error:focus {
|
|
border-color: #e53935;
|
|
box-shadow: 0 0 0 3px rgba(229, 57, 53, 0.14);
|
|
}
|
|
.fa-field__input::placeholder,
|
|
.fa-field__textarea::placeholder {
|
|
color: #9aa8b8;
|
|
}
|
|
.fa-field__input:hover,
|
|
.fa-field__textarea:hover,
|
|
.fa-field__select:hover {
|
|
border-color: #c5d4e4;
|
|
}
|
|
.fa-field__input:focus,
|
|
.fa-field__textarea:focus,
|
|
.fa-field__select:focus {
|
|
outline: none;
|
|
border-color: var(--blue);
|
|
box-shadow: 0 0 0 3px rgba(72, 164, 252, 0.14);
|
|
}
|
|
.fa-field__textarea {
|
|
resize: vertical;
|
|
min-height: 108px;
|
|
line-height: 1.6;
|
|
}
|
|
.fa-field__select {
|
|
cursor: pointer;
|
|
height: 44px;
|
|
appearance: none;
|
|
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath fill='%23666666' d='M1.41 0L6 4.58 10.59 0 12 1.41l-6 6-6-6z'/%3E%3C/svg%3E");
|
|
background-repeat: no-repeat;
|
|
background-position: right 14px center;
|
|
padding-right: 40px;
|
|
}
|
|
.fa-form__submit-error {
|
|
margin: 0 0 12px;
|
|
font-size: 14px;
|
|
color: #c62828;
|
|
}
|
|
.fa-form__actions {
|
|
margin-top: 8px;
|
|
padding-top: 8px;
|
|
border-top: 1px solid #eef2f6;
|
|
}
|
|
.fa-form__submit:disabled {
|
|
opacity: 0.7;
|
|
cursor: not-allowed;
|
|
}
|
|
.fa-form__submit {
|
|
width: 100%;
|
|
height: 52px;
|
|
font-size: 16px;
|
|
}
|
|
|
|
@media (max-width: 900px) {
|
|
.fa-form__row--3 {
|
|
grid-template-columns: 1fr;
|
|
}
|
|
}
|
|
|
|
@media (max-width: 640px) {
|
|
.fa-form {
|
|
padding: 28px 20px 32px;
|
|
}
|
|
}
|
|
</style>
|