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 59c8910..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 cffc1d5..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 d344881..b006fc8 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,25 +879,16 @@ function replenishSupplies() { } }); } -function showTableColumns(e, isExtraField) { +function showTableColumns(e, tabName) { //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(); } + saveUserColumnPreferences(tabName); } function searchTableRows(tabName) { Swal.fire({ @@ -925,4 +916,34 @@ 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(); + } + }); +} +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