fix: #325 move moveCurrentLoginUserToFirstRow to store

This commit is contained in:
Cindy Chang
2024-08-09 09:45:00 +08:00
parent c37e46cff4
commit a29f3c6406
2 changed files with 15 additions and 21 deletions

View File

@@ -1,5 +1,6 @@
import { defineStore } from 'pinia'; import { defineStore } from 'pinia';
import apiError from '@/module/apiError'; import apiError from '@/module/apiError';
import piniaLoginStore from '@/stores/login.ts';
import { JUST_CREATE_ACCOUNT_HOT_DURATION_MINS } from '@/constants/constants'; import { JUST_CREATE_ACCOUNT_HOT_DURATION_MINS } from '@/constants/constants';
interface User { interface User {
@@ -22,6 +23,8 @@ interface EditDetail {
is_active: boolean; is_active: boolean;
} }
const loginStore = piniaLoginStore();
export default defineStore('acctMgmtStore', { export default defineStore('acctMgmtStore', {
state: () => ({ state: () => ({
allUserAccoutList: [] as User[], allUserAccoutList: [] as User[],
@@ -87,13 +90,13 @@ export default defineStore('acctMgmtStore', {
try { try {
const response = await this.$axios.get(apiGetUserList); const response = await this.$axios.get(apiGetUserList);
const customizedResponseData = await this.customizeAllUserList(response.data); const customizedResponseData = await this.customizeAllUserList(response.data);
this.allUserAccoutList = customizedResponseData; this.allUserAccoutList = await this.moveCurrentLoginUserToFirstRow(customizedResponseData);
} catch (error) { } catch (error) {
apiError(error, 'Failed to get all users.'); 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<User[]> { async customizeAllUserList(rawResponseData: User[]): Promise<User[]> {
return rawResponseData.map(user => ({ return rawResponseData.map(user => ({
@@ -103,6 +106,16 @@ export default defineStore('acctMgmtStore', {
isEditHovered: false, isEditHovered: false,
})); }));
}, },
/**
* Current logged in user should be placed at the first row on list
*/
async moveCurrentLoginUserToFirstRow(fetchedUserList: User[]): Promise<User[]> {
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. * Create new user in database.
* @param {object} userToCreate * @param {object} userToCreate

View File

@@ -172,19 +172,6 @@ export default {
loginUserData.value = loginStore.userData; 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 = () => { const moveJustCreateUserToFirstRow = () => {
if(infiniteAcctData.value && infiniteAcctData.value.length){ if(infiniteAcctData.value && infiniteAcctData.value.length){
const index = acctMgmtStore.allUserAccoutList.findIndex(user => user.username === acctMgmtStore.justCreateUsername); const index = acctMgmtStore.allUserAccoutList.findIndex(user => user.username === acctMgmtStore.justCreateUsername);
@@ -248,7 +235,6 @@ export default {
}; };
const onEditButtonClick = userNameToEdit => { const onEditButtonClick = userNameToEdit => {
console.log('onEditButtonClick userNameToEdit', userNameToEdit);
acctMgmtStore.setCurrentViewingUser(userNameToEdit); acctMgmtStore.setCurrentViewingUser(userNameToEdit);
modalStore.openModal(MODAL_ACCT_EDIT); modalStore.openModal(MODAL_ACCT_EDIT);
} }
@@ -269,7 +255,6 @@ export default {
// 當夾帶有infiniteStart.value就表示依然考慮到無限捲動的需求 // 當夾帶有infiniteStart.value就表示依然考慮到無限捲動的需求
infiniteAcctData.value = allAccountResponsive.value.slice(0, infiniteStart.value + ONCE_RENDER_NUM_OF_DATA); infiniteAcctData.value = allAccountResponsive.value.slice(0, infiniteStart.value + ONCE_RENDER_NUM_OF_DATA);
moveCurrentLoginUserToFirstRow();
moveJustCreateUserToFirstRow(); moveJustCreateUserToFirstRow();
} }
}); });
@@ -314,7 +299,6 @@ export default {
onMounted(async () => { onMounted(async () => {
await fetchLoginUserData(); await fetchLoginUserData();
await acctMgmtStore.getAllUserAccounts(); await acctMgmtStore.getAllUserAccounts();
moveCurrentLoginUserToFirstRow();
}); });
/** /**
@@ -439,9 +423,6 @@ export default {
onEditBtnClickVue2(clickedUserName){ onEditBtnClickVue2(clickedUserName){
this.setCurrentViewingUser(clickedUserName); this.setCurrentViewingUser(clickedUserName);
this.openModal(MODAL_ACCT_EDIT); this.openModal(MODAL_ACCT_EDIT);
},
moveCurrentLoginUserToFirstRow(){
}, },
...mapActions(useModalStore, ['openModal']), ...mapActions(useModalStore, ['openModal']),
...mapActions(useAcctMgmtStore, [ ...mapActions(useAcctMgmtStore, [