index.vue
5.11 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
<script setup lang="ts">
import * as z from 'zod'
import type { FormSubmitEvent } from '@nuxt/ui'
const { t } = useAppI18n()
const toast = useToast()
const isSubmitting = ref(false)
const settingsApi = useSettingsApi()
const fileRef = ref<HTMLInputElement>()
const buildProfileSchema = () => z.object({
name: z.string().min(2, t('settings.profile.validation.tooShort')),
email: z.string().email(t('settings.profile.validation.invalidEmail')),
username: z.string().min(2, t('settings.profile.validation.tooShort')),
avatar: z.string().optional(),
bio: z.string().optional()
})
const profileSchema = computed(buildProfileSchema)
type ProfileSchema = z.output<ReturnType<typeof buildProfileSchema>>
const { data: initialProfile } = await settingsApi.getProfile()
const profile = reactive<ProfileSchema>({
name: initialProfile.value.name,
email: initialProfile.value.email,
username: initialProfile.value.username,
avatar: initialProfile.value.avatar,
bio: initialProfile.value.bio
})
async function onSubmit(event: FormSubmitEvent<ProfileSchema>) {
isSubmitting.value = true
try {
const result = await settingsApi.updateProfile(event.data)
if (!result.success) {
toast.add({
title: t('common.error'),
description: result.message,
icon: 'i-lucide-circle-alert',
color: 'error'
})
return
}
if (result.profile) {
profile.name = result.profile.name
profile.email = result.profile.email
profile.username = result.profile.username
profile.avatar = result.profile.avatar
profile.bio = result.profile.bio
}
toast.add({
title: t('settings.profile.toastTitle'),
description: t('settings.profile.toastDesc'),
icon: 'i-lucide-check',
color: 'success'
})
} catch {
toast.add({
title: t('common.error'),
description: t('common.requestFailed'),
icon: 'i-lucide-circle-alert',
color: 'error'
})
} finally {
isSubmitting.value = false
}
}
function onFileChange(e: Event) {
const input = e.target as HTMLInputElement
if (!input.files?.length) {
return
}
profile.avatar = URL.createObjectURL(input.files[0]!)
}
function onFileClick() {
fileRef.value?.click()
}
</script>
<template>
<UForm
id="settings"
:schema="profileSchema"
:state="profile"
@submit="onSubmit"
>
<UPageCard
:title="t('settings.profile.title')"
:description="t('settings.profile.description')"
variant="naked"
orientation="horizontal"
class="mb-4"
>
<UButton
form="settings"
:label="t('common.saveChanges')"
color="neutral"
type="submit"
:loading="isSubmitting"
class="w-fit lg:ms-auto"
/>
</UPageCard>
<UPageCard variant="subtle">
<UFormField
name="name"
:label="t('settings.profile.name')"
:description="t('settings.profile.nameDesc')"
required
class="flex max-sm:flex-col justify-between items-start gap-4"
>
<UInput
v-model="profile.name"
autocomplete="off"
/>
</UFormField>
<USeparator />
<UFormField
name="email"
:label="t('settings.profile.email')"
:description="t('settings.profile.emailDesc')"
required
class="flex max-sm:flex-col justify-between items-start gap-4"
>
<UInput
v-model="profile.email"
type="email"
autocomplete="off"
/>
</UFormField>
<USeparator />
<UFormField
name="username"
:label="t('settings.profile.username')"
:description="t('settings.profile.usernameDesc')"
required
class="flex max-sm:flex-col justify-between items-start gap-4"
>
<UInput
v-model="profile.username"
type="username"
autocomplete="off"
/>
</UFormField>
<USeparator />
<UFormField
name="avatar"
:label="t('settings.profile.avatar')"
:description="t('settings.profile.avatarDesc')"
class="flex max-sm:flex-col justify-between sm:items-center gap-4"
>
<div class="flex flex-wrap items-center gap-3">
<UAvatar
:src="profile.avatar"
:alt="profile.name"
size="lg"
/>
<UButton
:label="t('common.choose')"
color="neutral"
type="button"
@click="onFileClick"
/>
<input
ref="fileRef"
type="file"
class="hidden"
accept=".jpg, .jpeg, .png, .gif"
@change="onFileChange"
>
</div>
</UFormField>
<USeparator />
<UFormField
name="bio"
:label="t('settings.profile.bio')"
:description="t('settings.profile.bioDesc')"
class="flex max-sm:flex-col justify-between items-start gap-4"
:ui="{ container: 'w-full' }"
>
<UTextarea
v-model="profile.bio"
:rows="5"
autoresize
class="w-full"
/>
</UFormField>
</UPageCard>
</UForm>
</template>