Skip to content

Commit

Permalink
fixup! AIMAAS #197: refactor users/groups state management
Browse files Browse the repository at this point in the history
  • Loading branch information
jonas-brr committed Apr 3, 2024
1 parent 28a588d commit c26d147
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 71 deletions.
12 changes: 6 additions & 6 deletions frontend/src/components/Schema.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
</template>
<script>
import { shallowRef } from "vue";
import { markRaw } from "vue";
import BaseLayout from "@/components/layout/BaseLayout";
import EntityList from "@/components/EntityList";
import EntityForm from "@/components/inputs/EntityForm";
Expand All @@ -33,31 +33,31 @@ export default {
tabs: [
{
name: "Entities",
component: shallowRef(EntityList),
component: markRaw(EntityList),
icon: "table_view",
tooltip: "Show entities"
},
{
name: "Edit / Show",
component: shallowRef(SchemaEdit),
component: markRaw(SchemaEdit),
icon: "mode_edit",
tooltip: "Edit/Show schema structure"
},
{
name: "Add Entity",
component: shallowRef(EntityForm),
component: markRaw(EntityForm),
icon: 'add_circle',
tooltip: 'Add new entity'
},
{
name: "Permissions",
component: shallowRef(PermissionList),
component: markRaw(PermissionList),
icon: "security",
tooltip: "Manage permissions on the schema"
},
{
name: "History",
component: shallowRef(Changes),
component: markRaw(Changes),
icon: 'history',
tooltip: 'Change history of schema'
}
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/components/SearchPanel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
</template>

<script>
import {shallowRef} from "vue";
import { markRaw } from "vue";
import {TYPE_INPUT_MAP} from "@/utils";
import IntegerInput from "@/components/inputs/IntegerInput.vue";
import FloatInput from "@/components/inputs/FloatInput.vue";
Expand Down Expand Up @@ -148,7 +148,7 @@ export default {
operator: null,
value: null,
operatorOptions: [],
component: shallowRef(TextInput)
component: markRaw(TextInput)
});
},
clearFilters() {
Expand Down
29 changes: 16 additions & 13 deletions frontend/src/components/auth/GroupManager.vue
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@
<div v-if="activeGroup" class="d-flex justify-content-between">
<h4 class="">{{ activeGroup.name }}</h4>
<ConfirmButton
:callback="() => { deleteGroup(activeGroup.id); activeGroup = undefined }"
:callback="
() => {
deleteGroup(activeGroup.id);
activeGroup = undefined;
}
"
btnClass="btn-outline-danger order-last"
:reverse="true"
>
Expand All @@ -54,7 +59,8 @@ import ModalDialog from "@/components/layout/ModalDialog";
import PermissionList from "@/components/auth/PermissionList";
import { useAuthStore } from "../../store/auth";
const { groups, tree, loadGroupData, deleteGroup, createGroup } = useAuthStore();
const { groups, tree, loadGroupData, deleteGroup, createGroup } =
useAuthStore();
const toggleButton = new Button({
css: "btn-outline-primary flex-grow-1",
Expand All @@ -69,7 +75,7 @@ const addGroupModalButtons = [
icon: "group_add",
text: "Add",
callback: () => {
createGroup(addGroupModalContent.value.myGroup)
createGroup(addGroupModalContent.value.myGroup);
},
}),
];
Expand All @@ -95,25 +101,22 @@ const tabs = [
},
];
const bindArgs = computed(() => (
[
{ group: activeGroup.value },
{ group: activeGroup.value },
{ recipientType: "Group", recipientId: activeGroup.value?.id },
]
));
const bindArgs = computed(() => [
{ group: activeGroup.value },
{ group: activeGroup.value },
{ recipientType: "Group", recipientId: activeGroup.value?.id },
]);
const rootGroups = computed(() => {
return tree.value[null]?.map((i) => groups.value[i]);
})
});
const activeGroup = ref();
function selectGroup(newGroup) {
if (newGroup.id === activeGroup.value?.id) {
activeGroup.value = undefined;
} else {
activeGroup.value = newGroup
activeGroup.value = newGroup;
}
}
Expand Down
5 changes: 0 additions & 5 deletions frontend/src/components/auth/GroupMemberships.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,7 @@
</template>
<script setup>
import { api } from "@/composables/api";
import { onActivated } from "vue";
const props = defineProps(["username"]);
const memberships = await api.getUserMemberships({ username: props.username });
onActivated(async () => {
memberships.value = await api.getUserMemberships({ username: props.username });
})
</script>
46 changes: 27 additions & 19 deletions frontend/src/components/auth/UserDetails.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,32 @@
</div>
<div class="row">
<div class="col-3 fw-bold">Email:</div>
<div class="col-9"><a :href="`mailto:${user?.email}`">{{ user?.email }}</a></div>
<div class="col-9">
<a :href="`mailto:${user?.email}`">{{ user?.email }}</a>
</div>
</div>
<div class="row">
<div class="col-3 fw-bold">Is Active?</div>
<div class="col-9 d-flex flex-row justify-content-between flex-grow-0">
<div>
<span class="badge" :class="user?.is_active? 'bg-success text-black': 'bg-dark text-light'">
{{ user?.is_active ? 'Yes' : 'No' }}
<span
class="badge"
:class="
user?.is_active ? 'bg-success text-black' : 'bg-dark text-light'
"
>
{{ user?.is_active ? "Yes" : "No" }}
</span>
</div>
<button class="btn" @click="onClick"
:class="`btn-outline-${user?.is_active? 'danger': 'primary'}`">
<button
class="btn"
@click="onClick"
:class="`btn-outline-${user?.is_active ? 'danger' : 'primary'}`"
>
<i class="eos-icons me-1">
{{ user?.is_active? 'block': 'play_arrow'}}
{{ user?.is_active ? "block" : "play_arrow" }}
</i>
{{ user?.is_active? 'Deactivate': 'Activate'}}
{{ user?.is_active ? "Deactivate" : "Activate" }}
</button>
</div>
</div>
Expand All @@ -49,28 +59,26 @@ export default {
props: {
user: {
required: false,
type: Object
}
type: Object,
},
},
setup() {
const { activateUser, deactivateUser} = useAuthStore();
return { activateUser, deactivateUser }
const { activateUser, deactivateUser } = useAuthStore();
return { activateUser, deactivateUser };
},
methods: {
async onClick() {
if (this.user === null) {
return;
}
if (this.user?.is_active) {
await this.deactivateUser(this.user.username)
await this.deactivateUser(this.user.username);
} else {
await this.activateUser(this.user.username)
await this.activateUser(this.user.username);
}
}
}
}
},
},
};
</script>

