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 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<User[]> {
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<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.
* @param {object} userToCreate