Changing invoice state and updating display from js

This commit is contained in:
rockstardev
2019-05-03 14:54:40 -05:00
committed by Nicolas Dorier
parent 9a2e1d43ea
commit 95d746504d
2 changed files with 59 additions and 52 deletions

View File

@@ -673,61 +673,46 @@ namespace BTCPayServer.Controllers
} }
} }
[HttpGet]
[Route("invoices/{invoiceId}/changestate/{newState}")]
[Authorize(AuthenticationSchemes = Policies.CookieAuthentication)]
[BitpayAPIConstraint(false)]
public IActionResult ChangeInvoiceState(string invoiceId, string newState)
{
if (newState == "invalid")
{
return View("Confirm", new ConfirmModel()
{
Action = "Make invoice invalid",
Title = "Change invoice state",
Description = $"You will transition the state of this invoice to \"invalid\", do you want to continue?",
});
}
else if (newState == "complete")
{
return View("Confirm", new ConfirmModel()
{
Action = "Make invoice complete",
Title = "Change invoice state",
Description = $"You will transition the state of this invoice to \"complete\", do you want to continue?",
ButtonClass = "btn-primary"
});
}
else
return NotFound();
}
[HttpPost] [HttpPost]
[Route("invoices/{invoiceId}/changestate/{newState}")] [Route("invoices/{invoiceId}/changestate/{newState}")]
[Authorize(AuthenticationSchemes = Policies.CookieAuthentication)] [Authorize(AuthenticationSchemes = Policies.CookieAuthentication)]
[BitpayAPIConstraint(false)] [BitpayAPIConstraint(false)]
public async Task<IActionResult> ChangeInvoiceStateConfirm(string invoiceId, string newState) public async Task<IActionResult> ChangeInvoiceState(string invoiceId, string newState)
{ {
var invoice = (await _InvoiceRepository.GetInvoices(new InvoiceQuery() var invoice = (await _InvoiceRepository.GetInvoices(new InvoiceQuery()
{ {
InvoiceId = invoiceId, InvoiceId = invoiceId,
UserId = GetUserId() UserId = GetUserId()
})).FirstOrDefault(); })).FirstOrDefault();
var model = new InvoiceStateChangeModel();
if (invoice == null) if (invoice == null)
return NotFound(); {
model.NotFound = true;
return NotFound(model);
}
if (newState == "invalid") if (newState == "invalid")
{ {
await _InvoiceRepository.UpdatePaidInvoiceToInvalid(invoiceId); await _InvoiceRepository.UpdatePaidInvoiceToInvalid(invoiceId);
_EventAggregator.Publish(new InvoiceEvent(invoice, 1008, InvoiceEvent.MarkedInvalid)); _EventAggregator.Publish(new InvoiceEvent(invoice, 1008, InvoiceEvent.MarkedInvalid));
StatusMessage = "Invoice marked invalid"; model.StatusString = new InvoiceState("invalid", "marked").ToString();
} }
else if (newState == "complete") else if (newState == "complete")
{ {
await _InvoiceRepository.UpdatePaidInvoiceToComplete(invoiceId); await _InvoiceRepository.UpdatePaidInvoiceToComplete(invoiceId);
_EventAggregator.Publish(new InvoiceEvent(invoice, 2008, InvoiceEvent.MarkedCompleted)); _EventAggregator.Publish(new InvoiceEvent(invoice, 2008, InvoiceEvent.MarkedCompleted));
StatusMessage = "Invoice marked complete"; model.StatusString = new InvoiceState("complete", "marked").ToString();
} }
return RedirectToAction(nameof(ListInvoices));
return Json(model);
}
public class InvoiceStateChangeModel
{
public bool NotFound { get; set; }
public string StatusString { get; set; }
} }
[TempData] [TempData]

View File

@@ -91,7 +91,7 @@
</th> </th>
<th style="max-width: 180px;">OrderId</th> <th style="max-width: 180px;">OrderId</th>
<th>InvoiceId</th> <th>InvoiceId</th>
<th>Status</th> <th style="min-width: 140px;">Status</th>
<th style="text-align:right">Amount</th> <th style="text-align:right">Amount</th>
<th style="text-align:right">Actions</th> <th style="text-align:right">Actions</th>
</tr> </tr>
@@ -119,23 +119,25 @@
<td> <td>
@if (invoice.CanMarkStatus) @if (invoice.CanMarkStatus)
{ {
<span class="dropdown-toggle dropdown-toggle-split pavpill pavpil-@invoice.Status.ToString().ToLower()" <div id="pavpill_@invoice.InvoiceId">
data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> <span class="dropdown-toggle dropdown-toggle-split pavpill pavpil-@invoice.Status.ToString().ToLower()"
@invoice.StatusString data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
</span> @invoice.StatusString
<div class="dropdown-menu pull-right"> </span>
@if (invoice.CanMarkInvalid) <div class="dropdown-menu pull-right">
{ @if (invoice.CanMarkInvalid)
<form method="get" asp-action="ChangeInvoiceState" asp-route-invoiceId="@invoice.InvoiceId" asp-route-newState="invalid"> {
<button class="dropdown-item small">Mark as invalid <span class="fa fa-times"></span></button> <button class="dropdown-item small cursorPointer" onclick="changeInvoiceState(this, '@invoice.InvoiceId', 'invalid')">
</form> Mark as invalid <span class="fa fa-times"></span>
} </button>
@if (invoice.CanMarkComplete) }
{ @if (invoice.CanMarkComplete)
<form method="get" asp-action="ChangeInvoiceState" asp-route-invoiceId="@invoice.InvoiceId" asp-route-newState="complete"> {
<button class="dropdown-item small">Mark as complete <span class="fa fa-check-circle"></span></button> <button class="dropdown-item small cursorPointer" onclick="changeInvoiceState(this, '@invoice.InvoiceId', 'complete')">
</form> Mark as complete <span class="fa fa-check-circle"></span>
} </button>
}
</div>
</div> </div>
} }
else else
@@ -251,6 +253,22 @@
}); });
return false; return false;
} }
function changeInvoiceState(sender, invoiceId, newState) {
var pavpill = $("#pavpill_" + invoiceId);
var originalHtml = pavpill.html();
pavpill.html("<span class='fa fa-bitcoin fa-spin' style='margin-left:16px;'></span>");
$.post("invoices/" + invoiceId + "/changestate/" + newState)
.done(function (data) {
var statusHtml = "<span class='pavpill pavpil-" + newState + "'>" + data.statusString + "</span>";
pavpill.html(statusHtml);
})
.fail(function (data) {
pavpill.html(originalHtml.replace("dropdown-menu pull-right show", "dropdown-menu pull-right"));
alert("Invoice state update failed");
});
}
</script> </script>
<style type="text/css"> <style type="text/css">
@@ -283,6 +301,10 @@
cursor: pointer; cursor: pointer;
} }
.dropdown-item {
cursor: pointer;
}
.pavpil-new { .pavpil-new {
background: #d4edda; background: #d4edda;
color: #000; color: #000;