update edit view with feedback

This commit is contained in:
axolotle 2023-08-28 15:10:25 +02:00
parent c898122abe
commit 8d6b1aa791

View file

@ -4,40 +4,80 @@ import { toTypedSchema } from '@vee-validate/yup'
import * as yup from 'yup' import * as yup from 'yup'
import type { UserData } from '@/composables/api' import type { UserData } from '@/composables/api'
const { t } = useI18n()
const { userData, update } = await useUserInfo() const { userData, update } = await useUserInfo()
const { handleSubmit, setFieldError } = useForm({ const loading: Ref<boolean | null> = ref(false)
const feedback: Ref<{
variant: 'success' | 'warning' | 'error'
icon: string
message: string
} | null> = ref(null)
const { handleSubmit, setFieldError, resetForm, meta } = useForm({
validationSchema: toTypedSchema( validationSchema: toTypedSchema(
yup.object({ yup.object({
username: yup.string().required(), // username: yup.string().required(),
fullname: yup.string().required().min(2), fullname: yup.string().required().min(2),
mailalias: yup.array().of(yup.string().email().required()), mailalias: yup.array().of(yup.string().email().required()).required(),
mailforward: yup.array().of(yup.string().email().required()), mailforward: yup.array().of(yup.string().email().required()).required(),
}), }),
), ),
initialValues: { initialValues: {
username: userData.value.username, // username: userData.value.username,
fullname: userData.value.fullname, fullname: userData.value.fullname,
mailalias: userData.value.mailalias.length mailalias: userData.value.mailalias,
? userData.value.mailalias mailforward: userData.value.mailforward,
: [''],
mailforward: userData.value.mailforward.length
? userData.value.mailforward
: [''],
}, },
}) })
watch(
() => meta.value.touched,
(value) => {
// remove loading and feedback on edition
if (value) {
loading.value = null
feedback.value = null
}
},
)
const onSubmit = handleSubmit(async (form) => { const onSubmit = handleSubmit(async (form) => {
const { data, error } = await useApi<UserData>('/update', { loading.value = true
const { error, data } = await useApi<
Pick<UserData, 'fullname' | 'mailalias' | 'mailforward'>
>('/update', {
method: 'PUT', method: 'PUT',
body: form, body: form,
}) })
if (error.value) { if (error.value) {
setFieldError(error.value.data.path, error.value.data.error_key) const errData = error.value.data
if (errData.path) {
setFieldError(errData.path, errData.error)
} else {
feedback.value = {
variant: 'error',
icon: 'mdi:alert',
message: errData.error,
}
}
} else if (data.value) { } else if (data.value) {
update(data.value) update(data.value)
resetForm({
values: data.value,
touched: Object.fromEntries(
['fullname', 'mailalias', 'mailforward'].map((key) => [key, false]),
),
})
feedback.value = {
variant: 'success',
icon: 'mdi:thumb-up',
message: t('user_profile_updated'),
}
} }
loading.value = false
}) })
</script> </script>
@ -46,9 +86,11 @@ const onSubmit = handleSubmit(async (form) => {
<PageTitle :text="$t('footerlink_edit')" /> <PageTitle :text="$t('footerlink_edit')" />
<form novalidate class="my-10" @submit="onSubmit"> <form novalidate class="my-10" @submit="onSubmit">
<div class="sm:flex sm:justify-between"> <BaseAlert v-show="feedback" v-bind="feedback" class="mb-10" />
<div class="sm:w-1/3">
<FormField name="username" :label="$t('username')" class="mb-3"> <div class="lg:flex lg:justify-between">
<div class="lg:w-1/2 lg:me-20">
<!-- <FormField name="username" :label="$t('username')" class="mb-3">
<TextInput <TextInput
name="username" name="username"
type="text" type="text"
@ -56,7 +98,7 @@ const onSubmit = handleSubmit(async (form) => {
disabled disabled
class="w-full" class="w-full"
/> />
</FormField> </FormField> -->
<FormField name="fullname" :label="$t('fullname')"> <FormField name="fullname" :label="$t('fullname')">
<TextInput <TextInput
@ -69,7 +111,7 @@ const onSubmit = handleSubmit(async (form) => {
</FormField> </FormField>
</div> </div>
<div class="basis-1/2 mt-10 sm:mt-0"> <div class="basis-1/2 mt-10 lg:mt-0">
<TextInputList <TextInputList
name="mailalias" name="mailalias"
type="email" type="email"
@ -89,14 +131,21 @@ const onSubmit = handleSubmit(async (form) => {
</div> </div>
</div> </div>
<!-- SR "loading" announcement -->
<BaseAlert
:message="loading ? $t('api.processing') : ''"
class="sr-only"
assertive
/>
<div class="flex mt-10"> <div class="flex mt-10">
<NuxtLink to="/password" class="btn btn-primary"> <NuxtLink to="/password" class="btn btn-primary">
{{ $t('change_password') }} {{ $t('change_password') }}
</NuxtLink> </NuxtLink>
<NuxtLink to="/" class="btn ml-auto mr-2"> <NuxtLink to="/" class="btn ml-auto me-2">
{{ $t('cancel') }} {{ $t('cancel') }}
</NuxtLink> </NuxtLink>
<YButton :text="$t('save')" type="submit" variant="success" /> <SubmitButton :loading="loading" variant="success" />
</div> </div>
</form> </form>
</div> </div>