From 00d086ff1d0b9414597bcd2772a3a8f6d17e9e4c Mon Sep 17 00:00:00 2001 From: Cindy Chang Date: Mon, 24 Jun 2024 10:09:39 +0800 Subject: [PATCH] delete account and later on modal closing effect. --- src/i18n/en.json | 3 ++- src/stores/acctMgmt.js | 25 ++++++++++++++++++- src/stores/modal.js | 5 +++- src/utils/timeUtil.js | 4 +++ .../AccountManagement/AccountAdmin/index.vue | 1 + .../AccountManagement/ModalAccountEdit.vue | 7 ------ .../AccountManagement/ModalDeleteAlert.vue | 17 ++++++++++++- 7 files changed, 51 insertions(+), 11 deletions(-) create mode 100644 src/utils/timeUtil.js diff --git a/src/i18n/en.json b/src/i18n/en.json index e39e399..a893908 100644 --- a/src/i18n/en.json +++ b/src/i18n/en.json @@ -35,7 +35,8 @@ "DelteQuestion": "Are you sure to delete ?", "DeleteFirstClause": "deletion is irreversible !", "DeleteSecondClause": "You will delete all data and content on this account.", - "MsgAccountAdded": "Account added." + "MsgAccountAdded": "Account added.", + "MsgAccountDeleteSuccess": "Account deleted." }, "Compare": { "timeUsage": "Time Usage", diff --git a/src/stores/acctMgmt.js b/src/stores/acctMgmt.js index 0ac69e7..940c4e5 100644 --- a/src/stores/acctMgmt.js +++ b/src/stores/acctMgmt.js @@ -7,7 +7,10 @@ export default defineStore('acctMgmtStore', { isAcctMenuOpen: false, currentViewingUser: { detail: null, - } + }, + response: { + deleteAccount: null, + }, }), getters: { }, @@ -92,6 +95,26 @@ export default defineStore('acctMgmtStore', { apiError(error, 'Failed to add a new account.'); }; }, + /** + * Delete an account from database. + * @param {string} userToDelete this value is unique in database. + * @returns the result of whether the deletion is success or not. + */ + async deleteAccount(userToDelete) { + const apiDelete = `/api/users/${userToDelete}`; + + try{ + const response = await this.$axios.delete(apiDelete); + if(response.status === 200) { + return true; + } + } + catch(error) { + console.log('error', error, error.status, error.code); + apiError(error, 'Failed to delete the account.'); + return false; + }; + }, /** * Get user detail by unique username. * @param {string} uniqueUsername diff --git a/src/stores/modal.js b/src/stores/modal.js index 491f88e..71bb056 100644 --- a/src/stores/modal.js +++ b/src/stores/modal.js @@ -1,5 +1,7 @@ import { defineStore } from 'pinia'; import { MODAL_ACCT_INFO, } from '@/constants/constants.js'; +import { delaySecond } from "@/utils/timeUtil.js"; + export const useModalStore = defineStore('modalStore', { state: () => ({ isModalOpen: false, @@ -10,7 +12,8 @@ export const useModalStore = defineStore('modalStore', { this.isModalOpen = true; this.whichModal = whichModal; }, - closeModal() { + async closeModal(){ + await delaySecond(2); this.isModalOpen = false; }, }, diff --git a/src/utils/timeUtil.js b/src/utils/timeUtil.js new file mode 100644 index 0000000..08aade8 --- /dev/null +++ b/src/utils/timeUtil.js @@ -0,0 +1,4 @@ +export const delaySecond = (s = 2.5) => { + return new Promise((resolve) => + setTimeout(resolve, s * 1000) +);} \ No newline at end of file diff --git a/src/views/AccountManagement/AccountAdmin/index.vue b/src/views/AccountManagement/AccountAdmin/index.vue index 178df5a..c0ad7e4 100644 --- a/src/views/AccountManagement/AccountAdmin/index.vue +++ b/src/views/AccountManagement/AccountAdmin/index.vue @@ -219,6 +219,7 @@ export default { }; const onDeleteBtnClick = (usernameToDelete) => { + acctMgmtStore.setCurrentViewingUser(usernameToDelete); modalStore.openModal(MODAL_DELETE); }; diff --git a/src/views/AccountManagement/ModalAccountEdit.vue b/src/views/AccountManagement/ModalAccountEdit.vue index 7285c09..52eb825 100644 --- a/src/views/AccountManagement/ModalAccountEdit.vue +++ b/src/views/AccountManagement/ModalAccountEdit.vue @@ -199,13 +199,6 @@ export default defineComponent({ } //TODO: 這邊要記得回來加一個判斷帳號是否已經存在的邏輯 checkAccountIsUnique(); - console.log('input content to feed in', - inputUserAccount.value, - inputPwd.value, - inputName.value, - isSetAsAdminChecked.value, - isSetActivedChecked.value, - ); await acctMgmtStore.createNewAccount({ username: inputUserAccount.value, password: inputPwd.value, diff --git a/src/views/AccountManagement/ModalDeleteAlert.vue b/src/views/AccountManagement/ModalDeleteAlert.vue index 6f38587..f83369b 100644 --- a/src/views/AccountManagement/ModalDeleteAlert.vue +++ b/src/views/AccountManagement/ModalDeleteAlert.vue @@ -18,7 +18,9 @@ {{ i18next.t("No") }} @@ -28,13 +30,25 @@