diff --git a/Controllers/VehicleController.cs b/Controllers/VehicleController.cs index 102535f..dcc0b9d 100644 --- a/Controllers/VehicleController.cs +++ b/Controllers/VehicleController.cs @@ -2055,15 +2055,11 @@ namespace CarCareTracker.Controllers { //check if template name already taken. var existingRecord = _planRecordTemplateDataAccess.GetPlanRecordTemplatesByVehicleId(planRecord.VehicleId).Where(x => x.Description == planRecord.Description).Any(); - if (existingRecord) + if (planRecord.Id == default && existingRecord) { return Json(new OperationResponse { Success = false, Message = "A template with that description already exists for this vehicle" }); } planRecord.Files = planRecord.Files.Select(x => { return new UploadedFiles { Name = x.Name, Location = _fileHelper.MoveFileFromTemp(x.Location, "documents/") }; }).ToList(); - if (planRecord.Supplies.Any() && planRecord.CopySuppliesAttachment) - { - planRecord.Files.AddRange(GetSuppliesAttachments(planRecord.Supplies)); - } var result = _planRecordTemplateDataAccess.SavePlanRecordTemplateToVehicle(planRecord); return Json(new OperationResponse { Success = result, Message = result ? "Template Added" : StaticHelper.GenericErrorMessage }); } @@ -2113,6 +2109,10 @@ namespace CarCareTracker.Controllers if (existingRecord.Supplies.Any()) { existingRecord.RequisitionHistory = RequisitionSupplyRecordsByUsage(existingRecord.Supplies, DateTime.Parse(existingRecord.DateCreated), existingRecord.Description); + if (existingRecord.CopySuppliesAttachment) + { + existingRecord.Files.AddRange(GetSuppliesAttachments(existingRecord.Supplies)); + } } var result = _planRecordDataAccess.SavePlanRecordToVehicle(existingRecord.ToPlanRecord()); return Json(new OperationResponse { Success = result, Message = result ? "Plan Record Added" : StaticHelper.GenericErrorMessage }); @@ -2210,6 +2210,12 @@ namespace CarCareTracker.Controllers return Json(result); } [HttpGet] + public IActionResult GetPlanRecordTemplateForEditById(int planRecordTemplateId) + { + var result = _planRecordTemplateDataAccess.GetPlanRecordTemplateById(planRecordTemplateId); + return PartialView("_PlanRecordTemplateEditModal", result); + } + [HttpGet] public IActionResult GetPlanRecordForEditById(int planRecordId) { var result = _planRecordDataAccess.GetPlanRecordById(planRecordId); diff --git a/Views/Vehicle/_PlanRecordModal.cshtml b/Views/Vehicle/_PlanRecordModal.cshtml index 0663f43..a4e4269 100644 --- a/Views/Vehicle/_PlanRecordModal.cshtml +++ b/Views/Vehicle/_PlanRecordModal.cshtml @@ -126,7 +126,8 @@ id: @Model.Id, dateCreated: decodeHTMLEntities('@(Model.DateCreated)'), reminderRecordId: decodeHTMLEntities('@Model.ReminderRecordId'), - createdFromReminder: @Model.CreatedFromReminder.ToString().ToLower() + createdFromReminder: @Model.CreatedFromReminder.ToString().ToLower(), + isTemplate: false } } \ No newline at end of file diff --git a/Views/Vehicle/_PlanRecordTemplateEditModal.cshtml b/Views/Vehicle/_PlanRecordTemplateEditModal.cshtml index d58f461..5fdce1c 100644 --- a/Views/Vehicle/_PlanRecordTemplateEditModal.cshtml +++ b/Views/Vehicle/_PlanRecordTemplateEditModal.cshtml @@ -21,10 +21,7 @@ - @if (isNew) - { - @await Html.PartialAsync("_SupplyStore", "PlanRecordTemplate") - } + @await Html.PartialAsync("_SupplyStore", "PlanRecordTemplate") - + @supplyRecord.Quantity @StaticHelper.TruncateStrings(supplyRecord.PartNumber) @StaticHelper.TruncateStrings(supplyRecord.Description) diff --git a/wwwroot/js/planrecord.js b/wwwroot/js/planrecord.js index 6178fb1..5303858 100644 --- a/wwwroot/js/planrecord.js +++ b/wwwroot/js/planrecord.js @@ -14,7 +14,8 @@ function showEditPlanRecordModal(planRecordId, nocache) { if (existingContent.trim() != '') { //check if id is same. var existingId = getPlanRecordModelData().id; - if (existingId == planRecordId && $('[data-changed=true]').length > 0) { + var isNotTemplate = !getPlanRecordModelData().isTemplate; + if (existingId == planRecordId && isNotTemplate && $('[data-changed=true]').length > 0) { $('#planRecordModal').modal('show'); $('.cached-banner').show(); return; @@ -37,7 +38,34 @@ function showEditPlanRecordModal(planRecordId, nocache) { }); } function showEditPlanRecordTemplateModal(planRecordTemplateId, nocache) { - + hidePlanRecordTemplatesModal(); + if (!nocache) { + var existingContent = $("#planRecordModalContent").html(); + if (existingContent.trim() != '') { + //check if id is same. + var existingId = getPlanRecordModelData().id; + var isTemplate = getPlanRecordModelData().isTemplate; + if (existingId == planRecordTemplateId && isTemplate && $('[data-changed=true]').length > 0) { + $('#planRecordModal').modal('show'); + $('.cached-banner').show(); + return; + } + } + } + $.get(`/Vehicle/GetPlanRecordTemplateForEditById?planRecordTemplateId=${planRecordTemplateId}`, function (data) { + if (data) { + $("#planRecordModalContent").html(data); + //initiate datepicker + initDatePicker($('#planRecordDate')); + $('#planRecordModal').modal('show'); + bindModalInputChanges('planRecordModal'); + $('#planRecordModal').off('shown.bs.modal').on('shown.bs.modal', function () { + if (getGlobalConfig().useMarkDown) { + toggleMarkDownOverlay("planRecordNotes"); + } + }); + } + }); } function hideAddPlanRecordModal() { $('#planRecordModal').modal('hide'); @@ -136,6 +164,7 @@ function deletePlannerRecordTemplate(planRecordTemplateId) { $("#workAroundInput").hide(); if (data) { successToast("Template Deleted"); + hideAddPlanRecordModal(); hidePlanRecordTemplatesModal(); } else { errorToast(genericErrorMessage()); @@ -146,7 +175,7 @@ function deletePlannerRecordTemplate(planRecordTemplateId) { } }); } -function savePlanRecordTemplate() { +function savePlanRecordTemplate(isEdit) { //get values var formValues = getAndValidatePlanRecordValues(); //validate @@ -157,7 +186,13 @@ function savePlanRecordTemplate() { //save to db. $.post('/Vehicle/SavePlanRecordTemplateToVehicleId', { planRecord: formValues }, function (data) { if (data.success) { - successToast(data.message); + if (isEdit) { + hideAddPlanRecordModal(); + showPlanRecordTemplatesModal(); + successToast('Plan Template Updated'); + } else { + successToast('Plan Template Added'); + } } else { errorToast(data.message); }