koel/resources/assets/js/components/profile-preferences/ProfileForm.vue

121 lines
3 KiB
Vue
Raw Normal View History

2022-04-15 14:24:30 +00:00
<template>
2022-04-21 22:51:48 +00:00
<form data-testid="update-profile-form" @submit.prevent="update">
2022-04-15 14:24:30 +00:00
<div class="form-row">
<label for="inputProfileCurrentPassword">Current Password</label>
<input
id="inputProfileCurrentPassword"
2022-04-21 22:51:48 +00:00
v-model="profile.current_password"
2022-04-15 14:24:30 +00:00
v-koel-focus
2022-04-21 22:51:48 +00:00
name="current_password"
placeholder="Required to update your profile"
2022-04-15 14:24:30 +00:00
required
2022-04-21 22:51:48 +00:00
type="password"
2022-04-15 14:24:30 +00:00
>
</div>
<div class="form-row">
<label for="inputProfileName">Name</label>
2022-04-21 22:51:48 +00:00
<input id="inputProfileName" v-model="profile.name" name="name" required type="text">
2022-04-15 14:24:30 +00:00
</div>
<div class="form-row">
<label for="inputProfileEmail">Email Address</label>
2022-04-21 22:51:48 +00:00
<input id="inputProfileEmail" v-model="profile.email" name="email" required type="email">
2022-04-15 14:24:30 +00:00
</div>
<div class="change-password">
<div class="form-row">
<label for="inputProfileNewPassword">New Password</label>
<input
2022-04-21 22:51:48 +00:00
id="inputProfileNewPassword"
2022-04-15 14:24:30 +00:00
v-model="profile.new_password"
2022-04-21 22:51:48 +00:00
autocomplete="new-password"
2022-04-15 14:24:30 +00:00
name="new_password"
2022-04-21 22:51:48 +00:00
placeholder="Leave empty to keep current password"
2022-04-15 14:24:30 +00:00
type="password"
>
<p class="password-rules help">
2022-04-21 22:51:48 +00:00
Min. 10 characters. Should be a mix of characters, numbers, and symbols.<br>
2022-04-15 14:24:30 +00:00
</p>
</div>
</div>
<div class="form-row">
2022-04-21 22:51:48 +00:00
<Btn class="btn-submit" type="submit">Save</Btn>
2022-04-29 20:15:10 +00:00
<span v-if="isDemo" class="demo-notice">
2022-04-15 14:24:30 +00:00
Changes will not be saved in the demo version.
</span>
</div>
</form>
</template>
2022-04-15 17:00:08 +00:00
<script lang="ts" setup>
2022-04-21 22:51:48 +00:00
import { defineAsyncComponent, onMounted, ref } from 'vue'
import { UpdateCurrentProfileData, userStore } from '@/stores'
2022-04-29 20:15:10 +00:00
import { alerts, isDemo, parseValidationError } from '@/utils'
2022-04-15 14:24:30 +00:00
2022-04-21 18:39:18 +00:00
const Btn = defineAsyncComponent(() => import('@/components/ui/Btn.vue'))
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
const profile = ref<UpdateCurrentProfileData>({} as unknown as UpdateCurrentProfileData)
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
onMounted(() => {
profile.value = {
name: userStore.current.name,
email: userStore.current.email,
current_password: null
2022-04-15 14:24:30 +00:00
}
})
2022-04-15 17:00:08 +00:00
const update = async () => {
if (!profile.value) {
throw Error()
}
2022-04-29 20:15:10 +00:00
if (isDemo) {
alerts.success('Profile updated.')
return
}
2022-04-15 17:00:08 +00:00
try {
await userStore.updateProfile(profile.value)
profile.value.current_password = null
delete profile.value.new_password
2022-04-21 22:51:48 +00:00
alerts.success('Profile updated.')
2022-04-15 17:00:08 +00:00
} catch (err: any) {
const msg = err.response.status === 422 ? parseValidationError(err.response.data)[0] : 'Unknown error.'
alerts.error(msg)
}
}
2022-04-15 14:24:30 +00:00
</script>
<style lang="scss" scoped>
input {
&[type="text"], &[type="email"], &[type="password"] {
width: 33%;
}
}
.change-password {
padding: 1.75rem 0;
}
.password-rules {
margin-top: .75rem;
}
2022-04-29 20:15:10 +00:00
.demo-notice {
font-size: .95rem;
opacity: .7;
margin-left: 5px;
}
2022-04-15 17:00:08 +00:00
@media only screen and (max-width: 667px) {
2022-04-15 14:24:30 +00:00
input {
&[type="text"], &[type="email"], &[type="password"] {
width: 100%;
height: 32px;
}
}
}
</style>