2022-04-15 14:24:30 +00:00
|
|
|
<template>
|
2022-11-19 21:59:56 +00:00
|
|
|
<form data-testid="edit-user-form" @submit.prevent="submit" @keydown.esc="maybeClose">
|
|
|
|
<header>
|
|
|
|
<h1>Edit User</h1>
|
|
|
|
</header>
|
2022-04-15 14:24:30 +00:00
|
|
|
|
2024-04-04 22:20:42 +00:00
|
|
|
<main class="space-y-5">
|
2024-03-30 16:49:25 +00:00
|
|
|
<AlertBox v-if="user.sso_provider" type="info">
|
|
|
|
This user logs in via SSO by {{ user.sso_provider }}.<br>
|
|
|
|
</AlertBox>
|
|
|
|
|
2024-04-04 22:20:42 +00:00
|
|
|
<FormRow>
|
|
|
|
<template #label>Name</template>
|
|
|
|
<TextInput v-model="updateData.name" v-koel-focus name="name" required title="Name" />
|
|
|
|
</FormRow>
|
|
|
|
<FormRow>
|
|
|
|
<template #label>Email</template>
|
|
|
|
<TextInput
|
|
|
|
v-model="updateData.email"
|
|
|
|
:readonly="user.sso_provider"
|
|
|
|
name="email"
|
|
|
|
required
|
|
|
|
title="Email"
|
|
|
|
type="email"
|
|
|
|
/>
|
|
|
|
</FormRow>
|
|
|
|
<FormRow v-if="!user.sso_provider">
|
|
|
|
<template #label>Password</template>
|
|
|
|
<TextInput
|
|
|
|
v-model="updateData.password"
|
|
|
|
autocomplete="new-password"
|
|
|
|
name="password"
|
|
|
|
placeholder="Leave blank for no changes"
|
2024-04-22 21:04:03 +00:00
|
|
|
title="Password"
|
2024-04-04 22:20:42 +00:00
|
|
|
type="password"
|
|
|
|
/>
|
|
|
|
<template #help>Min. 10 characters. Should be a mix of characters, numbers, and symbols.</template>
|
|
|
|
</FormRow>
|
|
|
|
<FormRow>
|
|
|
|
<div>
|
|
|
|
<CheckBox v-model="updateData.is_admin" name="is_admin" />User is an admin
|
2022-12-02 16:17:37 +00:00
|
|
|
<TooltipIcon title="Admins can perform administrative tasks like managing users and uploading songs." />
|
2024-04-04 22:20:42 +00:00
|
|
|
</div>
|
|
|
|
</FormRow>
|
2022-11-19 21:59:56 +00:00
|
|
|
</main>
|
2022-04-15 14:24:30 +00:00
|
|
|
|
2022-11-19 21:59:56 +00:00
|
|
|
<footer>
|
|
|
|
<Btn class="btn-update" type="submit">Update</Btn>
|
|
|
|
<Btn class="btn-cancel" white @click.prevent="maybeClose">Cancel</Btn>
|
|
|
|
</footer>
|
|
|
|
</form>
|
2022-04-15 14:24:30 +00:00
|
|
|
</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-11-19 21:59:56 +00:00
|
|
|
import { reactive, watch } from 'vue'
|
2022-11-27 15:29:29 +00:00
|
|
|
import { logger, parseValidationError } from '@/utils'
|
2022-04-15 14:24:30 +00:00
|
|
|
import { UpdateUserData, userStore } from '@/stores'
|
2022-11-27 15:29:29 +00:00
|
|
|
import { useDialogBox, useMessageToaster, useModal, useOverlay } from '@/composables'
|
2022-04-15 14:24:30 +00:00
|
|
|
|
2024-04-04 22:20:42 +00:00
|
|
|
import Btn from '@/components/ui/form/Btn.vue'
|
2022-07-07 18:05:46 +00:00
|
|
|
import TooltipIcon from '@/components/ui/TooltipIcon.vue'
|
2024-04-04 22:20:42 +00:00
|
|
|
import CheckBox from '@/components/ui/form/CheckBox.vue'
|
2024-03-30 16:49:25 +00:00
|
|
|
import AlertBox from '@/components/ui/AlertBox.vue'
|
2024-04-04 22:20:42 +00:00
|
|
|
import TextInput from '@/components/ui/form/TextInput.vue'
|
|
|
|
import FormRow from '@/components/ui/form/FormRow.vue'
|
2022-04-15 14:24:30 +00:00
|
|
|
|
2022-11-19 21:59:56 +00:00
|
|
|
const { showOverlay, hideOverlay } = useOverlay()
|
2022-11-18 17:45:38 +00:00
|
|
|
const { toastSuccess } = useMessageToaster()
|
|
|
|
const { showConfirmDialog, showErrorDialog } = useDialogBox()
|
2024-04-04 22:20:42 +00:00
|
|
|
|
2022-11-27 15:29:29 +00:00
|
|
|
const user = useModal().getFromContext<User>('user')
|
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 = {
|
2022-11-27 15:29:29 +00:00
|
|
|
name: user.name,
|
|
|
|
email: user.email,
|
|
|
|
is_admin: user.is_admin
|
2022-07-10 15:17:48 +00:00
|
|
|
}
|
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 submit = async () => {
|
2022-11-19 21:59:56 +00:00
|
|
|
showOverlay()
|
2022-04-15 14:24:30 +00:00
|
|
|
|
2022-04-15 17:00:08 +00:00
|
|
|
try {
|
2022-11-27 15:29:29 +00:00
|
|
|
await userStore.update(user, updateData)
|
2022-11-18 17:45:38 +00:00
|
|
|
toastSuccess('User profile updated.')
|
2022-04-15 17:00:08 +00:00
|
|
|
close()
|
2022-08-10 14:56:01 +00:00
|
|
|
} catch (error: any) {
|
|
|
|
const msg = error.response.status === 422 ? parseValidationError(error.response.data)[0] : 'Unknown error.'
|
2022-11-18 17:45:38 +00:00
|
|
|
showErrorDialog(msg, 'Error')
|
2022-08-10 14:56:01 +00:00
|
|
|
logger.error(error)
|
2022-04-15 17:00:08 +00:00
|
|
|
} finally {
|
2022-11-19 21:59:56 +00:00
|
|
|
hideOverlay()
|
2022-04-15 17:00:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-13 15:18:24 +00:00
|
|
|
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
|
|
|
|
2022-07-26 09:51:19 +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
|
|
|
|
2022-11-18 17:45:38 +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>
|