Skip to content

Commit cb421de

Browse files
luizhf42gustavosbarreto
authored andcommitted
refactor(ui): migrate Tags store to Pinia
1 parent c694119 commit cb421de

File tree

16 files changed

+118
-153
lines changed

16 files changed

+118
-153
lines changed

ui/src/components/Devices/DeviceDelete.vue

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
<script setup lang="ts">
5353
import { ref } from "vue";
5454
import { useRouter } from "vue-router";
55-
import { useStore } from "@/store";
5655
import handleError from "@/utils/handleError";
5756
import useSnackbar from "@/helpers/snackbar";
5857
import BaseDialog from "../BaseDialog.vue";
@@ -68,26 +67,17 @@ const props = defineProps<{
6867
const emit = defineEmits(["update"]);
6968
const showDialog = ref(false);
7069
const snackbar = useSnackbar();
71-
const store = useStore();
7270
const devicesStore = useDevicesStore();
7371
const router = useRouter();
7472
75-
const emitUpdate = (): void => {
76-
emit("update");
77-
};
78-
7973
const removeDevice = async (): Promise<void> => {
8074
try {
8175
await devicesStore.removeDevice(props.uid);
8276
83-
if (props.redirect) {
84-
router.push("/devices");
85-
} else {
86-
await store.dispatch("tags/fetch");
87-
}
77+
if (props.redirect) router.push("/devices");
8878
8979
snackbar.showSuccess("Successfully removed device.");
90-
emitUpdate();
80+
emit("update");
9181
} catch (error: unknown) {
9282
snackbar.showError("Failed to remove device.");
9383
handleError(error);

ui/src/components/PublicKeys/PublicKeyAdd.vue

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ import { computed, nextTick, ref, watch } from "vue";
127127
import * as yup from "yup";
128128
import axios, { AxiosError } from "axios";
129129
import { actions, authorizer } from "@/authorizer";
130-
import { useStore } from "@/store";
131130
import hasPermission from "@/utils/permission";
132131
import { validateKey } from "@/utils/validate";
133132
import handleError from "@/utils/handleError";
@@ -136,13 +135,14 @@ import BaseDialog from "../BaseDialog.vue";
136135
import useAuthStore from "@/store/modules/auth";
137136
import usePublicKeysStore from "@/store/modules/public_keys";
138137
import { IPublicKeyCreate } from "@/interfaces/IPublicKey";
138+
import useTagsStore from "@/store/modules/tags";
139139
140140
const { size } = defineProps<{ size?: string }>();
141141
142142
const emit = defineEmits(["update"]);
143-
const store = useStore();
144143
const authStore = useAuthStore();
145144
const publicKeysStore = usePublicKeysStore();
145+
const tagsStore = useTagsStore();
146146
const showDialog = ref(false);
147147
const snackbar = useSnackbar();
148148
const validateLength = ref(true);
@@ -211,7 +211,7 @@ const {
211211
initialValue: "",
212212
});
213213
214-
const tagNames = computed(() => store.getters["tags/list"]);
214+
const tagNames = computed(() => tagsStore.tags);
215215
216216
const hasAuthorization = computed(() => {
217217
const { role } = authStore;
@@ -234,7 +234,7 @@ watch(tagChoices, (list) => {
234234
235235
watch(choiceFilter, async () => {
236236
if (choiceFilter.value === "tags") {
237-
await store.dispatch("tags/fetch");
237+
await tagsStore.fetchTags();
238238
}
239239
});
240240

ui/src/components/PublicKeys/PublicKeyEdit.vue

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,13 @@ import {
134134
onUpdated,
135135
} from "vue";
136136
import * as yup from "yup";
137-
import { useStore } from "@/store";
138137
import { IPublicKey } from "@/interfaces/IPublicKey";
139138
import handleError from "@/utils/handleError";
140139
import useSnackbar from "@/helpers/snackbar";
141140
import BaseDialog from "../BaseDialog.vue";
142141
import { HostnameFilter, TagsFilter } from "@/interfaces/IFilter";
143142
import usePublicKeysStore from "@/store/modules/public_keys";
143+
import useTagsStore from "@/store/modules/tags";
144144
145145
const props = defineProps<{
146146
publicKey: IPublicKey;
@@ -149,8 +149,8 @@ const props = defineProps<{
149149
150150
const emit = defineEmits(["update"]);
151151
const showDialog = ref(false);
152-
const store = useStore();
153152
const publicKeysStore = usePublicKeysStore();
153+
const tagsStore = useTagsStore();
154154
const snackbar = useSnackbar();
155155
const choiceFilter = ref("hostname");
156156
const validateLength = ref(true);
@@ -234,13 +234,13 @@ const hasTags = computed(() => {
234234
235235
watch(choiceFilter, async () => {
236236
if (choiceFilter.value === "tags") {
237-
await store.dispatch("tags/fetch");
237+
await tagsStore.fetchTags();
238238
}
239239
});
240240
241241
const tagNames = computed({
242242
get() {
243-
return store.getters["tags/list"];
243+
return tagsStore.tags;
244244
},
245245
set(val) {
246246
tagChoices.value = val;

ui/src/components/Tags/TagEdit.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,18 @@
5454

5555
<script setup lang="ts">
5656
import { ref, computed, watch } from "vue";
57-
import { useStore } from "@/store";
5857
import handleError from "@/utils/handleError";
5958
import useSnackbar from "@/helpers/snackbar";
6059
import BaseDialog from "../BaseDialog.vue";
60+
import useTagsStore from "@/store/modules/tags";
6161
6262
const props = defineProps<{
6363
tag: string;
6464
hasAuthorization: boolean;
6565
}>();
6666
6767
const emit = defineEmits(["update"]);
68-
const store = useStore();
68+
const tagsStore = useTagsStore();
6969
const snackbar = useSnackbar();
7070
const showDialog = ref(false);
7171
@@ -100,7 +100,7 @@ const update = () => {
100100
const edit = async () => {
101101
if (!tagsError.value) {
102102
try {
103-
await store.dispatch("tags/edit", {
103+
await tagsStore.updateTag({
104104
oldTag: props.tag,
105105
newTag: inputTags.value,
106106
});

ui/src/components/Tags/TagFormUpdate.vue

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@
6464
<script setup lang="ts">
6565
import { computed, ref, watch } from "vue";
6666
import axios, { AxiosError } from "axios";
67-
import { useStore } from "@/store";
6867
import handleError from "@/utils/handleError";
6968
import useSnackbar from "@/helpers/snackbar";
7069
import BaseDialog from "../BaseDialog.vue";
7170
import useDevicesStore from "@/store/modules/devices";
71+
import useTagsStore from "@/store/modules/tags";
7272
7373
const props = defineProps<{
7474
deviceUid: string;
@@ -78,8 +78,8 @@ const props = defineProps<{
7878
7979
const emit = defineEmits(["update"]);
8080
const snackbar = useSnackbar();
81-
const store = useStore();
8281
const devicesStore = useDevicesStore();
82+
const tagsStore = useTagsStore();
8383
const showDialog = ref(false);
8484
const hasTags = computed(() => props.tagsList.length > 0);
8585
const inputTags = ref<string[]>([]);
@@ -112,12 +112,7 @@ const save = async () => {
112112
tags: { tags: inputTags.value },
113113
});
114114
115-
await store.dispatch("tags/setTags", {
116-
data: inputTags.value,
117-
headers: {
118-
"x-total-count": inputTags.value.length,
119-
},
120-
});
115+
tagsStore.fetchTags();
121116
showDialog.value = false;
122117
snackbar.showSuccess("Tags updated successfully.");
123118

ui/src/components/Tags/TagList.vue

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,17 @@
6666

6767
<script setup lang="ts">
6868
import { computed, onMounted, ref } from "vue";
69-
import { useStore } from "@/store";
7069
import { actions, authorizer } from "@/authorizer";
7170
import hasPermission from "@/utils/permission";
7271
import TagRemove from "./TagRemove.vue";
7372
import TagEdit from "./TagEdit.vue";
7473
import handleError from "@/utils/handleError";
7574
import useSnackbar from "@/helpers/snackbar";
7675
import useAuthStore from "@/store/modules/auth";
76+
import useTagsStore from "@/store/modules/tags";
7777
78-
const store = useStore();
7978
const authStore = useAuthStore();
79+
const tagsStore = useTagsStore();
8080
const snackbar = useSnackbar();
8181
const headers = ref([
8282
{
@@ -93,7 +93,7 @@ const headers = ref([
9393
},
9494
]);
9595
96-
const tags = computed(() => store.getters["tags/list"]);
96+
const tags = computed(() => tagsStore.tags);
9797
9898
const hasAuthorizationEdit = () => {
9999
const { role } = authStore;
@@ -107,14 +107,14 @@ const hasAuthorizationRemove = () => {
107107
108108
const getTags = async () => {
109109
try {
110-
await store.dispatch("tags/fetch");
110+
await tagsStore.fetchTags();
111111
} catch (error: unknown) {
112112
snackbar.showError("Failed to load tags.");
113113
handleError(error);
114114
}
115115
};
116116
117-
onMounted(() => {
118-
getTags();
117+
onMounted(async () => {
118+
await getTags();
119119
});
120120
</script>

ui/src/components/Tags/TagRemove.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@
4141

4242
<script setup lang="ts">
4343
import { ref } from "vue";
44-
import { useStore } from "@/store";
4544
import handleError from "@/utils/handleError";
4645
import useSnackbar from "@/helpers/snackbar";
4746
import BaseDialog from "../BaseDialog.vue";
47+
import useTagsStore from "@/store/modules/tags";
4848
4949
defineOptions({
5050
inheritAttrs: false,
@@ -57,12 +57,12 @@ const props = defineProps<{
5757
5858
const emit = defineEmits(["update"]);
5959
const showDialog = ref(false);
60-
const store = useStore();
60+
const tagsStore = useTagsStore();
6161
const snackbar = useSnackbar();
6262
6363
const remove = async () => {
6464
try {
65-
await store.dispatch("tags/remove", props.tag);
65+
await tagsStore.removeTag(props.tag);
6666
snackbar.showSuccess(`Tag ${props.tag} removed successfully.`);
6767
emit("update");
6868
} catch (error: unknown) {

ui/src/components/Tags/TagSelector.vue

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
<!-- eslint-disable vue/no-v-text-v-html-on-component -->
21
<template>
32
<div class="mr-4">
43
<v-menu location="bottom" v-bind="$attrs" scrim eager>
@@ -14,7 +13,7 @@
1413
data-test="tags-btn"
1514
color="primary"
1615
variant="outlined"
17-
:disabled="getListTags.length == 0"
16+
:disabled="tags.length === 0"
1817
@click="getTags"
1918
>
2019
Tags
@@ -24,7 +23,7 @@
2423
</template>
2524
<v-list shaped density="compact" class="bg-v-theme-surface">
2625
<div>
27-
<template v-for="(item, i) in getListTags">
26+
<template v-for="(item, i) in tags">
2827
<v-divider v-if="!item" :key="`divider-${i}`" />
2928

3029
<v-list-item
@@ -42,8 +41,7 @@
4241
color="primary"
4342
hide-details
4443
/>
45-
46-
<v-list-item-title v-text="item" />
44+
<v-list-item-title>{{ item }}</v-list-item-title>
4745
</v-list-item-action>
4846
</div>
4947
</template>
@@ -58,25 +56,24 @@
5856
<script setup lang="ts">
5957
import { computed, onMounted, ref } from "vue";
6058
import axios, { AxiosError } from "axios";
61-
import { useStore } from "@/store";
6259
import handleError from "@/utils/handleError";
6360
import useSnackbar from "@/helpers/snackbar";
6461
import useContainersStore from "@/store/modules/containers";
6562
import useDevicesStore from "@/store/modules/devices";
63+
import useTagsStore from "@/store/modules/tags";
6664
6765
const props = defineProps<{ variant: "device" | "container" }>();
6866
69-
const store = useStore();
7067
const containersStore = useContainersStore();
7168
const devicesStore = useDevicesStore();
69+
const tagsStore = useTagsStore();
7270
const snackbar = useSnackbar();
73-
const prevSelectedLength = ref(0);
74-
const getListTags = computed(() => store.getters["tags/list"]);
75-
const selectedTags = computed<Array<string>>(() => store.getters["tags/selected"]);
71+
const tags = computed(() => tagsStore.tags);
72+
const selectedTags = ref<Array<string>>([]);
7673
const tagIsSelected = (tag: string) => selectedTags.value.includes(tag);
7774
7875
const getTags = async () => {
79-
await store.dispatch("tags/fetch");
76+
await tagsStore.fetchTags();
8077
};
8178
8279
const fetchDevices = async (filter?: string) => {
@@ -113,18 +110,11 @@ const getItems = async (item: Array<string>) => {
113110
};
114111
115112
const selectTag = async (item: string) => {
116-
store.dispatch("tags/setSelected", item);
117-
if (selectedTags.value.length > 0) {
118-
await getItems(selectedTags.value);
119-
prevSelectedLength.value = selectedTags.value.length;
120-
} else if (prevSelectedLength.value === 1 && selectedTags.value.length === 0) {
121-
await fetchDevices();
122-
}
113+
if (tagIsSelected(item)) selectedTags.value = selectedTags.value.filter((tag) => tag !== item);
114+
else selectedTags.value.push(item);
123115
124-
if (selectedTags.value.length === 0) {
125-
await store.dispatch("tags/clearSelectedTags");
126-
await fetchDevices();
127-
}
116+
if (selectedTags.value.length) await getItems(selectedTags.value);
117+
else await fetchDevices();
128118
};
129119
130120
onMounted(async () => {

ui/src/components/firewall/FirewallRuleAdd.vue

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,19 +175,21 @@ import { FormFilterOptions } from "@/interfaces/IFilter";
175175
import BaseDialog from "../BaseDialog.vue";
176176
import useAuthStore from "@/store/modules/auth";
177177
import useFirewallRulesStore from "@/store/modules/firewall_rules";
178+
import useTagsStore from "@/store/modules/tags";
178179
179180
const snackbar = useSnackbar();
180181
const store = useStore();
181182
const authStore = useAuthStore();
182183
const firewallRulesStore = useFirewallRulesStore();
184+
const tagsStore = useTagsStore();
183185
const emit = defineEmits(["update"]);
184186
const showDialog = ref(false);
185187
const active = ref(true);
186188
const action = ref<IFirewallRule["action"]>("allow");
187189
const selectedIPOption = ref("all");
188190
const selectedUsernameOption = ref("all");
189191
const selectedFilterOption = ref(FormFilterOptions.All);
190-
const availableTags = computed(() => store.getters["tags/list"]);
192+
const availableTags = computed(() => tagsStore.tags);
191193
const {
192194
value: priority,
193195
errorMessage: priorityError,
@@ -284,7 +286,7 @@ const handleFilterUpdate = async () => {
284286
if (selectedFilterOption.value === FormFilterOptions.Hostname) setHostnameError("This field is required");
285287
if (selectedFilterOption.value === FormFilterOptions.Tags) {
286288
setSelectedTagsError();
287-
await store.dispatch("tags/fetch");
289+
await tagsStore.fetchTags();
288290
}
289291
};
290292

0 commit comments

Comments
 (0)