koel/resources/assets/js/components/user/add-form.vue

101 lines
2.8 KiB
Vue
Raw Normal View History

2022-04-15 14:24:30 +00:00
<template>
<div class="add-user" @keydown.esc="maybeClose">
2022-04-15 17:00:08 +00:00
<SoundBar v-if="loading"/>
2022-04-15 14:24:30 +00:00
<form class="user-add" @submit.prevent="submit" v-else data-testid="add-user-form">
<header>
<h1>Add New User</h1>
</header>
<div>
<div class="form-row">
<label>Name</label>
<input title="Name" type="text" name="name" v-model="newUser.name" required v-koel-focus>
</div>
<div class="form-row">
<label>Email</label>
<input title="Email" type="email" name="email" v-model="newUser.email" required>
</div>
<div class="form-row">
<label>Password</label>
<input
title="Password"
type="password"
name="password"
v-model="newUser.password"
autocomplete="new-password"
required
>
<p class="help">Min. 10 characters. Must be a mix of characters, numbers, and symbols.</p>
</div>
<div class="form-row">
<label>
<input type="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>
</div>
<footer>
2022-04-15 17:00:08 +00:00
<Btn class="btn-add" type="submit">Save</Btn>
<Btn class="btn-cancel" @click.prevent="maybeClose" white>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>
import { defineAsyncComponent, reactive, ref } from 'vue'
2022-04-15 14:24:30 +00:00
import { isEqual } from 'lodash'
import { CreateUserData, userStore } from '@/stores'
import { alerts, parseValidationError } from '@/utils'
2022-04-21 18:39:18 +00:00
const Btn = defineAsyncComponent(() => import('@/components/ui/Btn.vue'))
2022-04-15 17:00:08 +00:00
const SoundBar = defineAsyncComponent(() => import('@/components/ui/sound-bar.vue'))
const TooltipIcon = defineAsyncComponent(() => import('@/components/ui/tooltip-icon.vue'))
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
const loading = ref(false)
const emptyUserData = reactive<CreateUserData>({
name: '',
email: '',
password: '',
is_admin: false
})
const newUser = reactive<CreateUserData>({} as unknown as CreateUserData)
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
Object.assign(newUser, 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)
close()
} catch (err: any) {
const msg = err.response.status === 422 ? parseValidationError(err.response.data)[0] : 'Unknown error.'
alerts.error(msg)
} finally {
loading.value = false
}
}
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
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(newUser, emptyUserData)) {
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>