diff --git a/composables/api.ts b/composables/api.ts index f7cc281..662e1a9 100644 --- a/composables/api.ts +++ b/composables/api.ts @@ -50,8 +50,8 @@ export interface UserData { username: string fullname: string mail: string - 'mail-aliases': string[] - 'mail-forward': string[] + mailalias: string[] + mailforward: string[] groups: string[] apps: Record } @@ -65,5 +65,13 @@ export async function useUserInfo() { const { data } = await useApi('/me') userData.value = data.value as UserData } - return userData as Ref + + const update = (data: Partial) => { + Object.assign(userData.value as UserData, data) + } + + return { + userData: userData as Ref, + update, + } } diff --git a/layouts/default.vue b/layouts/default.vue index 0bf21bf..9378c83 100644 --- a/layouts/default.vue +++ b/layouts/default.vue @@ -5,7 +5,7 @@ const head = useLocaleHead({ }) const { t } = useI18n() const isLoggedIn = useIsLoggedIn() -const me = await useUserInfo() +const { userData: me } = await useUserInfo() const colorMode = useColorMode() const themes = [ diff --git a/pages/edit.vue b/pages/edit.vue index 0c24d8e..cbe5e0b 100644 --- a/pages/edit.vue +++ b/pages/edit.vue @@ -2,32 +2,42 @@ import { useForm } from 'vee-validate' import { toTypedSchema } from '@vee-validate/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( yup.object({ - username: yup.string(), + username: yup.string().required(), fullname: yup.string().required().min(1), - mailAliases: yup.array().of(yup.string().email()), - mailForward: yup.array().of(yup.string().email()), + mailalias: yup.array().of(yup.string().email().required()), + mailforward: yup.array().of(yup.string().email().required()), }), ), initialValues: { - username: me.value.username, - fullname: me.value.fullname, - mailAliases: me.value['mail-aliases'].length - ? me.value['mail-aliases'] + username: userData.value.username, + fullname: userData.value.fullname, + mailalias: userData.value.mailalias.length + ? userData.value.mailalias : [''], - mailForward: me.value['mail-forward'].length - ? me.value['mail-forward'] + mailforward: userData.value.mailforward.length + ? userData.value.mailforward : [''], }, }) -const onSubmit = handleSubmit((form) => { - console.log('SUBMIT user edit', form) +const onSubmit = handleSubmit(async (form) => { + const { data, error } = await useApi('/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) + } }) @@ -58,21 +68,17 @@ const onSubmit = handleSubmit((form) => {
- + - + diff --git a/pages/index.vue b/pages/index.vue index ed9874d..537e301 100644 --- a/pages/index.vue +++ b/pages/index.vue @@ -1,6 +1,6 @@