load filter algorithms from api via options

This commit is contained in:
Corban-Lee Jones 2024-07-10 21:20:53 +01:00
parent f798e0c0f0
commit 30694569ca
4 changed files with 27 additions and 26 deletions

View File

@ -122,6 +122,10 @@ async function editFilter(id, formData) {
return await ajaxRequest(`/api/filter/${id}/`, "PUT", formData); return await ajaxRequest(`/api/filter/${id}/`, "PUT", formData);
} }
async function getFilterOptions() {
return await ajaxRequest("/api/filter/", "OPTIONS")
}
// Tracked Content // Tracked Content

View File

@ -189,11 +189,11 @@ async function loadFilters(guildId, page=1, pageSize=null) {
} }
} }
$(document).ready(function() { $(document).ready(async function() {
loadMatchingAlgorithms(); await loadMatchingAlgorithms();
}); });
function loadMatchingAlgorithms() { async function loadMatchingAlgorithms() {
// Disable input while options are loading // Disable input while options are loading
$("#filterAlgorithm").prop("disabled", true); $("#filterAlgorithm").prop("disabled", true);
@ -206,25 +206,22 @@ function loadMatchingAlgorithms() {
// Clear select2 input // Clear select2 input
$("#filterAlgorithm").val("").change(); $("#filterAlgorithm").val("").change();
algorithms = [ try {
{id: "", name: "None"}, options = await getFilterOptions();
{id: 1, name: "Any Words"}, options.actions.POST.matching_algorithm.choices.forEach(algorithm => {
{id: 2, name: "All Words"}, $("#filterAlgorithm").append($("<option>", {
{id: 3, name: "Exact Match"}, text: algorithm.display_name,
{id: 4, name: "Regular Expression"}, value: algorithm.value > 0 ? algorithm.value : "" // empty string for 'None' option at 0
{id: 5, name: "Fuzzy Match"}, })); // (helps with validation)
] });
}
algorithms.forEach(algorithm => { catch (error) {
$("#filterAlgorithm").append($("<option>", { console.error(error);
text: algorithm.name, }
value: algorithm.id finally {
})); // Re-enable the input
}); $("#filterAlgorithm").prop("disabled", false);
}
// Re-enable the input
$("#filterAlgorithm").prop("disabled", false);
}; };
$(document).on("selectedServerChange", async function() { $(document).on("selectedServerChange", async function() {

View File

@ -4,7 +4,7 @@
<div class="modal-content rounded-1"> <div class="modal-content rounded-1">
<form id="filterForm" class="mb-0" novalidate> <form id="filterForm" class="mb-0" novalidate>
<div class="modal-header"> <div class="modal-header">
<h5 class="modal-title"> <h5 class="modal-title ms-2">
<span class="form-create">Add</span> <span class="form-create">Add</span>
<span class="form-edit">Edit</span> <span class="form-edit">Edit</span>
Filter Filter
@ -13,13 +13,13 @@
<div class="modal-body p-4"> <div class="modal-body p-4">
<input type="hidden" id="filterId" name="filterId"> <input type="hidden" id="filterId" name="filterId">
<div class="row"> <div class="row">
<div class="col-lg-6 pe-lg-4"> <div class="col-12">
<div class="mb-4"> <div class="mb-4">
<label for="filterName" class="form-label">Name</label> <label for="filterName" class="form-label">Name</label>
<input type="text" id="filterName" name="filterName" class="form-control rounded-1" tabindex="0"> <input type="text" id="filterName" name="filterName" class="form-control rounded-1" tabindex="0">
</div> </div>
</div> </div>
<div class="col-lg-6 ps-lg-4"> <div class="col-12">
<div class="mb-4"> <div class="mb-4">
<label for="filterAlgorithm" class="form-label">Matching Algorithm</label> <label for="filterAlgorithm" class="form-label">Matching Algorithm</label>
<select name="filterAlgorithm" id="filterAlgorithm" class="select-2" data-dropdownparent="#filterFormModal" tabindex="1"></select> <select name="filterAlgorithm" id="filterAlgorithm" class="select-2" data-dropdownparent="#filterFormModal" tabindex="1"></select>

View File

@ -4,7 +4,7 @@
<div class="modal-content rounded-1"> <div class="modal-content rounded-1">
<form id="serverForm" class="mb-0" novalidate> <form id="serverForm" class="mb-0" novalidate>
<div class="modal-header"> <div class="modal-header">
<h5 class="modal-title"> <h5 class="modal-title ms-2">
Add Server Add Server
</h5> </h5>
</div> </div>