-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccountPersonalData.vue
More file actions
263 lines (232 loc) · 8.13 KB
/
Copy pathAccountPersonalData.vue
File metadata and controls
263 lines (232 loc) · 8.13 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 { getTranslatedProperty } from '@shopware/helpers'
import type { operations } from '#shopware'
const { user, refreshUser, updatePersonalInfo } = useUser()
const { getSalutations } = useSalutations()
const { success, error: showError } = useGlobalNotifications()
const { t } = useI18n()
const isLoading = ref(false)
const isSuccess = ref(false)
const state = reactive({
firstName: '',
lastName: '',
salutationId: '',
title: '',
accountType: 'private' as 'private' | 'business',
company: '',
vatIds: ''
})
const errors = reactive({
firstName: '',
lastName: '',
salutationId: '',
company: '',
vatIds: ''
})
const salutations = computed(() => {
return getSalutations.value.map((salutation) => {
return {
value: salutation.id,
label: getTranslatedProperty(salutation, 'displayName'),
disabled: false
}
})
})
const accountTypes = reactive([
{
value: 'private',
label: t('form.accountTypePrivate'),
disabled: false
},
{
value: 'business',
label: t('form.accountTypeBusiness'),
disabled: false
}
])
const validateForm = () => {
let isValid = true
// Reset errors
Object.keys(errors).forEach(key => {
errors[key as keyof typeof errors] = ''
})
if (!state.firstName.trim()) {
errors.firstName = t('form.validation.required', { field: t('form.firstName') })
isValid = false
}
if (!state.lastName.trim()) {
errors.lastName = t('form.validation.required', { field: t('form.lastName') })
isValid = false
}
if (!state.salutationId) {
errors.salutationId = t('form.validation.required', { field: t('form.salutation') })
isValid = false
}
if (state.accountType === 'business') {
if (!state.company.trim()) {
errors.company = t('form.validation.required', { field: t('form.company') })
isValid = false
}
if (!state.vatIds.trim()) {
errors.vatIds = t('form.validation.required', { field: t('form.vatId') })
isValid = false
}
}
return isValid
}
const handleSubmit = async () => {
if (!validateForm()) return
isLoading.value = true
isSuccess.value = false
try {
const baseData = {
firstName: state.firstName,
lastName: state.lastName,
salutationId: state.salutationId,
title: state.title || undefined
}
// The change-profile body is a discriminated union on accountType, so the
// business-specific fields have to be part of the same object literal
// rather than assigned afterwards.
const updateData: operations["changeProfile post /account/change-profile"]["body"] =
state.accountType === 'business'
? {
...baseData,
accountType: 'business',
company: state.company,
vatIds: [state.vatIds]
}
: baseData
await updatePersonalInfo(updateData)
await refreshUser()
isSuccess.value = true
success(t('account.profile.messages.personalDataUpdated'))
} catch (error) {
console.error('Failed to update personal data:', error)
showError(t('messages.error'))
} finally {
isLoading.value = false
}
}
onMounted(async () => {
await refreshUser()
if (user.value) {
state.firstName = user.value.firstName || ''
state.lastName = user.value.lastName || ''
state.salutationId = user.value.salutationId || ''
state.title = user.value.title || ''
state.accountType = user.value.accountType || 'private'
if (user.value.accountType === 'business') {
state.company = user.value.company || ''
state.vatIds = user.value.vatIds?.[0] || ''
}
}
})
</script>
<template>
<div class="space-y-6">
<div class="text-sm text-muted-foreground">
{{ $t('account.profile.personalDataInfo') }}
</div>
<form class="space-y-6" @submit.prevent="handleSubmit">
<!-- Account Type -->
<div class="space-y-2">
<FoundationLabel for="accountType" class="mb-1">
{{ $t('form.accountType') }}
</FoundationLabel>
<FoundationSelect
id="accountType"
v-model="state.accountType"
:options="accountTypes"
class="w-full"
/>
</div>
<!-- Salutation and Title Row -->
<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
<!-- Salutation -->
<div class="flex flex-col space-y-2">
<FoundationLabel for="salutation" class="mb-1">
{{ $t('form.salutation') }} *
</FoundationLabel>
<FoundationSelect
id="salutation"
v-model="state.salutationId"
required
:options="salutations"
:placeholder="$t('form.salutationPlaceholder')"
class="w-full"
/>
<p v-if="errors.salutationId" class="text-error text-xs">
{{ errors.salutationId }}
</p>
</div>
<!-- Title -->
<ComponentTextInput
id="title"
v-model="state.title"
:label="$t('form.title')"
bordered
:disabled="isLoading"
/>
</div>
<!-- Name Row -->
<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
<!-- First Name -->
<ComponentTextInput
id="firstName"
v-model="state.firstName"
:label="`${$t('form.firstName')} *`"
bordered
:disabled="isLoading"
:error="errors.firstName"
/>
<!-- Last Name -->
<ComponentTextInput
id="lastName"
v-model="state.lastName"
:label="`${$t('form.lastName')} *`"
bordered
:disabled="isLoading"
:error="errors.lastName"
/>
</div>
<!-- Business Fields (shown only when accountType is business) -->
<div v-if="state.accountType === 'business'" class="space-y-4">
<!-- Company -->
<ComponentTextInput
id="company"
v-model="state.company"
:label="`${$t('form.company')} *`"
bordered
:disabled="isLoading"
:error="errors.company"
/>
<!-- VAT ID -->
<ComponentTextInput
id="vatIds"
v-model="state.vatIds"
:label="`${$t('form.vatId')} *`"
bordered
:disabled="isLoading"
:error="errors.vatIds"
/>
</div>
<!-- Submit Button -->
<div class="pt-4 flex justify-end">
<FoundationButton
type="submit"
color="secondary"
:disabled="isLoading"
class="w-full md:w-auto"
@click="handleSubmit"
>
<div v-if="isLoading" class="flex items-center gap-2">
<div class="animate-spin rounded-full h-4 w-4 border-2 border-white border-t-transparent"/>
{{ $t('form.saving') }}
</div>
<span v-else>{{ $t('form.save') }}</span>
</FoundationButton>
</div>
</form>
</div>
</template>