2022-04-15 14:24:30 +00:00
|
|
|
<template>
|
2022-08-10 14:56:01 +00:00
|
|
|
<div @keydown.esc="maybeClose">
|
2022-08-01 08:58:25 +00:00
|
|
|
<SoundBars v-if="loading"/>
|
2022-08-10 14:56:01 +00:00
|
|
|
<form v-else data-testid="add-user-form" @submit.prevent="submit">
|
2022-04-15 14:24:30 +00:00
|
|
|
<header>
|
|
|
|
<h1>Add New 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:15:30 +00:00
|
|
|
<label>
|
|
|
|
Name
|
|
|
|
<input v-model="newUser.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:15:30 +00:00
|
|
|
<label>
|
|
|
|
Email
|
|
|
|
<input v-model="newUser.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:15:30 +00:00
|
|
|
<label>
|
|
|
|
Password
|
|
|
|
<input
|
|
|
|
v-model="newUser.password"
|
|
|
|
autocomplete="new-password"
|
|
|
|
name="password"
|
|
|
|
required
|
|
|
|
title="Password"
|
|
|
|
type="password"
|
|
|
|
>
|
|
|
|
</label>
|
2022-04-23 21:36:19 +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-09-11 08:29:09 +00:00
|
|
|
<CheckBox name="is_admin" v-model="newUser.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-add" type="submit">Save</Btn>
|
2022-04-23 21:36:19 +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-07-07 18:05:46 +00:00
|
|
|
import { reactive, ref } from 'vue'
|
2022-04-15 14:24:30 +00:00
|
|
|
import { CreateUserData, userStore } from '@/stores'
|
2022-07-26 09:51:19 +00:00
|
|
|
import { parseValidationError, requireInjection } from '@/utils'
|
|
|
|
import { DialogBoxKey, MessageToasterKey } from '@/symbols'
|
2022-04-15 14:24:30 +00:00
|
|
|
|
2022-07-07 18:05:46 +00:00
|
|
|
import Btn from '@/components/ui/Btn.vue'
|
2022-08-01 08:58:25 +00:00
|
|
|
import SoundBars from '@/components/ui/SoundBars.vue'
|
2022-07-07 18:05:46 +00:00
|
|
|
import TooltipIcon from '@/components/ui/TooltipIcon.vue'
|
2022-09-11 08:29:09 +00:00
|
|
|
import CheckBox from '@/components/ui/CheckBox.vue'
|
2022-04-15 14:24:30 +00:00
|
|
|
|
2022-07-26 09:51:19 +00:00
|
|
|
const toaster = requireInjection(MessageToasterKey)
|
|
|
|
const dialog = requireInjection(DialogBoxKey)
|
|
|
|
|
2022-04-15 17:00:08 +00:00
|
|
|
const loading = ref(false)
|
2022-04-23 21:36:19 +00:00
|
|
|
|
|
|
|
const emptyUserData: CreateUserData = {
|
2022-04-15 17:00:08 +00:00
|
|
|
name: '',
|
|
|
|
email: '',
|
|
|
|
password: '',
|
|
|
|
is_admin: false
|
2022-04-23 21:36:19 +00:00
|
|
|
}
|
2022-04-15 14:24:30 +00:00
|
|
|
|
2022-04-23 21:36:19 +00:00
|
|
|
const newUser = reactive<CreateUserData>(Object.assign({}, emptyUserData))
|
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.store(newUser)
|
2022-07-26 09:51:19 +00:00
|
|
|
toaster.value.success(`New user "${newUser.name}" created.`)
|
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.'
|
2022-07-26 09:51:19 +00:00
|
|
|
dialog.value.error(msg, 'Error')
|
2022-04-15 17:00:08 +00:00
|
|
|
} finally {
|
|
|
|
loading.value = false
|
|
|
|
}
|
|
|
|
}
|
2022-04-15 14:24:30 +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(newUser, emptyUserData)) {
|
|
|
|
close()
|
|
|
|
return
|
2022-04-15 14:24:30 +00:00
|
|
|
}
|
2022-04-15 17:00:08 +00:00
|
|
|
|
2022-07-26 09:51:19 +00:00
|
|
|
await dialog.value.confirm('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>
|