koel/resources/assets/js/components/user/EditUserForm.vue

118 lines
3.3 KiB
Vue
Raw Normal View History

2022-04-15 14:24:30 +00:00
<template>
<div class="edit-user" @keydown.esc="maybeClose">
2022-08-01 08:58:25 +00:00
<SoundBars v-if="loading"/>
2022-04-23 21:46:25 +00:00
<form v-else class="user-edit" data-testid="edit-user-form" @submit.prevent="submit">
2022-04-15 14:24:30 +00:00
<header>
<h1>Edit User</h1>
</header>
2022-07-22 16:15:30 +00:00
<main>
2022-04-15 14:24:30 +00:00
<div class="form-row">
2022-07-22 16:43:32 +00:00
<label>
Name
<input v-model="updateData.name" v-koel-focus name="name" required title="Name" type="text">
</label>
2022-04-15 14:24:30 +00:00
</div>
<div class="form-row">
2022-07-22 16:43:32 +00:00
<label>
Email
<input v-model="updateData.email" name="email" required title="Email" type="email">
</label>
2022-04-15 14:24:30 +00:00
</div>
<div class="form-row">
2022-07-22 16:43:32 +00:00
<label>
Password
<input
v-model="updateData.password"
autocomplete="new-password"
name="password"
placeholder="Leave blank for no changes"
type="password"
>
</label>
2022-04-23 21:46:25 +00:00
<p class="help">Min. 10 characters. Should be a mix of characters, numbers, and symbols.</p>
2022-04-15 14:24:30 +00:00
</div>
<div class="form-row">
<label>
<CheckBox v-model="updateData.is_admin" name="is_admin"/>
User is an admin
2022-04-15 17:00:08 +00:00
<TooltipIcon title="Admins can perform administrative tasks like managing users and uploading songs."/>
2022-04-15 14:24:30 +00:00
</label>
</div>
2022-07-22 16:15:30 +00:00
</main>
2022-04-15 14:24:30 +00:00
<footer>
2022-04-15 17:00:08 +00:00
<Btn class="btn-update" type="submit">Update</Btn>
<Btn class="btn-cancel" white @click.prevent="maybeClose">Cancel</Btn>
2022-04-15 14:24:30 +00:00
</footer>
</form>
</div>
</template>
2022-04-15 17:00:08 +00:00
<script lang="ts" setup>
2022-04-15 14:24:30 +00:00
import { isEqual } from 'lodash'
2022-07-20 08:00:02 +00:00
import { reactive, ref, watch } from 'vue'
import { logger, parseValidationError, requireInjection } from '@/utils'
2022-04-15 14:24:30 +00:00
import { UpdateUserData, userStore } from '@/stores'
import { useDialogBox, useMessageToaster } from '@/composables'
import { UserKey } from '@/symbols'
2022-04-15 14:24:30 +00:00
import Btn from '@/components/ui/Btn.vue'
2022-08-01 08:58:25 +00:00
import SoundBars from '@/components/ui/SoundBars.vue'
import TooltipIcon from '@/components/ui/TooltipIcon.vue'
import CheckBox from '@/components/ui/CheckBox.vue'
2022-04-15 14:24:30 +00:00
const { toastSuccess } = useMessageToaster()
const { showConfirmDialog, showErrorDialog } = useDialogBox()
2022-07-20 08:00:02 +00:00
const [user] = requireInjection(UserKey)
2022-04-15 14:24:30 +00:00
2022-07-10 15:17:48 +00:00
let originalData: UpdateUserData
let updateData: UpdateUserData
watch(user, () => {
originalData = {
name: user.value.name,
email: user.value.email,
is_admin: user.value.is_admin
}
2022-04-23 21:46:25 +00:00
2022-07-10 15:17:48 +00:00
updateData = reactive(Object.assign({}, originalData))
}, { immediate: true })
2022-04-23 21:46:25 +00:00
2022-04-15 17:00:08 +00:00
const loading = ref(false)
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
const submit = async () => {
loading.value = true
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
try {
await userStore.update(user.value, updateData)
toastSuccess('User profile updated.')
2022-04-15 17:00:08 +00:00
close()
} catch (error: any) {
const msg = error.response.status === 422 ? parseValidationError(error.response.data)[0] : 'Unknown error.'
showErrorDialog(msg, 'Error')
logger.error(error)
2022-04-15 17:00:08 +00:00
} finally {
loading.value = false
}
}
const emit = defineEmits<{ (e: 'close'): void }>()
2022-04-15 17:00:08 +00:00
const close = () => emit('close')
2022-04-15 14:24:30 +00:00
const maybeClose = async () => {
2022-04-15 17:00:08 +00:00
if (isEqual(originalData, updateData)) {
close()
return
}
2022-04-15 14:24:30 +00:00
await showConfirmDialog('Discard all changes?') && close()
2022-04-15 17:00:08 +00:00
}
2022-04-15 14:24:30 +00:00
</script>
<style lang="scss" scoped>
.help {
margin-top: .75rem;
}
</style>