Editing existing subscription

This commit is contained in:
Corban-Lee Jones 2024-04-26 01:09:44 +01:00
parent 8b572eba46
commit c0db79d51c
5 changed files with 89 additions and 33 deletions

View File

@ -3,7 +3,7 @@
transform: translateY(0);
}
50% {
transform: translateY(3px); /* Adjust the height of the bump */
transform: translateY(5px); /* Adjust the height of the bump */
}
100% {
transform: translateY(0);
@ -14,19 +14,19 @@
animation: bump .2s ease-out;
}
.server-item.active img {
.server-item-selector.active img {
border-radius: .75rem !important;
}
.server-item:hover img {
.server-item-selector:hover img, .server-item-selector:focus img, .server-item-selector:focus-visible img {
border-radius: .75rem !important;
}
.server-item:active img {
.server-item-selector:active, .server-item-selector:focus, .server-item-selector:focus-visible {
animation: bump .2s ease-out;
}
.server-item img {
.server-item-selector img {
transition: border-radius .15s ease-in;
}

View File

@ -107,7 +107,7 @@ function addServerTemplate(serverPrimaryKey, serverGuildId, serverName, serverIc
template.attr("data-id", serverPrimaryKey);
// Bind the button for selecting this server
template.find(".nav-link").off("click").on("click", function() {
template.find(".server-item-selector").off("click").on("click", function() {
selectServer(serverPrimaryKey);
});

View File

@ -22,25 +22,45 @@ function initSubscriptionTable() {
columns: [
{
// Select row checkbox column
title: '<input type="checkbox" class="form-check-input table-select-all mt-0" />',
title: '<input type="checkbox" class="form-check-input table-select-all" />',
data: null,
orderable: false,
className: "text-center",
render: function() {
return '<input type="checkbox" class="form-check-input table-select-row mt-0" />'
return '<input type="checkbox" class="form-check-input table-select-row" />'
}
},
{ title: "ID", data: "id", visible: false },
{
title: "Name",
data: "name",
render: function(data, type, row) {
return `<a href="#" onclick="showEditSubModal(${row.id})" class="text-decoration-none">${data}</a>`
}
},
{
title: "URL",
data: "url",
render: function(data, type) {
return `<a href="${data}" class="text-decoration-none" target="_blank">${data}</a>`
}
},
{ title: "Name", data: "name" },
{ title: "URL", data: "url" },
{ title: "Channels", data: "channels_count" },
{ title: "Created", data: "creation_datetime" },
{
title: "Created",
data: "creation_datetime",
render: function(data, type) {
return new Date(data).toISOString().split("T")[0];
}
},
{
title: "Notes",
data: "extra_notes",
orderable: false,
className: "text-center",
render: function(data, type) {
return `<i class="bi bi-chat-left-text" title="${data}"></i>`
if (!data) return "-";
return $(`<i class="bi bi-chat-left-text" data-bs-trigger="hover focus" data-bs-toggle="popover" data-bs-title="Extra Notes" data-bs-content="${data}"></i>`).popover()[0];
}
},
{
@ -49,7 +69,7 @@ function initSubscriptionTable() {
orderable: false,
className: "text-center form-switch",
render: function(data, type) {
return `<input type="checkbox" class="form-check-input m-0" ${data ? "checked" : ""} />`
return `<input type="checkbox" class="form-check-input ms-0" ${data ? "checked" : ""} />`
}
}
]
@ -69,6 +89,8 @@ function determineSelectAllState() {
selectAllCheckbox.prop("checked", checked);
selectAllCheckbox.prop("indeterminate", indeterminate);
$("#tableDeleteSelectedBtn").prop("disabled", !(checked || indeterminate));
}
// Select a row via checkbox
@ -106,27 +128,45 @@ $("#tableButton").on("click", function() {
});
// Open new subscription modal
$("#tableAddRowBtn").on("click", function() {
$("#subFormModal").modal("show");
$("#tableAddRowBtn").on("click", async function() {
await showEditSubModal(-1);
});
async function showEditSubModal(guildId) {
if (guildId === -1) {
$("#subFormModal input, #subFormModal textarea").val("");
}
else {
const subscription = subTable.row({id:guildId}).data()
$("#subName").val(subscription.name);
$("#subUrl").val(subscription.url);
$("#subExtraNotes").val(subscription.extra_notes);
}
$("#subId").val(guildId);
$("#subFormModal").modal("show");
}
$("#subForm").on("submit", async function(event) {
event.preventDefault();
var name = $("#subName").val();
var id = $("#subId").val();
name = $("#subName").val();
url = $("#subUrl").val();
guildId = getCurrentlyActiveServer().guild_id;
extraNotes = $("#subExtraNotes").val();
var subPrimaryKey = await registerNewSubscription(name, url, guildId, extraNotes);
var subPrimaryKey = await saveSubscription(id, name, url, guildId, extraNotes);
if (subPrimaryKey)
showToast("success", "subscription created", "created with ID: " + subPrimaryKey);
showToast("success", "Subscription Saved", "Subscription ID: " + subPrimaryKey);
await loadSubscriptions(guildId);
$("#subFormModal").modal("hide");
});
async function registerNewSubscription(name, url, guildId, extraNotes) {
async function saveSubscription(id, name, url, guildId, extraNotes) {
var formData = new FormData();
formData.append("name", name);
formData.append("url", url);
@ -134,9 +174,13 @@ async function registerNewSubscription(name, url, guildId, extraNotes) {
formData.append("extra_notes", extraNotes);
formData.append("active", true);
try { response = await newSubscription(formData); }
var response;
try {
if (id === "-1") response = await newSubscription(formData);
else response = await editSubscription(id, formData);
}
catch (err) {
// alert(JSON.stringify(err))
showToast("danger", "Subscription Error", err.responseText, 18000);
return false;
}
@ -169,4 +213,17 @@ async function loadSubscriptions(guildId) {
$(document).on("selectedServerChange", async function() {
const activeServer = getCurrentlyActiveServer();
await loadSubscriptions(activeServer.guild_id);
})
$("#tableDeleteSelectedBtn").on("click", async function() {
// showToast("danger", "Not Implemented", "This feature isn't implemented");
var rows = subTable.rows(".selected").data();
$.each(rows, async function() {
// alert(JSON.stringify(this, null, 4));
await deleteSubscription(this.id);
showToast("danger", "Deleted Subscription", "Subscription ID: " + this.id)
})
loadSubscriptions(getCurrentlyActiveServer().guild_id);
})

View File

@ -9,6 +9,7 @@
</h5>
</div>
<div class="modal-body">
<input type="hidden" id="subId" name="subId">
<div class="mb-3">
<label for="subName" class="form-label">Name</label>
<input type="text" id="subName" name="subName" class="form-control" placeholder="BBC News · Top Stories">

View File

@ -15,11 +15,9 @@
<ul id="serverList" class="nav nav-pills nav-flush flex-column mb-auto text-center">
<li class="nav-item"><hr class="my-2"></li>
<li class="nav-item">
<a href="#" id="newServerBtn" class="nav-link px-3 py-2 rounded-0">
<div class="text-light bg-primary rounded-circle mx-auto ratio ratio-1x1 position-relative" style="max-width: 100">
<i class="bi bi-plus-lg fs-5 position-absolute top-50 start-50 translate-middle" style="width: fit-content; height: fit-content;"></i>
</div>
</a>
<button type="button" id="newServerBtn" class="btn btn-outline-primary rounded-circle" style="width: 46px; height: 46px;">
<i class="bi bi-plus-lg fs-5"></i>
</button>
</li>
</ul>
</div>
@ -39,10 +37,10 @@
</div>
<div class="col-12 bg-body-tertiary">
<div class="px-3 py-4">
<button type="button" id="tableButton" class="btn btn-primary">See Selected</button>
<button type="button" id="tableAddRowBtn" class="btn btn-success">Add New</button>
<button type="button" id="tableDeleteSelectedBtn" class="btn btn-danger" disabled>Delete Selected</button>
<div class="d-flex px-3 py-4">
<button type="button" id="tableAddRowBtn" class="btn btn-success me-2">Add New</button>
<button type="button" id="tableDeleteSelectedBtn" class="btn btn-danger me-auto" disabled>Delete Selected</button>
<button type="button" id="tableButton" class="btn btn-outline-primary">See Selected</button>
</div>
</div>
@ -62,9 +60,9 @@
{% block javascript %}
<script id="serverItemTemplate" type="text/template">
<li class="nav-item server-item" data-id="">
<a href="#" class="nav-link px-3 py-2 rounded-0" data-bs-toggle="tooltip" data-bs-placement="right" data-bs-title="Option 1" aria-label="Option 1">
<img src="" alt="Guild Icon" class="rounded-circle" style="max-width: 100%;">
</a>
<button type="button" class="btn border-0 server-item-selector">
<img src="" alt="Guild Icon" class="rounded-circle" width="46" height="46">
</button>
</li>
</script>
<script src="{% static 'js/api.js' %}"></script>