Compare commits

...

4 commits

Author SHA1 Message Date
Suven-p
d552975d6c Display toast on invalid query params 2024-10-28 07:21:28 +05:45
Suven-p
baf0a1bdb9 Refactor 2024-10-28 06:05:35 +05:45
Suven-p
dfb1b97780 Use default value to remove unused filters from query params 2024-10-28 06:01:35 +05:45
Suven-p
177363ac60 Revert "Delete query params if empty"
This reverts commit 2e18a19a9e.
2024-10-28 05:50:50 +05:45
3 changed files with 61 additions and 45 deletions

View file

@ -160,32 +160,34 @@ export default {
/** /**
* Returns applied filters based on query params. * Returns applied filters based on query params.
* @returns {{ status: number[], active: bool, tags: number[] }} The current filter state. * @returns {{ status: number[], active: any[], tags: number[] }} The current filter state.
*/ */
filterState() { filterState() {
// Since query params are always strings, convert them to the correct type // Since query params are always strings, convert them to the correct type
let status = this.$route.query["status"] || null; let status = this.$route.query["status"] || [];
if (status) { if (!Array.isArray(status)) {
if (!Array.isArray(status)) { status = [ status ];
status = [ status ];
}
status = status.map(Number);
} }
status = status.map(Number);
// Casting to boolean does not work here as Boolean("false") === true // Casting to boolean does not work here as Boolean("false") === true
let active = this.$route.query["active"] || null; let active = this.$route.query["active"] || [];
if (active) { if (!Array.isArray(active)) {
if (!Array.isArray(active)) { active = [ active ];
active = [ active ];
}
active = active.map(val => val === "true");
} }
let tags = this.$route.query["tags"] || null; active = active.map(val => {
if (tags) { if (val === "true") {
if (!Array.isArray(tags)) { return true;
tags = [ tags ];
} }
tags = tags.map(Number); if (val === "false") {
return false;
}
return val;
});
let tags = this.$route.query["tags"] || [];
if (!Array.isArray(tags)) {
tags = [ tags ];
} }
tags = tags.map(Number);
return { return {
status, status,
active, active,
@ -198,12 +200,11 @@ export default {
return this.$route.query.searchText || ""; return this.$route.query.searchText || "";
}, },
set(value) { set(value) {
const newQuery = { let newQuery = { ...this.$route.query };
...this.$route.query, if (value === "") {
searchText: value,
};
if (!value) {
delete newQuery.searchText; delete newQuery.searchText;
} else {
newQuery.searchText = value;
} }
this.$router.replace({ this.$router.replace({
query: newQuery, query: newQuery,
@ -286,26 +287,17 @@ export default {
}, },
/** /**
* Update the MonitorList Filter * Update the MonitorList Filter
* @param {{ status: number[], active: bool, tags: number[] }} newFilter Object with new filter * @param {{ status: number[], active: any[], tags: number[] }} newFilter Object with new filter
* @returns {void} * @returns {void}
*/ */
updateFilter(newFilter) { updateFilter(newFilter) {
let newQuery = {
...this.$route.query,
...newFilter,
};
if (!newFilter.status || newFilter.status.length === 0) {
delete newQuery.status;
}
if (!newFilter.active || newFilter.active.length === 0) {
delete newQuery.active;
}
if (!newFilter.tags || newFilter.tags.length === 0) {
delete newQuery.tags;
}
this.$router.replace({ this.$router.replace({
query: newQuery, query: {
...this.$route.query,
status: newFilter.status,
active: newFilter.active,
tags: newFilter.tags,
},
}); });
}, },
/** /**

View file

@ -80,8 +80,9 @@
<MonitorListFilterDropdown :filterActive="filterState.active?.length > 0"> <MonitorListFilterDropdown :filterActive="filterState.active?.length > 0">
<template #status> <template #status>
<span v-if="filterState.active?.length === 1"> <span v-if="filterState.active?.length === 1">
<span v-if="filterState.active[0]">{{ $t("Running") }}</span> <span v-if="filterState.active[0] === true">{{ $t("Running") }}</span>
<span v-else>{{ $t("filterActivePaused") }}</span> <span v-else-if="filterState.active[0] === false">{{ $t("filterActivePaused") }}</span>
<span v-else>{{ $t("Unknown") }}</span>
</span> </span>
<span v-else> <span v-else>
{{ $t("filterActive") }} {{ $t("filterActive") }}
@ -186,10 +187,31 @@ export default {
}); });
return num; return num;
},
/**
* Returns an array of invalid filters assuming tagsList has been fetched
* @returns {Array} Array of invalid filters
*/
invalidFilters() {
const filters = [];
if (!this.filterState.status.every((val) => val >= 0 && val <= 3)) {
filters.push(this.$t("Status"));
}
if (!this.filterState.active.every((val) => val === true || val === false)) {
filters.push(this.$t("Active"));
}
if (!this.filterState.tags.every((val) => this.tagsList.find(tag => tag.id === val))) {
filters.push(this.$t("Tags"));
}
return filters;
} }
}, },
mounted() { mounted() {
this.getExistingTags(); this.getExistingTags(() => {
if (this.invalidFilters.length > 0) {
this.$root.toastError(this.$t("InvalidFilters", [ this.invalidFilters.join(", ") ]));
}
});
}, },
methods: { methods: {
toggleStatusFilter(status) { toggleStatusFilter(status) {
@ -242,14 +264,15 @@ export default {
}, },
clearFilters() { clearFilters() {
this.$emit("updateFilter", { this.$emit("updateFilter", {
status: null, status: [],
}); });
}, },
getExistingTags() { getExistingTags(callback) {
this.$root.getSocket().emit("getTags", (res) => { this.$root.getSocket().emit("getTags", (res) => {
if (res.ok) { if (res.ok) {
this.tagsList = res.tags; this.tagsList = res.tags;
} }
callback();
}); });
}, },
getTaggedMonitorCount(tag) { getTaggedMonitorCount(tag) {

View file

@ -1060,5 +1060,6 @@
"RabbitMQ Password": "RabbitMQ Password", "RabbitMQ Password": "RabbitMQ Password",
"rabbitmqHelpText": "To use the monitor, you will need to enable the Management Plugin in your RabbitMQ setup. For more information, please consult the {rabitmq_documentation}.", "rabbitmqHelpText": "To use the monitor, you will need to enable the Management Plugin in your RabbitMQ setup. For more information, please consult the {rabitmq_documentation}.",
"SendGrid API Key": "SendGrid API Key", "SendGrid API Key": "SendGrid API Key",
"Separate multiple email addresses with commas": "Separate multiple email addresses with commas" "Separate multiple email addresses with commas": "Separate multiple email addresses with commas",
"InvalidFilters": "The following filters are invalid: {0}"
} }