From 9eb695714bdb487dc515f9ddbaf49aa6e21a7f3f Mon Sep 17 00:00:00 2001 From: jb-alvarado Date: Thu, 23 Nov 2023 22:37:35 +0100 Subject: [PATCH] work on select user, delete user --- components/UserConfig.vue | 101 +++++++++++++++++++++++++++++++++----- types/index.d.ts | 2 +- 2 files changed, 89 insertions(+), 14 deletions(-) diff --git a/components/UserConfig.vue b/components/UserConfig.vue index 43602244..482862a4 100644 --- a/components/UserConfig.vue +++ b/components/UserConfig.vue @@ -2,17 +2,26 @@

User Configuration

-
-
- +
+
+
+ +
+
+
+ + +
@@ -24,7 +33,6 @@ class="form-control" id="userName" v-model="configStore.configUser.username" - disabled />
@@ -140,6 +148,8 @@ const indexStore = useIndex() const { $bootstrap } = useNuxtApp() +const selected = ref(null) +const users = ref([] as User[]) const userModal = ref() const newPass = ref('') const confirmPass = ref('') @@ -153,12 +163,74 @@ const user = ref({ role_id: 2, } 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() { user.value.username = '' user.value.mail = '' user.value.password = '' user.value.confirm = '' - user.value.admin = false, + user.value.admin = false user.value.role_id = 2 } @@ -182,6 +254,9 @@ async function addUser() { if (update.status === 200) { indexStore.alertVariant = 'alert-success' indexStore.alertMsg = 'Add user success!' + + await getUsers() + await getUserConfig() } else { indexStore.alertVariant = 'alert-danger' indexStore.alertMsg = 'Add user failed!' diff --git a/types/index.d.ts b/types/index.d.ts index 492c5ba3..cf221e45 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -13,7 +13,7 @@ declare global { interface User { username: String - mail: String + mail?: String password?: String confirm?: String admin?: Boolean