From 78e8924bfad4b7dbeb5c6575846b0013629b82c2 Mon Sep 17 00:00:00 2001 From: James Kolpack Date: Tue, 13 Oct 2015 20:39:51 -0400 Subject: [PATCH] Match count badges --- Web/Controllers/FuelLogController.cs | 16 +++++++++- Web/Scripts/Shared/Site.js | 48 ++++++++++++++++++++-------- Web/Views/FuelLog/Index.cshtml | 6 +++- Web/Views/FuelLog/MatchLink.cshtml | 6 +++- 4 files changed, 59 insertions(+), 17 deletions(-) diff --git a/Web/Controllers/FuelLogController.cs b/Web/Controllers/FuelLogController.cs index fa92a31..6a7faaa 100644 --- a/Web/Controllers/FuelLogController.cs +++ b/Web/Controllers/FuelLogController.cs @@ -60,7 +60,21 @@ namespace MileageTraker.Web.Controllers var vm = new FuelLogViewModel(fuelLog) { VehicleId = DataService.GetVehicleIdByTag(fuelLog.TagNumber) }; return View(vm); } - + + public JsonResult AvailableMatchCount(int id) + { + var fuelLog = DataService.GetFuelLog(id); + var fuelLogViewModel = new FuelLogViewModel(fuelLog) {VehicleId = DataService.GetVehicleIdByTag(fuelLog.TagNumber)}; + + var logs = from l in DataService.GetPossibleMatchingLogs(fuelLog).ToList() + where l != fuelLog.Log + let v = l.Vehicle + let vm = new LogMatchViewModel(l, v, fuelLogViewModel) + select vm; + + return Json(logs.Count(), JsonRequestBehavior.AllowGet); + } + public ViewResult Match(int id) { var fuelLog = DataService.GetFuelLog(id); diff --git a/Web/Scripts/Shared/Site.js b/Web/Scripts/Shared/Site.js index a86e31b..bd03977 100644 --- a/Web/Scripts/Shared/Site.js +++ b/Web/Scripts/Shared/Site.js @@ -21,11 +21,13 @@ $(function () { $("form select#Year").change(function () { var year = $(this).val(); var months = availableLogYearMonths[year]; - var options = ''; - for (var i = 0; i < months.length; i++) { - options += ''; - } - $("form select#Month").html(options); + var options = ''; + if (months != undefined) { + for (var i = 0; i < months.length; i++) { + options += ''; + } + } + $("form select#Month").html(options); }); $("input#ModelYear,input#Price,input#VehicleId,input#EndOdometer,input#GasPurchased").numeric(); @@ -70,15 +72,33 @@ $(function () { // add recent logs to a div if it exists $(function () { - var $recentLogs = $("#RecentLogs"); - if ($recentLogs.length > 0) { - $.ajax({ - url: "/CreateLog/RecentLogs", - success: function (data) { - $recentLogs.append(data); - } - }); - } + var $recentLogs = $("#RecentLogs"); + if ($recentLogs.length > 0) { + $.ajax({ + url: "/FuelLog/RecentLogs", + success: function (data) { + $recentLogs.append(data); + } + }); + } +}); + +// add get match count for all the current items +$(function () { + var $requests = $("a[matchcount]").map(function() { + var $link = $(this); + return $.ajax({ + url: $link.attr("matchcount"), + success: function (matchcount) { + if (matchcount > 0) { + $link.append(" " + matchcount + ""); + } else { + $link.append(" 0"); + } + } + }); + }); + $.when.apply($, $requests); }); $(function() { diff --git a/Web/Views/FuelLog/Index.cshtml b/Web/Views/FuelLog/Index.cshtml index 3ba1c55..1216590 100644 --- a/Web/Views/FuelLog/Index.cshtml +++ b/Web/Views/FuelLog/Index.cshtml @@ -47,7 +47,11 @@ grid.Column("Log", "Matched Log", @ @(item.LogId != null ? Html.ActionLink("View Match", "Match", new {id = item.FuelLogId}, new {@class = "btn btn-mini", target="_blank"}) - : Html.ActionLink("Match", "Match", new {id = item.FuelLogId}, new {@class = "btn btn-warning btn-mini", target="_blank"})) + : Html.ActionLink("Match", "Match", new {id = item.FuelLogId}, + new {@class = "btn btn-warning btn-mini", + target="_blank", + matchcount=Url.Action("AvailableMatchCount", "FuelLog", new {id = item.FuelLogId}) + })) )), htmlAttributes: new { @class = "table table-striped table-bordered table-hover table-condensed"}, numericLinksCount: 20 diff --git a/Web/Views/FuelLog/MatchLink.cshtml b/Web/Views/FuelLog/MatchLink.cshtml index e56c1fa..8ae2e39 100644 --- a/Web/Views/FuelLog/MatchLink.cshtml +++ b/Web/Views/FuelLog/MatchLink.cshtml @@ -8,5 +8,9 @@ } else { - @Html.ActionLink("Match", "Match", new {id = Model.FuelLogId}, new {@class = "btn btn-warning btn-mini", target = "_blank"}) + @Html.ActionLink("Match", "Match", new {id = Model.FuelLogId}, + new {@class = "btn btn-warning btn-mini", + target="_blank", + matchcount=Url.Action("AvailableMatchCount", "FuelLog", new {id = Model.FuelLogId}) + }) }