From bf14e4c8c0d0fe7f0123459a44b318980ad09887 Mon Sep 17 00:00:00 2001 From: "DESKTOP-GENO133\\IvanPlex" Date: Sat, 2 Mar 2024 06:49:40 -0700 Subject: [PATCH] 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