From a29f3c6406658904b94151a5988377b629fdf962 Mon Sep 17 00:00:00 2001 From: Cindy Chang Date: Fri, 9 Aug 2024 09:45:00 +0800 Subject: [PATCH] fix: #325 move moveCurrentLoginUserToFirstRow to store --- src/stores/acctMgmt.ts | 17 +++++++++++++++-- .../AccountAdmin/AccountAdmin.vue | 19 ------------------- 2 files changed, 15 insertions(+), 21 deletions(-) diff --git a/src/stores/acctMgmt.ts b/src/stores/acctMgmt.ts index 48c2473..883ae59 100644 --- a/src/stores/acctMgmt.ts +++ b/src/stores/acctMgmt.ts @@ -1,5 +1,6 @@ import { defineStore } from 'pinia'; import apiError from '@/module/apiError'; +import piniaLoginStore from '@/stores/login.ts'; import { JUST_CREATE_ACCOUNT_HOT_DURATION_MINS } from '@/constants/constants'; interface User { @@ -22,6 +23,8 @@ interface EditDetail { is_active: boolean; } +const loginStore = piniaLoginStore(); + export default defineStore('acctMgmtStore', { state: () => ({ allUserAccoutList: [] as User[], @@ -87,13 +90,13 @@ export default defineStore('acctMgmtStore', { try { const response = await this.$axios.get(apiGetUserList); const customizedResponseData = await this.customizeAllUserList(response.data); - this.allUserAccoutList = customizedResponseData; + this.allUserAccoutList = await this.moveCurrentLoginUserToFirstRow(customizedResponseData); } catch (error) { apiError(error, 'Failed to get all users.'); } }, /** - * For example, add isHovered field + * Add some customization. For example, add isHovered field */ async customizeAllUserList(rawResponseData: User[]): Promise { return rawResponseData.map(user => ({ @@ -103,6 +106,16 @@ export default defineStore('acctMgmtStore', { isEditHovered: false, })); }, + /** + * Current logged in user should be placed at the first row on list + */ + async moveCurrentLoginUserToFirstRow(fetchedUserList: User[]): Promise { + await loginStore.getUserData(); + const loginUserData:User = loginStore.userData; + const foundLoginUserIndex = fetchedUserList.findIndex(user => user.username === loginUserData.username); + fetchedUserList.unshift(fetchedUserList.splice(foundLoginUserIndex, 1)[0]); + return fetchedUserList; + }, /** * Create new user in database. * @param {object} userToCreate diff --git a/src/views/AccountManagement/AccountAdmin/AccountAdmin.vue b/src/views/AccountManagement/AccountAdmin/AccountAdmin.vue index 3f381bd..abf8ed6 100644 --- a/src/views/AccountManagement/AccountAdmin/AccountAdmin.vue +++ b/src/views/AccountManagement/AccountAdmin/AccountAdmin.vue @@ -172,19 +172,6 @@ export default { loginUserData.value = loginStore.userData; }; - const moveCurrentLoginUserToFirstRow = () => { - if(infiniteAcctData.value && infiniteAcctData.value.length){ - const loginUser = acctMgmtStore.allUserAccoutList.find(user => user.username === loginUserData.value.username); - const index = infiniteAcctData.value.findIndex(user => user.username=== loginUserData.value.username); - if(loginUser){ - infiniteAcctData.value.unshift(infiniteAcctData.value.splice(index, 1)[0]); - console.log('第一個infiniteAcctData.value[0]', infiniteAcctData.value[0], 'loginUserData.value.username', loginUserData.value.username); - console.log('infiniteAcctData.value[index]',infiniteAcctData.value[index] ); - } - } - - }; - const moveJustCreateUserToFirstRow = () => { if(infiniteAcctData.value && infiniteAcctData.value.length){ const index = acctMgmtStore.allUserAccoutList.findIndex(user => user.username === acctMgmtStore.justCreateUsername); @@ -248,7 +235,6 @@ export default { }; const onEditButtonClick = userNameToEdit => { - console.log('onEditButtonClick userNameToEdit', userNameToEdit); acctMgmtStore.setCurrentViewingUser(userNameToEdit); modalStore.openModal(MODAL_ACCT_EDIT); } @@ -269,7 +255,6 @@ export default { // 當夾帶有infiniteStart.value,就表示依然考慮到無限捲動的需求 infiniteAcctData.value = allAccountResponsive.value.slice(0, infiniteStart.value + ONCE_RENDER_NUM_OF_DATA); - moveCurrentLoginUserToFirstRow(); moveJustCreateUserToFirstRow(); } }); @@ -314,7 +299,6 @@ export default { onMounted(async () => { await fetchLoginUserData(); await acctMgmtStore.getAllUserAccounts(); - moveCurrentLoginUserToFirstRow(); }); /** @@ -439,9 +423,6 @@ export default { onEditBtnClickVue2(clickedUserName){ this.setCurrentViewingUser(clickedUserName); this.openModal(MODAL_ACCT_EDIT); - }, - moveCurrentLoginUserToFirstRow(){ - }, ...mapActions(useModalStore, ['openModal']), ...mapActions(useAcctMgmtStore, [