Skip to content

Commit

Permalink
#383 - User property for summary sorting.
Browse files Browse the repository at this point in the history
  • Loading branch information
maraf committed Jun 4, 2022
1 parent f8da1b5 commit 0402c8d
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Money.Api/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
"RequireUppercase": false
}
},
"UserProperties": [ "PriceDecimalDigits", "DateFormat", "MobileMenu" ]
"UserProperties": [ "PriceDecimalDigits", "DateFormat", "MobileMenu", "SummarySort" ]
}
3 changes: 2 additions & 1 deletion src/Money.Blazor.Host/Pages/Summary.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Money.Models.Queries;
using Money.Models.Sorting;
using Money.Services;
using Money.Queries;
using Neptuo;
using Neptuo.Commands;
using Neptuo.Events;
Expand Down Expand Up @@ -75,10 +76,10 @@ protected override async Task OnInitializedAsync()
{
Log.Debug("Summary.OnInitializedAsync");
BindEvents();
SortDescriptor = new SortDescriptor<SummarySortType>(SummarySortType.ByCategory, SortDirection.Ascending);

await base.OnInitializedAsync();

SortDescriptor = await Queries.QueryAsync(new GetSummarySortProperty());
formatter = await CurrencyFormatterFactory.CreateAsync();
}

Expand Down
22 changes: 22 additions & 0 deletions src/Money.Blazor.Host/Pages/Users/Settings.razor
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,26 @@
<Buttons>
<button class="btn btn-primary" @onclick="SetMobileMenuAsync">Set</button>
</Buttons>
</Modal>

<Modal @ref="SummarySortEditor" TitleIcon="@SummarySort.Icon" Title="Set summary sort">
<ChildContent>
<div class="form-group">
<label for="summary-sort-property">Field:</label>
<select class="form-control" id="summary-sort-property" @bind="SummarySortProperty">
<option value="ByAmount">Amount</option>
<option value="ByCategory">Category</option>
</select>
</div>
<div class="form-group">
<label for="summary-sort-order">Direction:</label>
<select class="form-control" id="summary-sort-order" @bind="SummarySortDirection">
<option value="Ascending">Ascending</option>
<option value="Descending">Descending</option>
</select>
</div>
</ChildContent>
<Buttons>
<button class="btn btn-primary" @onclick="SetSummarySortAsync">Set</button>
</Buttons>
</Modal>
20 changes: 20 additions & 0 deletions src/Money.Blazor.Host/Pages/Users/Settings.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ public partial class Settings : System.IDisposable,
protected List<IAvailableMenuItemModel> MobileMenuAvailableModels { get; set; }
protected List<string> MobileSelectedIdentifiers { get; set; }

protected PropertyViewModel SummarySort { get; set; }
protected Modal SummarySortEditor { get; set; }
protected string SummarySortProperty { get; set; }
protected string SummarySortDirection { get; set; }

protected List<UserPropertyModel> Models { get; set; }
protected List<PropertyViewModel> ViewModels { get; } = new List<PropertyViewModel>();

Expand All @@ -54,6 +59,7 @@ protected async override Task OnInitializedAsync()
PriceDecimals = AddProperty("PriceDecimalDigits", "Price decimal digits", () => PriceDecimalsEditor.Show(), icon: "pound-sign", defaultValue: "2");
DateFormat = AddProperty("DateFormat", "Date format", () => DateFormatEditor.Show(), icon: "calendar-day", defaultValue: CultureInfo.CurrentUICulture.DateTimeFormat.ShortDatePattern);
MobileMenu = AddProperty("MobileMenu", "Mobile menu", () => MobileMenuEditor.Show(), icon: "mobile");
SummarySort = AddProperty("SummarySort", "Summary sort", () => SummarySortEditor.Show(), icon: "sort-alpha-down", defaultValue: "ByCategory-Ascending");

MobileMenuAvailableModels = await Queries.QueryAsync(new ListAvailableMenuItem());

Expand All @@ -62,6 +68,13 @@ protected async override Task OnInitializedAsync()
MobileSelectedIdentifiers = MobileMenu.CurrentValue != null
? MobileMenu.CurrentValue.Split(',').ToList()
: new List<string>(0);

if (SummarySort.CurrentValue != null)
{
string[] parts = SummarySort.CurrentValue.Split('-');
SummarySortProperty = parts[0];
SummarySortDirection = parts[1];
}
}

public void Dispose()
Expand Down Expand Up @@ -91,6 +104,13 @@ protected async Task SetMobileMenuAsync()
MobileMenuEditor.Hide();
}

protected async Task SetSummarySortAsync()
{
SummarySort.CurrentValue = $"{SummarySortProperty}-{SummarySortDirection}";
await SummarySort.SetAsync();
SummarySortEditor.Hide();
}

Task IEventHandler<UserPropertyChanged>.HandleAsync(UserPropertyChanged payload)
{
var viewModel = ViewModels.FirstOrDefault(vm => vm.Key == payload.PropertyKey);
Expand Down
18 changes: 18 additions & 0 deletions src/Money.Blazor.Host/Queries/GetSummarySortProperty.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Money.Models.Sorting;
using Money.Pages;
using Neptuo.Queries;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Money.Queries
{
/// <summary>
/// A query for getting a user defined number of decimal digits or default value.
/// </summary>
public class GetSummarySortProperty : IQuery<SortDescriptor<SummarySortType>>
{ }
}
18 changes: 18 additions & 0 deletions src/Money.Blazor.Host/Services/UserPropertyQueryHandler.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using Money.Models.Queries;
using Money.Models.Sorting;
using Money.Queries;
using Money.Pages;
using Neptuo.Queries;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.IO;
using System.Linq;
Expand All @@ -25,6 +28,21 @@ public async Task<object> ExecuteAsync(object query, HttpQueryDispatcher dispatc
var value = IntPropertyValue(await dispatcher.QueryAsync(new FindUserProperty("PriceDecimalDigits")), 2);
return value;
}
else if (query is GetSummarySortProperty)
{
var value = await dispatcher.QueryAsync(new FindUserProperty("SummarySort"));
if (String.IsNullOrEmpty(value))
value = "ByCategory-Ascending";

string[] parts = value.Split('-');

var result = new SortDescriptor<SummarySortType>(
Enum.Parse<SummarySortType>(parts[0], true),
Enum.Parse<SortDirection>(parts[1], true)
);

return result;
}

return await next(query);
}
Expand Down

0 comments on commit 0402c8d

Please sign in to comment.