Skip to content

Commit 50fbb1f

Browse files
authored
Merge pull request #937 from nscuro/policy-tags
2 parents 56b3b5e + db3945d commit 50fbb1f

File tree

2 files changed

+71
-16
lines changed

2 files changed

+71
-16
lines changed

src/views/policy/PolicyList.vue

+21-16
Original file line numberDiff line numberDiff line change
@@ -306,10 +306,10 @@ export default {
306306
});
307307
},
308308
deleteTagLimiter: function (tagName) {
309-
let url = `${this.$api.BASE_URL}/${this.$api.URL_POLICY}/${this.policy.uuid}/tag/${tagName}`;
309+
let url = `${this.$api.BASE_URL}/${this.$api.URL_TAG}/${encodeURIComponent(tagName)}/policy`;
310310
this.axios
311-
.delete(url)
312-
.then((response) => {
311+
.delete(url, { data: [this.policy.uuid] })
312+
.then(() => {
313313
let p = [];
314314
for (let i = 0; i < this.tags.length; i++) {
315315
if (this.tags[i].name !== tagName) {
@@ -318,9 +318,6 @@ export default {
318318
}
319319
this.tags = p;
320320
this.$toastr.s(this.$t('message.updated'));
321-
})
322-
.catch((error) => {
323-
this.$toastr.w(this.$t('condition.unsuccessful_action'));
324321
});
325322
},
326323
updateProjectSelection: function (selections) {
@@ -347,19 +344,27 @@ export default {
347344
},
348345
updateTagSelection: function (selections) {
349346
this.$root.$emit('bv::hide::modal', 'selectTagModal');
347+
348+
let promises = [];
350349
for (let i = 0; i < selections.length; i++) {
351350
let selection = selections[i];
352-
let url = `${this.$api.BASE_URL}/${this.$api.URL_POLICY}/${this.policy.uuid}/tag/${selection.name}`;
353-
this.axios
354-
.post(url)
355-
.then((response) => {
356-
this.tags.push(selection);
357-
this.$toastr.s(this.$t('message.updated'));
358-
})
359-
.catch((error) => {
360-
this.$toastr.w(this.$t('condition.unsuccessful_action'));
361-
});
351+
let url = `${this.$api.BASE_URL}/${this.$api.URL_TAG}/${encodeURIComponent(selection.name)}/policy`;
352+
promises.push(
353+
this.axios
354+
.post(url, [this.policy.uuid])
355+
.then(() => Promise.resolve(selection.name)),
356+
);
362357
}
358+
359+
Promise.all(promises).then((addedTagNames) => {
360+
for (const tagName of addedTagNames) {
361+
if (!this.tags.some((tag) => tag.name === tagName)) {
362+
this.tags.push({ name: tagName });
363+
}
364+
}
365+
366+
this.$toastr.s(this.$t('message.updated'));
367+
});
363368
},
364369
updateIncludeChildren: function () {
365370
let url = `${this.$api.BASE_URL}/${this.$api.URL_POLICY}`;

src/views/portfolio/tags/TaggedPoliciesListModal.vue

+50
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ export default {
3737
apiUrl: function () {
3838
return `${this.$api.BASE_URL}/${this.$api.URL_TAG}/${this.tag}/policy`;
3939
},
40+
untag: function (policyUuids) {
41+
return this.axios.delete(this.apiUrl(), {
42+
data: policyUuids,
43+
});
44+
},
4045
refreshTable: function () {
4146
this.$refs.table.refresh({
4247
url: this.apiUrl(),
@@ -45,13 +50,33 @@ export default {
4550
});
4651
},
4752
},
53+
mounted() {
54+
// NB: Because this modal is loaded dynamically from TagList,
55+
// this.$refs.table may still be undefined when mounted() is called.
56+
// https://jefrydco.id/en/blog/safe-access-vue-refs-undefined
57+
const interval = setInterval(() => {
58+
if (this.$refs.table) {
59+
this.$refs.table.refreshOptions({
60+
showBtnDeleteSelected: this.isPermitted(
61+
this.PERMISSIONS.POLICY_MANAGEMENT,
62+
),
63+
});
64+
clearInterval(interval);
65+
}
66+
}, 50);
67+
},
4868
data() {
4969
return {
5070
labelIcon: {
5171
dataOn: '\u2713',
5272
dataOff: '\u2715',
5373
},
5474
columns: [
75+
{
76+
field: 'state',
77+
checkbox: true,
78+
align: 'center',
79+
},
5580
{
5681
title: this.$t('message.name'),
5782
field: 'name',
@@ -63,6 +88,31 @@ export default {
6388
],
6489
data: [],
6590
options: {
91+
buttons: {
92+
btnDeleteSelected: {
93+
icon: 'fa fa-minus',
94+
attributes: {
95+
title: this.$t('message.unassign_tag_from_selection'),
96+
},
97+
event: () => {
98+
let selected = this.$refs.table.getSelections();
99+
if (
100+
!selected ||
101+
(Array.isArray(selected) && selected.length === 0)
102+
) {
103+
this.$toastr.w(this.$t('message.empty_selection'));
104+
return;
105+
}
106+
107+
this.untag(selected.map((row) => row.uuid)).then(() => {
108+
this.$toastr.s(this.$t('message.tag_unassigned_successfully'));
109+
this.refreshTable();
110+
});
111+
},
112+
},
113+
},
114+
buttonsOrder: ['btnDeleteSelected', 'refresh', 'columns'],
115+
clickToSelect: true,
66116
search: true,
67117
showColumns: true,
68118
showRefresh: true,

0 commit comments

Comments
 (0)