2022-04-15 14:24:30 +00:00
|
|
|
<template>
|
|
|
|
<div class="edit-user" @keydown.esc="maybeClose">
|
2022-04-15 17:00:08 +00:00
|
|
|
<SoundBar 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>
|
|
|
|
|
|
|
|
<div>
|
|
|
|
<div class="form-row">
|
|
|
|
<label>Name</label>
|
2022-04-23 21:46:25 +00:00
|
|
|
<input v-model="updateData.name" v-koel-focus name="name" required title="Name" type="text">
|
2022-04-15 14:24:30 +00:00
|
|
|
</div>
|
|
|
|
<div class="form-row">
|
|
|
|
<label>Email</label>
|
2022-04-23 21:46:25 +00:00
|
|
|
<input v-model="updateData.email" name="email" required title="Email" type="email">
|
2022-04-15 14:24:30 +00:00
|
|
|
</div>
|
|
|
|
<div class="form-row">
|
|
|
|
<label>Password</label>
|
|
|
|
<input
|
2022-04-23 21:46:25 +00:00
|
|
|
v-model="updateData.password"
|
|
|
|
autocomplete="new-password"
|
2022-04-15 14:24:30 +00:00
|
|
|
name="password"
|
|
|
|
placeholder="Leave blank for no changes"
|
|
|
|
type="password"
|
|
|
|
>
|
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>
|
2022-04-23 21:46:25 +00:00
|
|
|
<input v-model="updateData.is_admin" name="is_admin" type="checkbox"> 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>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<footer>
|
2022-04-15 17:00:08 +00:00
|
|
|
<Btn class="btn-update" type="submit">Update</Btn>
|
2022-05-10 23:01:48 +00:00
|
|
|
<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-04-23 21:46:25 +00:00
|
|
|
import { defineAsyncComponent, reactive, ref, toRefs } from 'vue'
|
2022-04-15 14:24:30 +00:00
|
|
|
import { alerts, parseValidationError } from '@/utils'
|
|
|
|
import { UpdateUserData, userStore } from '@/stores'
|
|
|
|
|
2022-04-21 18:39:18 +00:00
|
|
|
const Btn = defineAsyncComponent(() => import('@/components/ui/Btn.vue'))
|
2022-04-23 21:36:19 +00:00
|
|
|
const SoundBar = defineAsyncComponent(() => import('@/components/ui/SoundBar.vue'))
|
|
|
|
const TooltipIcon = defineAsyncComponent(() => import('@/components/ui/TooltipIcon.vue'))
|
2022-04-15 14:24:30 +00:00
|
|
|
|
2022-04-15 17:00:08 +00:00
|
|
|
const props = defineProps<{ user: User }>()
|
|
|
|
const { user } = toRefs(props)
|
2022-04-15 14:24:30 +00:00
|
|
|
|
2022-04-23 21:46:25 +00:00
|
|
|
const originalData: UpdateUserData = {
|
|
|
|
name: user.value.name,
|
|
|
|
email: user.value.email,
|
|
|
|
is_admin: user.value.is_admin
|
|
|
|
}
|
|
|
|
|
|
|
|
const updateData = reactive<UpdateUserData>(Object.assign({}, originalData))
|
|
|
|
|
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)
|
2022-04-21 22:51:48 +00:00
|
|
|
alerts.success('User profile updated.')
|
2022-04-15 17:00:08 +00:00
|
|
|
close()
|
|
|
|
} catch (err: any) {
|
|
|
|
const msg = err.response.status === 422 ? parseValidationError(err.response.data)[0] : 'Unknown error.'
|
|
|
|
alerts.error(msg)
|
|
|
|
} finally {
|
|
|
|
loading.value = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const emit = defineEmits(['close'])
|
2022-04-15 14:24:30 +00:00
|
|
|
|
2022-04-15 17:00:08 +00:00
|
|
|
const close = () => emit('close')
|
2022-04-15 14:24:30 +00:00
|
|
|
|
2022-04-15 17:00:08 +00:00
|
|
|
const maybeClose = () => {
|
|
|
|
if (isEqual(originalData, updateData)) {
|
|
|
|
close()
|
|
|
|
return
|
|
|
|
}
|
2022-04-15 14:24:30 +00:00
|
|
|
|
2022-04-15 17:00:08 +00:00
|
|
|
alerts.confirm('Discard all changes?', close)
|
|
|
|
}
|
2022-04-15 14:24:30 +00:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<style lang="scss" scoped>
|
|
|
|
.help {
|
|
|
|
margin-top: .75rem;
|
|
|
|
}
|
|
|
|
</style>
|