#334 fixed v-if=isAdmin

This commit is contained in:
Cindy Chang
2024-08-07 16:29:15 +08:00
parent caa1b9cc70
commit 8fa1a7b8b3
3 changed files with 39 additions and 20 deletions

View File

@@ -7,7 +7,7 @@
</div> </div>
<ul class="w-full min-h-10"> <ul class="w-full min-h-10">
<!-- 這裡不使用迴圈是因為src值用變數的話會沒辦法顯示svg --> <!-- 這裡不使用迴圈是因為src值用變數的話會沒辦法顯示svg -->
<li id="btn_acct_mgmt" <li v-if="isAdmin" id="btn_acct_mgmt"
class="w-full h-[40px] flex py-2 px-4 hover:text-[#000000] hover:bg-[#F1F5F9] cursor-pointer class="w-full h-[40px] flex py-2 px-4 hover:text-[#000000] hover:bg-[#F1F5F9] cursor-pointer
items-center" @click="onBtnAcctMgmtClick"> items-center" @click="onBtnAcctMgmtClick">
<span class="w-[24px] h-[24px] flex"><img src="@/assets/icon-crown.svg" alt="accountManagement"></span> <span class="w-[24px] h-[24px] flex"><img src="@/assets/icon-crown.svg" alt="accountManagement"></span>
@@ -30,7 +30,7 @@
</template> </template>
<script> <script>
import { computed, } from 'vue'; import { computed, onMounted, ref, } from 'vue';
import { mapActions, mapState, storeToRefs } from 'pinia'; import { mapActions, mapState, storeToRefs } from 'pinia';
import i18next from '@/i18n/i18n'; import i18next from '@/i18n/i18n';
import LoginStore from '@/stores/login.ts'; import LoginStore from '@/stores/login.ts';
@@ -42,6 +42,7 @@ import { leaveFilter, leaveConformance } from '@/module/alertModal.js';
export default { export default {
setup() { setup() {
const { logOut } = LoginStore(); const { logOut } = LoginStore();
const loginStore = LoginStore();
const allMapDataStore = AllMapDataStore(); const allMapDataStore = AllMapDataStore();
const conformanceStore = ConformanceStore(); const conformanceStore = ConformanceStore();
const acctMgmtStore = AcctMgmtStore(); const acctMgmtStore = AcctMgmtStore();
@@ -49,11 +50,31 @@ export default {
const { conformanceLogTempCheckId } = storeToRefs(conformanceStore); const { conformanceLogTempCheckId } = storeToRefs(conformanceStore);
const isAcctMenuOpen = computed(() => acctMgmtStore.isAcctMenuOpen); const isAcctMenuOpen = computed(() => acctMgmtStore.isAcctMenuOpen);
const loginUserData = ref(null);
const currentViewingUserDetail = computed(() => acctMgmtStore.currentViewingUser.detail);
const isAdmin = ref(false);
return { logOut, tempFilterId, const getIsAdminValue = async () => {
await loginStore.getUserData();
loginUserData.value = loginStore.userData;
await acctMgmtStore.getUserDetail(loginUserData.value.username);
if(currentViewingUserDetail.value.roles.find(role => role.code === 'admin')) {
isAdmin.value = true;
}
};
onMounted(async () => {
await getIsAdminValue();
});
return {
logOut,
tempFilterId,
conformanceLogTempCheckId, conformanceLogTempCheckId,
allMapDataStore, conformanceStore, allMapDataStore,
conformanceStore,
isAcctMenuOpen, isAcctMenuOpen,
isAdmin,
}; };
}, },
data() { data() {

View File

@@ -41,7 +41,6 @@ export default {
const { conformanceLogTempCheckId, conformanceFilterTempCheckId, conformanceFileName } = storeToRefs(conformanceStore); const { conformanceLogTempCheckId, conformanceFilterTempCheckId, conformanceFileName } = storeToRefs(conformanceStore);
const toggleIsAcctMenuOpen = () => { const toggleIsAcctMenuOpen = () => {
console.log('head clicked', );
acctMgmtStore.toggleIsAcctMenuOpen(); acctMgmtStore.toggleIsAcctMenuOpen();
} }

View File

@@ -28,7 +28,7 @@ export default defineStore('acctMgmtStore', {
isAcctMenuOpen: false, isAcctMenuOpen: false,
currentViewingUser: { currentViewingUser: {
username: '', username: '',
detail: {} as EditDetail, detail: {},
} as User, } as User,
response: { response: {
deleteAccount: null, deleteAccount: null,
@@ -42,13 +42,13 @@ export default defineStore('acctMgmtStore', {
/** /**
* Set related boolean to true * Set related boolean to true
*/ */
openAcctMenu(){ openAcctMenu() {
this.isAcctMenuOpen = true; this.isAcctMenuOpen = true;
}, },
/** /**
* Set related boolean to false * Set related boolean to false
*/ */
closeAcctMenu(){ closeAcctMenu() {
this.isAcctMenuOpen = false; this.isAcctMenuOpen = false;
}, },
toggleIsAcctMenuOpen() { toggleIsAcctMenuOpen() {
@@ -59,8 +59,7 @@ export default defineStore('acctMgmtStore', {
* @param {string} username * @param {string} username
*/ */
setCurrentViewingUser(username: string) { setCurrentViewingUser(username: string) {
const userFind:User|undefined = this.allUserAccoutList.find(user => user.username === username); const userFind: User | undefined = this.allUserAccoutList.find(user => user.username === username);
console.log('userFind', userFind);
this.currentViewingUser = userFind || { username: '', detail: {} }; this.currentViewingUser = userFind || { username: '', detail: {} };
}, },
/** /**
@@ -88,7 +87,7 @@ export default defineStore('acctMgmtStore', {
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 = customizedResponseData;
} catch(error) { } catch (error) {
apiError(error, 'Failed to get all users.'); apiError(error, 'Failed to get all users.');
} }
}, },
@@ -124,7 +123,7 @@ export default defineStore('acctMgmtStore', {
this.justCreateUsername = userToCreate.username; this.justCreateUsername = userToCreate.username;
setTimeout(this.resetJustCreateFlag, JUST_CREATE_ACCOUNT_HOT_DURATION_MINS * 1000 * 60); setTimeout(this.resetJustCreateFlag, JUST_CREATE_ACCOUNT_HOT_DURATION_MINS * 1000 * 60);
} }
} catch(error) { } catch (error) {
apiError(error, 'Failed to add a new account.'); apiError(error, 'Failed to add a new account.');
} }
}, },
@@ -139,7 +138,7 @@ export default defineStore('acctMgmtStore', {
try { try {
const response = await this.$axios.delete(apiDelete); const response = await this.$axios.delete(apiDelete);
return response.status === 200; return response.status === 200;
} catch(error) { } catch (error) {
apiError(error, 'Failed to delete the account.'); apiError(error, 'Failed to delete the account.');
return false; return false;
} }
@@ -160,7 +159,7 @@ export default defineStore('acctMgmtStore', {
is_active: editDetail.is_active, is_active: editDetail.is_active,
}); });
return response.status === 200; return response.status === 200;
} catch(error) { } catch (error) {
apiError(error, 'Failed to edit the account.'); apiError(error, 'Failed to edit the account.');
return false; return false;
} }
@@ -175,7 +174,7 @@ export default defineStore('acctMgmtStore', {
try { try {
const response = await this.$axios.put(apiAddRole); const response = await this.$axios.put(apiAddRole);
return response.status === 200; return response.status === 200;
} catch(error) { } catch (error) {
apiError(error, 'Failed to add role to the account.'); apiError(error, 'Failed to add role to the account.');
return false; return false;
} }
@@ -190,7 +189,7 @@ export default defineStore('acctMgmtStore', {
try { try {
const response = await this.$axios.delete(apiDeleteRole); const response = await this.$axios.delete(apiDeleteRole);
return response.status === 200; return response.status === 200;
} catch(error) { } catch (error) {
apiError(error, 'Failed to delete a role frome the account.'); apiError(error, 'Failed to delete a role frome the account.');
return false; return false;
} }
@@ -209,7 +208,7 @@ export default defineStore('acctMgmtStore', {
detail: response.data, detail: response.data,
}; };
return response.status === 200; return response.status === 200;
} catch(error) { } catch (error) {
//不需要跳出錯誤,因為如果是錯誤反而是好事,表示帳號是獨一的 //不需要跳出錯誤,因為如果是錯誤反而是好事,表示帳號是獨一的
return false; return false;
} }
@@ -261,7 +260,7 @@ export default defineStore('acctMgmtStore', {
/** /**
* Reset isOneAccountJustCreate to false, causing the badge to disappear. * Reset isOneAccountJustCreate to false, causing the badge to disappear.
*/ */
resetJustCreateFlag(){ resetJustCreateFlag() {
this.isOneAccountJustCreate = false; this.isOneAccountJustCreate = false;
}, },
/** Set the value of shouldUpdateList variable. /** Set the value of shouldUpdateList variable.