Skip to content

Commit

Permalink
#288 - Refactor UserState init to base component.
Browse files Browse the repository at this point in the history
  • Loading branch information
maraf committed Sep 16, 2023
1 parent 90892c6 commit 52a6ed5
Show file tree
Hide file tree
Showing 31 changed files with 37 additions and 94 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;

namespace Neptuo.Recollections.Accounts.Components;

public abstract class UserStateComponentBase : ComponentBase
{
[CascadingParameter]
protected UserState UserState { get; set; }

protected async override Task OnInitializedAsync()
{
await base.OnInitializedAsync();
await UserState.EnsureInitializedAsync();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
@using Neptuo.Recollections.Accounts
@page "/connections"
@layout Commons.Layouts.MainLayout
@inherits UserStateComponentBase

<DocumentTitle Value="Connections" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ public partial class Connections
[Inject]
protected Api Api { get; set; }

[CascadingParameter]
protected UserState UserState { get; set; }

protected Modal ShareModal { get; set; }
protected ConnectionModel ShareConnection { get; set; }

Expand All @@ -30,11 +27,7 @@ public partial class Connections

protected async override Task OnInitializedAsync()
{
IsLoading = true;

await base.OnInitializedAsync();
await UserState.EnsureAuthenticatedAsync();

await LoadDataAsync();
}

Expand Down
1 change: 1 addition & 0 deletions src/Recollections.Blazor.UI/Accounts/Pages/Login.razor
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@page "/login"
@inherits UserStateComponentBase

<DocumentTitle Value="Login" />

Expand Down
6 changes: 1 addition & 5 deletions src/Recollections.Blazor.UI/Accounts/Pages/Login.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ public partial class Login
[Inject]
protected ILog<Login> Log { get; set; }

[CascadingParameter]
protected UserState UserState { get; set; }

[Required]
protected string UserName { get; set; }

Expand All @@ -34,8 +31,7 @@ public partial class Login
protected async override Task OnInitializedAsync()
{
await base.OnInitializedAsync();

await UserState.EnsureInitializedAsync();

if (UserState.IsAuthenticated)
Navigator.OpenTimeline();
}
Expand Down
1 change: 1 addition & 0 deletions src/Recollections.Blazor.UI/Accounts/Pages/Profile.razor
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
@page "/users/{UserId}"
@layout Commons.Layouts.MainLayout
@inherits UserStateComponentBase

<DocumentTitle Value="@(Owner?.Name ?? UserId)" />

Expand Down
9 changes: 0 additions & 9 deletions src/Recollections.Blazor.UI/Accounts/Pages/Profile.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,13 @@ public partial class Profile
[Inject]
protected UiOptions UiOptions { get; set; }

[CascadingParameter]
protected UserState UserState { get; set; }

[Parameter]
public string UserId { get; set; }

protected ProfileModel Model { get; set; }
protected OwnerModel Owner { get; set; }
protected PermissionContainerState Permissions { get; } = new PermissionContainerState();

protected async override Task OnInitializedAsync()
{
await base.OnInitializedAsync();
await UserState.EnsureInitializedAsync();
}

public async override Task SetParametersAsync(ParameterView parameters)
{
var oldUserId = UserId;
Expand Down
3 changes: 2 additions & 1 deletion src/Recollections.Blazor.UI/Accounts/Pages/Register.razor
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@page "/register"
@page "/register"
@inherits UserStateComponentBase

<DocumentTitle Value="Register" />

Expand Down
4 changes: 0 additions & 4 deletions src/Recollections.Blazor.UI/Accounts/Pages/Register.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ public partial class Register
[Inject]
protected Navigator Navigator { get; set; }

[CascadingParameter]
protected UserState UserState { get; set; }

public List<string> ErrorMessages { get; } = new List<string>();

public string UserName { get; set; }
Expand All @@ -29,7 +26,6 @@ protected async override Task OnInitializedAsync()
{
await base.OnInitializedAsync();

await UserState.EnsureInitializedAsync();
if (UserState.IsAuthenticated)
Navigator.OpenTimeline();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<Modal @ref="Modal" Title="Select an Entry" CssClass="entry-picker">
@inherits UserStateComponentBase

<Modal @ref="Modal" Title="Select an Entry" CssClass="entry-picker">
<ChildContent>
<div class="timeline">
<ListView Items="@Entries" EmptyMessage="You don't have any entry..." Context="entry">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ public partial class EntryPicker
[Parameter]
public Action<TimelineEntryModel> Selected { get; set; }

[CascadingParameter]
protected UserState UserState { get; set; }

protected Modal Modal { get; set; }

private int offset;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@page "/beings/{BeingId}"
@inherits UserStateComponentBase

@if (Model != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ public partial class BeingDetail
[Inject]
protected Navigator Navigator { get; set; }

[CascadingParameter]
protected UserState UserState { get; set; }

private string previousBeingId;

[Parameter]
Expand All @@ -36,12 +33,6 @@ public partial class BeingDetail

protected BeingIconPicker IconPicker { get; set; }

protected async override Task OnInitializedAsync()
{
await base.OnInitializedAsync();
await UserState.EnsureInitializedAsync();
}

public override Task SetParametersAsync(ParameterView parameters)
{
previousBeingId = BeingId;
Expand Down
1 change: 1 addition & 0 deletions src/Recollections.Blazor.UI/Entries/Pages/Beings.razor
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@page "/beings"
@inherits UserStateComponentBase

<DocumentTitle Value="Beings" />

Expand Down
5 changes: 0 additions & 5 deletions src/Recollections.Blazor.UI/Entries/Pages/Beings.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ public partial class Beings
[Inject]
protected Api Api { get; set; }

[CascadingParameter]
protected UserState UserState { get; set; }

protected bool IsLoading { get; set; }

public string Name { get; set; }
Expand All @@ -36,8 +33,6 @@ protected async override Task OnInitializedAsync()
IsLoading = true;

await base.OnInitializedAsync();
await UserState.EnsureAuthenticatedAsync();

await LoadDataAsync();
}

Expand Down
1 change: 1 addition & 0 deletions src/Recollections.Blazor.UI/Entries/Pages/Calendar.razor
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
@page "/calendar"
@page "/calendar/{Year:int}"
@page "/calendar/{Year:int}/{Month:int}"
@inherits UserStateComponentBase


<DocumentTitle Value="@Title" />
Expand Down
9 changes: 0 additions & 9 deletions src/Recollections.Blazor.UI/Entries/Pages/Calendar.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ public partial class Calendar
[Inject]
protected Navigator Navigator { get; set; }

[CascadingParameter]
protected UserState UserState { get; set; }

[Parameter]
public int? Year { get; set; }

Expand All @@ -44,12 +41,6 @@ public partial class Calendar

protected List<CalendarEntryModel> Models { get; } = new List<CalendarEntryModel>();

protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();
await UserState.EnsureAuthenticatedAsync();
}

public override async Task SetParametersAsync(ParameterView parameters)
{
int? prevYear = Year;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@page "/entries/{EntryId}"
@inherits UserStateComponentBase

<NavigationLock OnBeforeInternalNavigation="OnBeforeInternalNavigation" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ public partial class EntryDetail
[Inject]
protected IExceptionHandler ExceptionHandler { get; set; }

[CascadingParameter]
protected UserState UserState { get; set; }

private string previousEntryId;

[Parameter]
Expand Down Expand Up @@ -90,7 +87,6 @@ protected async override Task OnInitializedAsync()
Log.Debug("OnInitializedAsync");

await base.OnInitializedAsync();
await UserState.EnsureInitializedAsync();

if (UserState.IsAuthenticated)
PoiToggleButton = new PoiToggleButton(Navigator, Properties, UserState);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@page "/entries/{EntryId}/images/{ImageId}"
@inherits UserStateComponentBase

@if (Model != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,13 @@ public partial class ImageDetail
[Parameter]
public string ImageId { get; set; }

[CascadingParameter]
protected UserState UserState { get; set; }

private ImageModel original;
protected ImageModel Model { get; set; }
protected OwnerModel Owner { get; set; }
protected List<MapMarkerModel> Markers { get; } = new List<MapMarkerModel>();

protected PermissionContainerState Permissions { get; } = new PermissionContainerState();

protected async override Task OnInitializedAsync()
{
await base.OnInitializedAsync();
await UserState.EnsureInitializedAsync();
}

public override Task SetParametersAsync(ParameterView parameters)
{
previousImageId = ImageId;
Expand Down
5 changes: 3 additions & 2 deletions src/Recollections.Blazor.UI/Entries/Pages/Map.razor
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
@layout Commons.Layouts.HeadLayout
@page "/map"
@page "/map"
@layout Commons.Layouts.HeadLayout
@inherits UserStateComponentBase

<DocumentTitle Value="Map" />

Expand Down
4 changes: 0 additions & 4 deletions src/Recollections.Blazor.UI/Entries/Pages/Map.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ public partial class Map
[Inject]
protected PropertyCollection Properties { get; set; }

[CascadingParameter]
protected UserState UserState { get; set; }

protected List<MapEntryModel> Entries { get; set; } = new List<MapEntryModel>();
protected List<MapMarkerModel> Markers { get; } = new List<MapMarkerModel>();

Expand All @@ -37,7 +34,6 @@ protected async override Task OnInitializedAsync()
PoiToggleButton = new PoiToggleButton(Navigator, Properties, UserState);

await base.OnInitializedAsync();
await UserState.EnsureAuthenticatedAsync();

await LoadAsync();
}
Expand Down
1 change: 1 addition & 0 deletions src/Recollections.Blazor.UI/Entries/Pages/Search.razor
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@page "/search"
@inherits UserStateComponentBase

<DocumentTitle Value="Search" />

Expand Down
4 changes: 0 additions & 4 deletions src/Recollections.Blazor.UI/Entries/Pages/Search.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ public partial class Search : IDisposable
[Inject]
protected ILog<Search> Log { get; set; }

[CascadingParameter]
protected UserState UserState { get; set; }

[Inject]
protected Api Api { get; set; }

Expand All @@ -49,7 +46,6 @@ public partial class Search : IDisposable
protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();
await UserState.EnsureAuthenticatedAsync();
Navigator.LocationChanged += OnLocationChanged;
}

Expand Down
1 change: 1 addition & 0 deletions src/Recollections.Blazor.UI/Entries/Pages/Stories.razor
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@page "/stories"
@inherits UserStateComponentBase

<DocumentTitle Value="Stories" />

Expand Down
5 changes: 0 additions & 5 deletions src/Recollections.Blazor.UI/Entries/Pages/Stories.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ public partial class Stories
[Inject]
protected Api Api { get; set; }

[CascadingParameter]
protected UserState UserState { get; set; }

protected bool IsLoading { get; set; }

public string Title { get; set; }
Expand All @@ -36,8 +33,6 @@ protected async override Task OnInitializedAsync()
IsLoading = true;

await base.OnInitializedAsync();
await UserState.EnsureAuthenticatedAsync();

await LoadDataAsync();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@page "/stories/{StoryId}"
@inherits UserStateComponentBase

@if (Model != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ public partial class StoryDetail
[Inject]
protected IExceptionHandler ExceptionHandler { get; set; }

[CascadingParameter]
protected UserState UserState { get; set; }

private string previousStoryId;

[Parameter]
Expand All @@ -43,12 +40,6 @@ public partial class StoryDetail
protected OwnerModel Owner { get; set; }
protected PermissionContainerState Permissions { get; } = new PermissionContainerState();

protected async override Task OnInitializedAsync()
{
await base.OnInitializedAsync();
await UserState.EnsureInitializedAsync();
}

public override Task SetParametersAsync(ParameterView parameters)
{
previousStoryId = StoryId;
Expand Down
1 change: 1 addition & 0 deletions src/Recollections.Blazor.UI/Entries/Pages/Timeline.razor
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@page "/"
@inherits UserStateComponentBase

<DocumentTitle Value="Timeline" />

Expand Down
Loading

0 comments on commit 52a6ed5

Please sign in to comment.