Fixed created assets not showing on list until site refresh

This commit is contained in:
2025-10-12 20:03:30 +02:00
parent 090db4a1ab
commit 04c39feb7a
3 changed files with 39 additions and 4 deletions

View File

@@ -66,6 +66,7 @@ public class AssetsController : Controller
[HttpPost("Create")]
public async Task<AssetsCreateResponseModel> Create([FromBody]AssetsCreateRequestModel assetModel)
{
string? assetId;
AssetsCreateResponseModel result;
if (assetModel is null)
{
@@ -82,8 +83,8 @@ public class AssetsController : Controller
[
new LdapAttribute("objectClass", ["top", "device", "extensibleObject"]),
];
attributeSet.Add(new LdapAttribute("cn", await _ldap.GetAssetCounterAndIncrementAsync()));
assetId = await _ldap.GetAssetCounterAndIncrementAsync();
attributeSet.Add(new LdapAttribute("cn", assetId));
if (assetModel.SerialNumber != null)
{
attributeSet.Add(new LdapAttribute("serialNumber", assetModel.SerialNumber));
@@ -107,7 +108,7 @@ public class AssetsController : Controller
}
await _ldap.CreateAsset(attributeSet);
result = new AssetsCreateResponseModel(successful: true);
result = new AssetsCreateResponseModel(successful: true, assetId);
}
catch (Exception e)
{

View File

@@ -1,8 +1,9 @@
using Berufsschule_HAM.Models;
public class AssetsCreateResponseModel(bool successful, string exception = "None")
public class AssetsCreateResponseModel(bool successful, string? assetId = null, string exception = "None")
{
public bool Success { get; set; } = successful;
public string? AssetId { get; set; } = assetId;
public string? Exception { get; set; } = exception;
}

View File

@@ -333,6 +333,39 @@
bootstrap.Modal.getInstance(createModalEl).hide();
createForm.reset();
attributesContainer.innerHTML = '';
const tableBody = document.querySelector('table tbody');
if (tableBody) {
const newRow = document.createElement('tr');
newRow.innerHTML = `
<td>${jsonData.Owner || ''}</td>
<td>${result.assetId || ''}</td>
<td>${jsonData.Name || ''}</td>
<td>${jsonData.Location || ''}</td>
<td>
<div class="d-flex gap-2">
<button class="btn btn-sm btn-warning btn-update"
data-asset-id="${result.assetId}"
data-bs-toggle="modal"
data-bs-target="#updateAssetModal">
@T["Update"]
</button>
<button class="btn btn-sm btn-danger btn-delete"
data-asset-id="${result.assetId}"
data-asset-name="${jsonData.Name || ''}"
data-bs-toggle="modal"
data-bs-target="#deleteModal">
@T["Delete"]
</button>
</div>
</td>
`;
newRow.classList.add('table-success');
setTimeout(() => {
newRow.classList.toggle('table-success');
}, 500);
tableBody.append(newRow);
}
showToast('@T["Asset created successfully"]', 'success');
} else {
showToast(`${result.reason || '@T["Error creating asset"]'}`, 'danger');