feature: just create badge. Important thing is the await keyword

This commit is contained in:
Cindy Chang
2024-06-26 11:46:53 +08:00
parent 353eecad81
commit 965aaeb097
8 changed files with 79 additions and 28 deletions

View File

@@ -1,5 +1,6 @@
import { defineStore } from "pinia";
import apiError from '@/module/apiError.js';
import { JUST_CREATE_ACCOUNT_HOT_DURATION_MINS } from '@/constants/constants.js';
export default defineStore('acctMgmtStore', {
state: () => ({
@@ -12,6 +13,9 @@ export default defineStore('acctMgmtStore', {
response: {
deleteAccount: null,
},
isOneAccountJustCreate: false, //如果有一個帳號剛剛建立,則會在列表上的第一列顯示這個帳號,並且右置一個徽章
justCreateUsername: "", // unique username
shouldUpdateList: false, // 控制是否該刷新列表
}),
getters: {
},
@@ -90,6 +94,11 @@ export default defineStore('acctMgmtStore', {
try{
const response = await this.$axios.post(apiCreateAccount, userToCreate);
if(response.status === 200) {
this.isOneAccountJustCreate = true;
this.justCreateUsername = userToCreate.username;
setTimeout(this.resetJustCreateFlag, JUST_CREATE_ACCOUNT_HOT_DURATION_MINS * 1000 * 60);
}
}
catch(error) {
apiError(error, 'Failed to add a new account.');
@@ -132,7 +141,7 @@ export default defineStore('acctMgmtStore', {
}
}
catch(error) {
apiError(error, 'Failed to get user detail.');
//不需要跳出錯誤,因為如果是錯誤反而是好事,表示帳號是獨一的
return false;
};
},
@@ -180,5 +189,17 @@ export default defineStore('acctMgmtStore', {
userToChange.isDetailHovered = isDetailHovered;
}
},
/**
* Reset isOneAccountJustCreate to false, causing the badge to disappear.
*/
resetJustCreateFlag(){
this.isOneAccountJustCreate = false;
},
/** Set the value of shouldUpdateList variable.
* @param {boolean} shouldUpdateBoolean
*/
setShouldUpdateList(shouldUpdateBoolean) {
this.shouldUpdateList = shouldUpdateBoolean;
},
},
})