<div
class="col-12 col-lg-2 d-flex flex-column lh-1 justify-content-center"
>
<span class="text-muted fs-7 d-block pb-1">Middle name</span>
<span class="fw-medium">{{ user.middleName }}</span>
</div>
<div
class="col-12 col-lg-2 d-flex flex-column lh-1 justify-content-center"
>
<span class="text-muted fs-7 d-block pb-1">Surname</span>
<span class="fw-medium">{{ user.surname }}</span>
</div>
<div
class="col-12 col-lg-2 d-flex flex-column lh-1 justify-content-center"
>
<span class="text-muted fs-7 d-block pb-1">Role</span>
<span class="fw-medium">{{ UserRole[user.role] }}</span>
</div>
<div
class="col-12 col-lg-2 d-flex flex-column lh-1 justify-content-center"
>
<span class="text-muted fs-7 d-block pb-1">E-mail</span>
<span class="fw-medium">{{ user.email }}</span>
</div>
<div class="col d-flex justify-content-end">
<button
v-if="
(currentUser.role === 'SUPER_ADMIN' ||
currentUser.role !== 'SUPER_ADMIN') &&
(currentUser.role === 'SUPER_ADMIN' ||
(currentUser.role === 'ADMIN' &&
(user.role === 'ADMIN' ||
user.role === 'STAFF_MEMBER' ||
user.role === 'MANAGER')))
"
type="button"
class="btn btn-success text-light d-flex float-start px-2 me-2"
data-bs-toggle="modal"
data-bs-target="#userModal"
data-bs-trigger="hover focus"
data-bs-content="Edit user"
@click="loadUser(user.id)"
>
<span class="fs-5 d-flex material-icons"> edit </span>
</button>
<button
v-if="
(currentUser.role === 'SUPER_ADMIN' ||
currentUser.role !== 'SUPER_ADMIN') &&
(currentUser.role === 'SUPER_ADMIN' ||
(currentUser.role === 'ADMIN' &&
user.role !== 'SUPER_ADMIN' &&
(user.role !== 'ADMIN' || currentUser.id !== user.id)))
"
type="button"
class="btn btn-danger d-flex float-start px-2"
data-bs-toggle="modal"
data-bs-target="#userDeleteModal"
data-bs-trigger="hover focus"
data-bs-content="Delete item"
@click="deleteUser(`${user.name} ${user.middleName}`, user.id)"
>
<span class="fs-5 d-flex material-icons"> delete </span>
</button>
</div>
</div> this is where i show the list of users and currently the logged in user is shown at the top but its also being shown in the list again and i need to exclude it from the list
Assistant
To exclude the currently logged in user from the list, you can add another condition in the v-if directive that checks for the user's ID. Here's an updated version of the code:
In the updated code, the condition currentUser.id !== user.id is added to exclude the currently logged in user from the loop. Make sure currentUser.id and user.id are representing the unique identifiers of each user. Adjust them accordingly based on your data structure.
User
[Vue warn]: Property "user" was accessed during render but is not defined on instance. 7:34:13 PM
[nuxt] [request error] [unhandled] [500] Cannot read properties of undefined (reading 'id') 7:34:13 PM
at _sfc_ssrRender (C:\Users\robik\Desktop@soft-dev\bb6-inventory-web\components\settings\UserList.vue:181:151)
now it throws this
Assistant
Based on the error message you provided, it seems that the issue lies in accessing the user property within the loop. To resolve this, you can add another condition to check if the user object exists before trying to access its properties. Here's an updated version of the code:
By adding the condition user before using currentUser.id !== user.id, you ensure that the user object exists before trying to access its properties. This should prevent the error from occurring.
User
now only the current user at the top is shown the list of users is not being shown
Assistant
I apologize for the confusion. It seems that the condition in the v-if directive is causing the issue. Currently, the condition currentUser && (currentUser.role === 'SUPER_ADMIN' || currentUser.role === 'ADMIN') && user && currentUser.id !== user.id is checking for both the currentUser and the user to meet certain conditions.
To resolve the issue and show the list of users while excluding the current user, you can modify the v-if condition. Here's an updated version of the code:
In the updated code, the condition user.id !== currentUser.id is used to exclude the current user from the loop. This checks if the IDs of the current user and the user in the loop are not equal to each other. Adjust the properties user.id and currentUser.id based on your actual data structure.
Now the list of users should be displayed, excluding the current user.
User
now it crashed like before
Assistant
I apologize for the confusion. It seems that the issue may lie with how the currentUser property is being accessed within the template.
To resolve this issue, you can bind the currentUser property to the component instance using v-bind, and then access it within the template using the bound property instead.
Here's an updated version of the code:
html
<template>
<div>
...
<div
v-for="(user, index) in userList.results"
v-if="
currentUser &&
(currentUser.role === 'SUPER_ADMIN' || currentUser.role === 'ADMIN') &&
user.id !== currentUser.id
"
:key="index"
:class="{
...
}"
>
<!-- User details -->
</div>
...
</div>
</template>
<script>
export default {
...
computed: {
currentUser() {
return this.$store.state.currentUser; // Replace this with your actual store property for the current user
},
userList() {
return this.$store.state.userList; // Replace this with your actual store property for the user list
},
},
...
};
</script>
In the script section, the currentUser property is now accessed from the Vuex store (replace it with your actual store property for the current user). The userList property is also accessed from the store.
Please make sure to replace this.$store.state.currentUser and this.$store.state.userList with the appropriate property names from your Vuex store.
By making these changes, the current user will be correctly accessed within the template, and the list of users will be displayed, excluding the current user.
User
Assistant
Thank you for providing the updated script. Based on the provided script, you can modify the v-if condition in the template to exclude the current user from the user list. Here's an updated version of the code:
In this updated code, the user.id !== currentUser.id condition is used to exclude the current user from the user list.
Please make sure that the currentUser object is correctly obtained from the auth composable and userList is populated with the correct data from the API response.
With these changes, the user list should be displayed, excluding the current user.
User
Assistant
To restrict certain actions like editing email, role, surname, middle name, and first name to admin and super admin users only, you can update the v-if condition for the edit button. Here's an updated version of the code:
In this updated code, the condition (currentUser.role === 'SUPER_ADMIN' || currentUser.role === 'ADMIN') ensures that only admin and super admin users can see the edit button. Additionally, the condition (user.role === 'ADMIN' || user.role === 'STAFF_MEMBER' || user.role === 'MANAGER') restricts the edit button to users with the roles 'ADMIN', 'STAFF_MEMBER', or 'MANAGER'. Adjust these conditions based on your specific user roles.
By making these changes, only admin and super admin users with the appropriate roles will see the edit button for email, role, surname, middle name, and first name changes. Other users will not have access to these fields and can only change their password in their own data.