From 4b5dcb1c7e736714770f96119b28eefb34d17466 Mon Sep 17 00:00:00 2001 From: "DESKTOP-GENO133\\IvanPlex" Date: Sat, 2 Mar 2024 05:47:22 -0700 Subject: [PATCH 1/2] revamped column check toggle behavior --- Views/Vehicle/_CollisionRecords.cshtml | 12 ++++---- Views/Vehicle/_Gas.cshtml | 18 +++++------ Views/Vehicle/_OdometerRecords.cshtml | 8 ++--- Views/Vehicle/_ServiceRecords.cshtml | 12 ++++---- Views/Vehicle/_SupplyRecords.cshtml | 16 +++++----- Views/Vehicle/_TaxRecords.cshtml | 10 +++--- Views/Vehicle/_UpgradeRecords.cshtml | 12 ++++---- wwwroot/js/shared.js | 42 +++++++++++++++----------- 8 files changed, 69 insertions(+), 61 deletions(-) diff --git a/Views/Vehicle/_CollisionRecords.cshtml b/Views/Vehicle/_CollisionRecords.cshtml index 59c8910..d37ac20 100644 --- a/Views/Vehicle/_CollisionRecords.cshtml +++ b/Views/Vehicle/_CollisionRecords.cshtml @@ -48,31 +48,31 @@
  • @@ -81,7 +81,7 @@ var elementId = Guid.NewGuid(); diff --git a/Views/Vehicle/_Gas.cshtml b/Views/Vehicle/_Gas.cshtml index cffc1d5..67b2358 100644 --- a/Views/Vehicle/_Gas.cshtml +++ b/Views/Vehicle/_Gas.cshtml @@ -95,49 +95,49 @@
  • @@ -146,7 +146,7 @@ var elementId = Guid.NewGuid(); diff --git a/Views/Vehicle/_OdometerRecords.cshtml b/Views/Vehicle/_OdometerRecords.cshtml index 07bf713..50582f4 100644 --- a/Views/Vehicle/_OdometerRecords.cshtml +++ b/Views/Vehicle/_OdometerRecords.cshtml @@ -47,19 +47,19 @@
  • @@ -68,7 +68,7 @@ var elementId = Guid.NewGuid(); diff --git a/Views/Vehicle/_ServiceRecords.cshtml b/Views/Vehicle/_ServiceRecords.cshtml index d689712..a55728b 100644 --- a/Views/Vehicle/_ServiceRecords.cshtml +++ b/Views/Vehicle/_ServiceRecords.cshtml @@ -48,31 +48,31 @@
  • @@ -81,7 +81,7 @@ var elementId = Guid.NewGuid(); diff --git a/Views/Vehicle/_SupplyRecords.cshtml b/Views/Vehicle/_SupplyRecords.cshtml index 329cdd9..28aacdf 100644 --- a/Views/Vehicle/_SupplyRecords.cshtml +++ b/Views/Vehicle/_SupplyRecords.cshtml @@ -48,43 +48,43 @@
  • @@ -93,7 +93,7 @@ var elementId = Guid.NewGuid(); diff --git a/Views/Vehicle/_TaxRecords.cshtml b/Views/Vehicle/_TaxRecords.cshtml index a9c5ef9..ffcbd77 100644 --- a/Views/Vehicle/_TaxRecords.cshtml +++ b/Views/Vehicle/_TaxRecords.cshtml @@ -48,25 +48,25 @@
  • @@ -75,7 +75,7 @@ var elementId = Guid.NewGuid(); diff --git a/Views/Vehicle/_UpgradeRecords.cshtml b/Views/Vehicle/_UpgradeRecords.cshtml index 26a50b3..178e174 100644 --- a/Views/Vehicle/_UpgradeRecords.cshtml +++ b/Views/Vehicle/_UpgradeRecords.cshtml @@ -48,31 +48,31 @@
  • @@ -81,7 +81,7 @@ var elementId = Guid.NewGuid(); diff --git a/wwwroot/js/shared.js b/wwwroot/js/shared.js index d344881..d69094d 100644 --- a/wwwroot/js/shared.js +++ b/wwwroot/js/shared.js @@ -847,7 +847,7 @@ function replenishSupplies() { var quantitybeforeRepl = globalParseFloat($('#supplyRecordQuantity').val()); if (isNaN(replquantity) || (replcost.trim() != '' && isNaN(parsedReplCost))) { Swal.showValidationMessage(`Please enter a valid quantity and cost`); - } else if (replcost.trim() == '' && (isNaN(quantitybeforeRepl) || quantitybeforeRepl == 0)){ + } else if (replcost.trim() == '' && (isNaN(quantitybeforeRepl) || quantitybeforeRepl == 0)) { Swal.showValidationMessage(`Unable to use unit cost calculation, please provide cost`); } return { replquantity, replcost, parsedReplCost } @@ -879,24 +879,14 @@ function replenishSupplies() { } }); } -function showTableColumns(e, isExtraField) { +function showTableColumns(e) { //logic for extra field since we dont hardcode the data-column type - if (isExtraField) { - var showColumn = $(e).is(':checked'); - var columnName = $(e).parent().find('.form-check-label').text(); - if (showColumn) { - $(`[data-column='${columnName}']`).show(); - } else { - $(`[data-column='${columnName}']`).hide(); - } + var showColumn = $(e).is(':checked'); + var columnName = $(e).attr('data-column-toggle'); + if (showColumn) { + $(`[data-column='${columnName}']`).show(); } else { - var showColumn = $(e).is(':checked'); - var columnName = $(e).attr('data-column-toggle'); - if (showColumn) { - $(`[data-column='${columnName}']`).show(); - } else { - $(`[data-column='${columnName}']`).hide(); - } + $(`[data-column='${columnName}']`).hide(); } } function searchTableRows(tabName) { @@ -925,4 +915,22 @@ function searchTableRows(tabName) { updateAggregateLabels(); } }); +} +function loadUserColumnPreferences(columns) { + if (columns.length == 0) { + //user has no preference saved, reset to default + return; + } + //uncheck all columns + $(".col-visible-toggle").prop("checked", false); + //hide all columns + $('[data-column]').hide(); + //toggle visibility of each column + columns.map(x => { + var defaultColumn = $(`[data-column-toggle='${x}'].col-visible-toggle`); + if (defaultColumn.length > 0) { + defaultColumn.prop("checked", true); + $(`[data-column='${x}']`).show(); + } + }); } \ No newline at end of file From bf14e4c8c0d0fe7f0123459a44b318980ad09887 Mon Sep 17 00:00:00 2001 From: "DESKTOP-GENO133\\IvanPlex" Date: Sat, 2 Mar 2024 06:49:40 -0700 Subject: [PATCH 2/2] added functonality to persist column visibility --- Controllers/HomeController.cs | 4 ++++ Controllers/VehicleController.cs | 24 ++++++++++++++++++++- Models/User/UserColumnPreference.cs | 8 +++++++ Models/UserConfig.cs | 1 + Views/Vehicle/_CollisionRecords.cshtml | 19 ++++++++++------ Views/Vehicle/_Gas.cshtml | 24 ++++++++++++--------- Views/Vehicle/_OdometerRecords.cshtml | 15 ++++++++----- Views/Vehicle/_ServiceRecords.cshtml | 17 +++++++++------ Views/Vehicle/_SupplyRecords.cshtml | 23 ++++++++++++-------- Views/Vehicle/_TaxRecords.cshtml | 17 +++++++++------ Views/Vehicle/_UpgradeRecords.cshtml | 19 ++++++++++------ Views/Vehicle/_UserColumnPreferences.cshtml | 9 ++++++++ appsettings.json | 1 + wwwroot/js/shared.js | 15 ++++++++++++- 14 files changed, 144 insertions(+), 52 deletions(-) create mode 100644 Models/User/UserColumnPreference.cs create mode 100644 Views/Vehicle/_UserColumnPreferences.cshtml diff --git a/Controllers/HomeController.cs b/Controllers/HomeController.cs index 73e3654..205d5fd 100644 --- a/Controllers/HomeController.cs +++ b/Controllers/HomeController.cs @@ -101,6 +101,10 @@ namespace CarCareTracker.Controllers [HttpPost] public IActionResult WriteToSettings(UserConfig userConfig) { + //retrieve existing userConfig. + var existingConfig = _config.GetUserConfig(User); + //copy over stuff that persists + userConfig.UserColumnPreferences = existingConfig.UserColumnPreferences; var result = _config.SaveUserConfig(User, userConfig); return Json(result); } diff --git a/Controllers/VehicleController.cs b/Controllers/VehicleController.cs index 6bfef10..2bef090 100644 --- a/Controllers/VehicleController.cs +++ b/Controllers/VehicleController.cs @@ -2448,7 +2448,29 @@ namespace CarCareTracker.Controllers } return Json(result); } + [HttpPost] + public IActionResult SaveUserColumnPreferences(UserColumnPreference columnPreference) + { + try + { + var userConfig = _config.GetUserConfig(User); + var existingUserColumnPreference = userConfig.UserColumnPreferences.Where(x => x.Tab == columnPreference.Tab); + if (existingUserColumnPreference.Any()) + { + var existingPreference = existingUserColumnPreference.Single(); + existingPreference.VisibleColumns = columnPreference.VisibleColumns; + } else + { + userConfig.UserColumnPreferences.Add(columnPreference); + } + var result = _config.SaveUserConfig(User, userConfig); + return Json(result); + } catch (Exception ex) + { + _logger.LogError(ex.Message); + return Json(false); + } + } #endregion - } } diff --git a/Models/User/UserColumnPreference.cs b/Models/User/UserColumnPreference.cs new file mode 100644 index 0000000..18bd80e --- /dev/null +++ b/Models/User/UserColumnPreference.cs @@ -0,0 +1,8 @@ +namespace CarCareTracker.Models +{ + public class UserColumnPreference + { + public ImportMode Tab { get; set; } + public List VisibleColumns { get; set; } = new List(); + } +} \ No newline at end of file diff --git a/Models/UserConfig.cs b/Models/UserConfig.cs index fb1d503..69a662c 100644 --- a/Models/UserConfig.cs +++ b/Models/UserConfig.cs @@ -17,6 +17,7 @@ public bool EnableExtraFieldColumns { get; set; } public string PreferredGasUnit { get; set; } = string.Empty; public string PreferredGasMileageUnit { get; set; } = string.Empty; + public List UserColumnPreferences { get; set; } = new List(); public string UserNameHash { get; set; } public string UserPasswordHash { get; set;} public string UserLanguage { get; set; } = "en_US"; diff --git a/Views/Vehicle/_CollisionRecords.cshtml b/Views/Vehicle/_CollisionRecords.cshtml index d37ac20..b09b041 100644 --- a/Views/Vehicle/_CollisionRecords.cshtml +++ b/Views/Vehicle/_CollisionRecords.cshtml @@ -12,6 +12,7 @@ { extraFields = Model.SelectMany(x => x.ExtraFields).Select(y => y.Name).Distinct().ToList(); } + var userColumnPreferences = userConfig.UserColumnPreferences.Where(x => x.Tab == ImportMode.RepairRecord); } @model List
    @@ -48,31 +49,31 @@
  • @@ -81,7 +82,7 @@ var elementId = Guid.NewGuid(); @@ -156,4 +157,8 @@
  • @translator.Translate(userLanguage, "Duplicate")
  • @translator.Translate(userLanguage, "Delete")
  • - \ No newline at end of file + +@if (userColumnPreferences.Any()) +{ + @await Html.PartialAsync("_UserColumnPreferences", userColumnPreferences) +} \ No newline at end of file diff --git a/Views/Vehicle/_Gas.cshtml b/Views/Vehicle/_Gas.cshtml index 67b2358..5f2ef41 100644 --- a/Views/Vehicle/_Gas.cshtml +++ b/Views/Vehicle/_Gas.cshtml @@ -46,6 +46,7 @@ { extraFields = Model.GasRecords.SelectMany(x => x.ExtraFields).Select(y => y.Name).Distinct().ToList(); } + var userColumnPreferences = userConfig.UserColumnPreferences.Where(x => x.Tab == ImportMode.GasRecord); }
    @@ -95,49 +96,49 @@
  • @@ -146,7 +147,7 @@ var elementId = Guid.NewGuid(); @@ -222,7 +223,10 @@
  • @translator.Translate(userLanguage, "Duplicate")
  • @translator.Translate(userLanguage, "Delete")
  • - +@if (userColumnPreferences.Any()) +{ + @await Html.PartialAsync("_UserColumnPreferences", userColumnPreferences) +} \ No newline at end of file diff --git a/appsettings.json b/appsettings.json index d8112be..e5b28aa 100644 --- a/appsettings.json +++ b/appsettings.json @@ -20,6 +20,7 @@ "UseThreeDecimalGasCost": true, "UseMarkDownOnSavedNotes": false, "PreferredGasMileageUnit": "", + "UserColumnPreferences": [], "PreferredGasUnit": "", "UserLanguage": "en_US", "VisibleTabs": [ 0, 1, 4, 2, 3, 6, 5, 8 ], diff --git a/wwwroot/js/shared.js b/wwwroot/js/shared.js index d69094d..b006fc8 100644 --- a/wwwroot/js/shared.js +++ b/wwwroot/js/shared.js @@ -879,7 +879,7 @@ function replenishSupplies() { } }); } -function showTableColumns(e) { +function showTableColumns(e, tabName) { //logic for extra field since we dont hardcode the data-column type var showColumn = $(e).is(':checked'); var columnName = $(e).attr('data-column-toggle'); @@ -888,6 +888,7 @@ function showTableColumns(e) { } else { $(`[data-column='${columnName}']`).hide(); } + saveUserColumnPreferences(tabName); } function searchTableRows(tabName) { Swal.fire({ @@ -933,4 +934,16 @@ function loadUserColumnPreferences(columns) { $(`[data-column='${x}']`).show(); } }); +} +function saveUserColumnPreferences(importMode) { + var visibleColumns = $('.col-visible-toggle:checked').map((index, elem) => $(elem).attr('data-column-toggle')).toArray(); + var columnPreference = { + tab: importMode, + visibleColumns: visibleColumns + }; + $.post('/Vehicle/SaveUserColumnPreferences', { columnPreference: columnPreference }, function (data) { + if (!data) { + errorToast(genericErrorMessage()); + } + }); } \ No newline at end of file