From 9b2bab9124a5f3a66e3f6f6796d8b8256bbf95a7 Mon Sep 17 00:00:00 2001 From: Cindy Chang Date: Thu, 27 Jun 2024 14:21:27 +0800 Subject: [PATCH] feature: admin role edit API --- src/components/Header.vue | 2 +- src/stores/acctMgmt.js | 36 +++++++++++++++++++ .../AccountAdmin/AccountAdmin.vue | 31 ++++++++++++---- 3 files changed, 62 insertions(+), 7 deletions(-) diff --git a/src/components/Header.vue b/src/components/Header.vue index aa14a46..362e194 100644 --- a/src/components/Header.vue +++ b/src/components/Header.vue @@ -10,7 +10,7 @@ - diff --git a/src/stores/acctMgmt.js b/src/stores/acctMgmt.js index fc64828..099eff1 100644 --- a/src/stores/acctMgmt.js +++ b/src/stores/acctMgmt.js @@ -147,6 +147,42 @@ export default defineStore('acctMgmtStore', { return false; }; }, + /** Add a role to the user in database. + * @param {string} usernameToEdit + * @param {string} roleCode + */ + async addRoleToUser(usernameToEdit, roleCode) { + const apiAddRole = `/api/users/${usernameToEdit}/roles/${roleCode}`; + + try{ + const response = await this.$axios.put(apiAddRole); + if(response.status === 200) { + return true; + } + } + catch(error) { + apiError(error, 'Failed to add role to the account.'); + return false; + }; + }, + /** Delete a role from the user in database. + * @param {string} usernameToEdit + * @param {string} roleCode + */ + async deleteRoleToUser(usernameToEdit, roleCode) { + const apiDeleteRole = `/api/users/${usernameToEdit}/roles/${roleCode}`; + + try{ + const response = await this.$axios.delete(apiDeleteRole); + if(response.status === 200) { + return true; + } + } + catch(error) { + apiError(error, 'Failed to delete a role frome the account.'); + return false; + }; + }, /** * Get user detail by unique username. * @param {string} uniqueUsername diff --git a/src/views/AccountManagement/AccountAdmin/AccountAdmin.vue b/src/views/AccountManagement/AccountAdmin/AccountAdmin.vue index 8b7b5cf..9371c9d 100644 --- a/src/views/AccountManagement/AccountAdmin/AccountAdmin.vue +++ b/src/views/AccountManagement/AccountAdmin/AccountAdmin.vue @@ -42,10 +42,10 @@
Radio On Radio Off
@@ -284,6 +284,28 @@ export default { toast.success(i18next.t("AcctMgmt.MsgAccountEdited")); } + const onAdminInputClick = async(userData, inputIsAdminOn) => { + const ADMIN_ROLE_NAME = 'admin'; + switch(inputIsAdminOn) { + case true: + await acctMgmtStore.addRoleToUser(userData.username, ADMIN_ROLE_NAME); + break; + case false: + await acctMgmtStore.deleteRoleToUser(userData.username, ADMIN_ROLE_NAME); + break; + default: + break; + } + const userDataToReplace = { + username: userData.username, + name: userData.name, + is_admin: inputIsAdminOn, + }; + + acctMgmtStore.updateSingleAccountPiniaState(userDataToReplace); + toast.success(i18next.t("AcctMgmt.MsgAccountEdited")); + } + onMounted(async () => { await fetchLoginUserData(); await acctMgmtStore.getAllUserAccounts(); @@ -339,6 +361,7 @@ export default { handleScroll, getRowClass, onDeleteBtnClick, + onAdminInputClick, handleDeleteMouseOver, handleDeleteMouseOut, handleRowMouseOver, @@ -373,10 +396,6 @@ export default { ...mapState(useAcctMgmtStore, ['allUserAccoutList']), }, methods: { - onAdminRightsBtnClick(isOn){ - - }, - /** * 無限滾動: 監聽 scroll 有沒有滾到底部 * @param {element} event 滾動傳入的事件