<style scoped>

</style>
<style scoped></style>
6 changes: 2 additions & 4 deletions frontend/src/components/auth/UserManager.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@
</li>
</ul>
</div>
<div v-show="selectedUser" class="user-detail">
<div v-show="selectedUser" class="user-detail flex-fill">
<h4>{{ selectedUser?.username}}</h4>
<keep-alive>
<Tabbing :bind-args="bindArgs" :tabs="tabs" />
</keep-alive>
<Tabbing :bind-args="bindArgs" :tabs="tabs" />
</div>
</div>
</template>
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/components/change_review/Changes.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</template>

<script>
import {shallowRef} from "vue";
import { markRaw } from "vue";
import AutoPagination from "@/components/layout/AutoPagination";
import BaseLayout from "@/components/layout/BaseLayout";
import ChangePage from "@/components/change_review/ChangePage";
Expand All @@ -32,7 +32,7 @@ export default {
},
data() {
return {
pageComponent: shallowRef(ChangePage)
pageComponent: markRaw(ChangePage)
}
},
computed: {
Expand Down
1 change: 0 additions & 1 deletion frontend/src/components/layout/Tabbing.vue
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ export default {
currentTab: this.initialTab
}
},
expose: ['currentTab'],
methods: {
navLinkClass(tabIndex) {
if (this.currentTab === tabIndex) {
Expand Down
16 changes: 11 additions & 5 deletions frontend/src/composables/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,7 @@ class API {
async getEntity({ schemaSlug, entityIdOrSlug } = {}) {
const params = new URLSearchParams();
return this._fetch({
url: `${
this.base
}/entity/${schemaSlug}/${entityIdOrSlug}?${params.toString()}`,
url: `${this.base}/entity/${schemaSlug}/${entityIdOrSlug}?${params.toString()}`,
});
}

Expand Down Expand Up @@ -356,7 +354,11 @@ class API {
method: "PATCH",
});
if (response !== null) {
this.alerts.push("success", `User activated: ${username}`);
if (response) {
this.alerts.push("success", `User activated: ${username}`);
} else {
this.alerts.push("success", `User already activated: ${username}`);
}
}
return response;
}
Expand All @@ -367,7 +369,11 @@ class API {
method: "DELETE",
});
if (response !== null) {
this.alerts.push("success", `User deactivated: ${username}`);
if (response) {
this.alerts.push("success", `User deactivated: ${username}`);
} else {
this.alerts.push("success", `User already deactivated: ${username}`);
}
}
return response;
}
Expand Down
44 changes: 30 additions & 14 deletions frontend/src/store/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,20 @@ export const useAuthStore = () => {
return computedTree;
})

const loadGroupData = async () => {
const fetchGroupData = async () => {
const response = await api.getGroups();

for (let group of (response || [])) {
groups.value[group.id] = group;
}
}

const loadGroupData = async () => {
if (Object.values(groups.value).length === 0) {
await fetchGroupData()
}
}

const updateGroup = async (groupId, groupData) => {
try {
const response = await api.updateGroup({ groupId: groupId, body: groupData });
Expand Down Expand Up @@ -56,29 +62,39 @@ export const useAuthStore = () => {
}
}

const loadUserData = async () => {
const fetchUserData = async () => {
const response = await api.getUsers();
for (const user of (response || [])) {
users.value[user.id] = user;
}
}

const loadUserData = async () => {
if (Object.values(users.value).length === 0) {
await fetchUserData();
}
}

const activateUser = async (username) => {
await api.activate_user({username: username});
Object.values(users.value).forEach(user => {
if(user.username === username) {
user.is_active = !user.is_active
}
});
const result = await api.activate_user({username: username});
if (result != null) {
Object.values(users.value).forEach(user => {
if(user.username === username) {
user.is_active = true;
}
});
}
}

const deactivateUser = async (username) => {
await api.deactivate_user({username: username});
Object.values(users.value).forEach(user => {
if(user.username === username) {
user.is_active = !user.is_active
}
});
const result = await api.deactivate_user({username: username});
if (result !== null) {
Object.values(users.value).forEach(user => {
if(user.username === username) {
user.is_active = false;
}
});
}
}

return {
Expand Down

0 comments on commit c26d147

Please sign in to comment.