edit: add error handling and rename keys

This commit is contained in:
axolotle 2023-08-01 15:13:35 +02:00
parent 7a18f54de9
commit 9f7451a276
4 changed files with 40 additions and 26 deletions

View file

@ -50,8 +50,8 @@ export interface UserData {
username: string username: string
fullname: string fullname: string
mail: string mail: string
'mail-aliases': string[] mailalias: string[]
'mail-forward': string[] mailforward: string[]
groups: string[] groups: string[]
apps: Record<string, { label: string; url: string }> apps: Record<string, { label: string; url: string }>
} }
@ -65,5 +65,13 @@ export async function useUserInfo() {
const { data } = await useApi('/me') const { data } = await useApi('/me')
userData.value = data.value as UserData userData.value = data.value as UserData
} }
return userData as Ref<UserData>
const update = (data: Partial<UserData>) => {
Object.assign(userData.value as UserData, data)
}
return {
userData: userData as Ref<UserData>,
update,
}
} }

View file

@ -5,7 +5,7 @@ const head = useLocaleHead({
}) })
const { t } = useI18n() const { t } = useI18n()
const isLoggedIn = useIsLoggedIn() const isLoggedIn = useIsLoggedIn()
const me = await useUserInfo() const { userData: me } = await useUserInfo()
const colorMode = useColorMode() const colorMode = useColorMode()
const themes = [ const themes = [

View file

@ -2,32 +2,42 @@
import { useForm } from 'vee-validate' import { useForm } from 'vee-validate'
import { toTypedSchema } from '@vee-validate/yup' import { toTypedSchema } from '@vee-validate/yup'
import * as yup from 'yup' import * as yup from 'yup'
import type { UserData } from '@/composables/api'
const me = await useUserInfo() const { userData, update } = await useUserInfo()
const { handleSubmit } = useForm({ const { handleSubmit, setFieldError } = useForm({
validationSchema: toTypedSchema( validationSchema: toTypedSchema(
yup.object({ yup.object({
username: yup.string(), username: yup.string().required(),
fullname: yup.string().required().min(1), fullname: yup.string().required().min(1),
mailAliases: yup.array().of(yup.string().email()), mailalias: yup.array().of(yup.string().email().required()),
mailForward: yup.array().of(yup.string().email()), mailforward: yup.array().of(yup.string().email().required()),
}), }),
), ),
initialValues: { initialValues: {
username: me.value.username, username: userData.value.username,
fullname: me.value.fullname, fullname: userData.value.fullname,
mailAliases: me.value['mail-aliases'].length mailalias: userData.value.mailalias.length
? me.value['mail-aliases'] ? userData.value.mailalias
: [''], : [''],
mailForward: me.value['mail-forward'].length mailforward: userData.value.mailforward.length
? me.value['mail-forward'] ? userData.value.mailforward
: [''], : [''],
}, },
}) })
const onSubmit = handleSubmit((form) => { const onSubmit = handleSubmit(async (form) => {
console.log('SUBMIT user edit', form) const { data, error } = await useApi<UserData>('/update', {
method: 'PUT',
body: form,
})
if (error.value) {
setFieldError(error.value.data.path, error.value.data.error_key)
} else if (data.value) {
update(data.value)
}
}) })
</script> </script>
@ -58,21 +68,17 @@ const onSubmit = handleSubmit((form) => {
</div> </div>
<div class="basis-1/2"> <div class="basis-1/2">
<FormField <FormField name="mailalias" :label="$t('mail_addresses')" class="mb-10">
name="mailAliases"
:label="$t('mail_addresses')"
class="mb-10"
>
<TextInputList <TextInputList
name="mailAliases" name="mailalias"
type="text" type="text"
:placeholder="$t('new_mail')" :placeholder="$t('new_mail')"
/> />
</FormField> </FormField>
<FormField name="mailForward" :label="$t('mail_forward')"> <FormField name="mailforward" :label="$t('mail_forward')">
<TextInputList <TextInputList
name="mailForward" name="mailforward"
type="text" type="text"
:placeholder="$t('new_forward')" :placeholder="$t('new_forward')"
/> />

View file

@ -1,6 +1,6 @@
<script setup lang="ts"> <script setup lang="ts">
const { t } = useI18n() const { t } = useI18n()
const userData = await useUserInfo() const { userData } = await useUserInfo()
const me = computed(() => { const me = computed(() => {
const appTileColors = [ const appTileColors = [