work on select user, delete user
This commit is contained in:
parent
36f0919aeb
commit
9eb695714b
@ -2,17 +2,26 @@
|
|||||||
<div>
|
<div>
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<h2 class="pb-4 pt-3">User Configuration</h2>
|
<h2 class="pb-4 pt-3">User Configuration</h2>
|
||||||
<div class="w-100" style="height: 43px">
|
<div class="row w-100" style="height: 43px">
|
||||||
<div class="float-end">
|
<div class="col-sm-2"></div>
|
||||||
<button
|
<div class="col-sm-4">
|
||||||
class="btn btn-primary"
|
<select class="form-select" v-model="selected" @change="onChange($event)">
|
||||||
title="Add new User"
|
<option v-for="item in users">{{ item.username }}</option>
|
||||||
data-tooltip="tooltip"
|
</select>
|
||||||
data-bs-toggle="modal"
|
</div>
|
||||||
data-bs-target="#userModal"
|
<div class="col px-0">
|
||||||
>
|
<div class="float-end">
|
||||||
Add User
|
<button
|
||||||
</button>
|
class="btn btn-primary me-2"
|
||||||
|
title="Add new User"
|
||||||
|
data-tooltip="tooltip"
|
||||||
|
data-bs-toggle="modal"
|
||||||
|
data-bs-target="#userModal"
|
||||||
|
>
|
||||||
|
Add User
|
||||||
|
</button>
|
||||||
|
<button class="btn btn-primary" @click="deleteUser()">Delete</button>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<form v-if="configStore.configUser" @submit.prevent="onSubmitUser">
|
<form v-if="configStore.configUser" @submit.prevent="onSubmitUser">
|
||||||
@ -24,7 +33,6 @@
|
|||||||
class="form-control"
|
class="form-control"
|
||||||
id="userName"
|
id="userName"
|
||||||
v-model="configStore.configUser.username"
|
v-model="configStore.configUser.username"
|
||||||
disabled
|
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@ -140,6 +148,8 @@ const indexStore = useIndex()
|
|||||||
|
|
||||||
const { $bootstrap } = useNuxtApp()
|
const { $bootstrap } = useNuxtApp()
|
||||||
|
|
||||||
|
const selected = ref(null)
|
||||||
|
const users = ref([] as User[])
|
||||||
const userModal = ref()
|
const userModal = ref()
|
||||||
const newPass = ref('')
|
const newPass = ref('')
|
||||||
const confirmPass = ref('')
|
const confirmPass = ref('')
|
||||||
@ -153,12 +163,74 @@ const user = ref({
|
|||||||
role_id: 2,
|
role_id: 2,
|
||||||
} as User)
|
} as User)
|
||||||
|
|
||||||
|
onMounted(() => {
|
||||||
|
getUsers()
|
||||||
|
})
|
||||||
|
|
||||||
|
async function getUsers() {
|
||||||
|
fetch('/api/users', {
|
||||||
|
method: 'GET',
|
||||||
|
headers: authStore.authHeader,
|
||||||
|
})
|
||||||
|
.then((response) => response.json())
|
||||||
|
.then((data) => {
|
||||||
|
users.value = data
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function onChange(event: any) {
|
||||||
|
selected.value = event.target.value
|
||||||
|
|
||||||
|
getUserConfig()
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getUserConfig() {
|
||||||
|
await fetch(`/api/user/${selected.value}`, {
|
||||||
|
method: 'GET',
|
||||||
|
headers: authStore.authHeader,
|
||||||
|
})
|
||||||
|
.then((response) => response.json())
|
||||||
|
.then((data) => {
|
||||||
|
configStore.configUser = data
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
async function deleteUser() {
|
||||||
|
if (configStore.configUser.username === configStore.currentUser) {
|
||||||
|
indexStore.alertVariant = 'alert-danger'
|
||||||
|
indexStore.alertMsg = 'Delete current user not possible!'
|
||||||
|
|
||||||
|
|
||||||
|
} else {
|
||||||
|
await fetch(`/api/user/${configStore.configUser.username}`, {
|
||||||
|
method: 'DELETE',
|
||||||
|
headers: authStore.authHeader,
|
||||||
|
}).then(async () => {
|
||||||
|
indexStore.alertVariant = 'alert-success'
|
||||||
|
indexStore.alertMsg = 'Delete user done!'
|
||||||
|
|
||||||
|
await configStore.getUserConfig()
|
||||||
|
await getUsers()
|
||||||
|
})
|
||||||
|
.catch((e) => {
|
||||||
|
indexStore.alertVariant = 'alert-danger'
|
||||||
|
indexStore.alertMsg = `Delete user error: ${e}`
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
indexStore.showAlert = true
|
||||||
|
|
||||||
|
setTimeout(() => {
|
||||||
|
indexStore.showAlert = false
|
||||||
|
}, 3000)
|
||||||
|
}
|
||||||
|
|
||||||
async function clearUser() {
|
async function clearUser() {
|
||||||
user.value.username = ''
|
user.value.username = ''
|
||||||
user.value.mail = ''
|
user.value.mail = ''
|
||||||
user.value.password = ''
|
user.value.password = ''
|
||||||
user.value.confirm = ''
|
user.value.confirm = ''
|
||||||
user.value.admin = false,
|
user.value.admin = false
|
||||||
user.value.role_id = 2
|
user.value.role_id = 2
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -182,6 +254,9 @@ async function addUser() {
|
|||||||
if (update.status === 200) {
|
if (update.status === 200) {
|
||||||
indexStore.alertVariant = 'alert-success'
|
indexStore.alertVariant = 'alert-success'
|
||||||
indexStore.alertMsg = 'Add user success!'
|
indexStore.alertMsg = 'Add user success!'
|
||||||
|
|
||||||
|
await getUsers()
|
||||||
|
await getUserConfig()
|
||||||
} else {
|
} else {
|
||||||
indexStore.alertVariant = 'alert-danger'
|
indexStore.alertVariant = 'alert-danger'
|
||||||
indexStore.alertMsg = 'Add user failed!'
|
indexStore.alertMsg = 'Add user failed!'
|
||||||
|
2
types/index.d.ts
vendored
2
types/index.d.ts
vendored
@ -13,7 +13,7 @@ declare global {
|
|||||||
|
|
||||||
interface User {
|
interface User {
|
||||||
username: String
|
username: String
|
||||||
mail: String
|
mail?: String
|
||||||
password?: String
|
password?: String
|
||||||
confirm?: String
|
confirm?: String
|
||||||
admin?: Boolean
|
admin?: Boolean
|
||||||
|
Loading…
x
Reference in New Issue
Block a user