Skip to content

Commit

Permalink
Storing selected client/patient in local storage and resuming with th…
Browse files Browse the repository at this point in the history
…ese values next time app loads
  • Loading branch information
ardalis committed Apr 2, 2021
1 parent d580d12 commit eefa323
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 14 deletions.
1 change: 1 addition & 0 deletions FrontDesk/src/FrontDesk.Blazor/FrontDesk.Blazor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Blazored.LocalStorage" Version="3.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="5.0.4" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="5.0.4" PrivateAssets="all" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="5.0.4" />
Expand Down
91 changes: 77 additions & 14 deletions FrontDesk/src/FrontDesk.Blazor/Pages/Index.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Blazored.LocalStorage;
using BlazorShared;
using BlazorShared.Models.Appointment;
using BlazorShared.Models.AppointmentType;
Expand Down Expand Up @@ -56,6 +57,9 @@ public partial class Index
[Inject]
SchedulerService SchedulerService { get; set; }

[Inject]
ISyncLocalStorageService LocalStorage { get; set; }

private bool IsShowEdit = false;
private bool IsLoaded = false;
private List<string> Groups = new List<string>();
Expand Down Expand Up @@ -89,6 +93,8 @@ public partial class Index
public bool IsConnected =>
hubConnection.State == HubConnectionState.Connected;

private ScheduleState _currentScheduleState = new();

protected override async Task OnInitializedAsync()
{
Logger.LogInformation("OnInitializedAsync()");
Expand All @@ -109,23 +115,36 @@ protected override async Task OnInitializedAsync()

Groups.Add("Rooms");

// check for saved client/patient Id
await LoadScheduleState();

IsLoaded = true;

await InitSignalR();
}

protected async Task ClientChanged(object selectedClientId)
{
if (selectedClientId == null)
try
{
// reset UI
Patients = new List<PatientDto>();
return;
}
ClientId = (int)selectedClientId;
Logger.LogInformation($"Client changed: {ClientId}");
if (selectedClientId == null || ((int)selectedClientId) == 0)
{
// reset UI
Patients = new List<PatientDto>();
return;
}
ClientId = (int)selectedClientId;
Logger.LogInformation($"Client changed: {ClientId}");

await GetClientPatientsAsync();
await GetClientPatientsAsync();
}
finally
{
if (ClientId != _currentScheduleState.SelectedClientId)
{
SaveScheduleState();
}
}
}

private async Task GetClientPatientsAsync()
Expand Down Expand Up @@ -160,15 +179,22 @@ private async Task AddPatientImages()

private void PatientChanged(object id)
{
if (id == null)
try
{
SelectedPatient = null;
return;
if (id == null)
{
SelectedPatient = null;
return;
}
PatientId = (int)id;
if (PatientId > 0)
{
SelectedPatient = Patients.FirstOrDefault(p => p.PatientId == PatientId);
}
}
PatientId = (int)id;
if (PatientId > 0)
finally
{
SelectedPatient = Patients.FirstOrDefault(p => p.PatientId == PatientId);
SaveScheduleState();
}
}

Expand Down Expand Up @@ -268,11 +294,48 @@ private void OpenEdit(AppointmentDto appointment)
return;
}
CustomEditFormShown = true;
SaveScheduleState();
}

public void Dispose()
{
_ = hubConnection.DisposeAsync();
}

public class ScheduleState
{
public int SelectedClientId { get; set; }
public int SelectedPatientId { get; set; }

public override string ToString()
{
return $"C{SelectedClientId}-P{SelectedPatientId}";
}
}

private void SaveScheduleState()
{
_currentScheduleState.SelectedClientId = ClientId;
_currentScheduleState.SelectedPatientId = PatientId;

Logger.LogInformation($"SaveScheduleState: {_currentScheduleState}");
LocalStorage.SetItem(nameof(ScheduleState), _currentScheduleState);
}

private async Task LoadScheduleState()
{
var currentState = LocalStorage.GetItem<ScheduleState>(nameof(ScheduleState));

if (currentState == null) return;

_currentScheduleState = currentState;
await ClientChanged(_currentScheduleState.SelectedClientId);

if (_currentScheduleState.SelectedPatientId > 0)
{
PatientId = _currentScheduleState.SelectedPatientId;
SelectedPatient = Patients.FirstOrDefault(p => p.PatientId == PatientId);
}
}
}
}
4 changes: 4 additions & 0 deletions FrontDesk/src/FrontDesk.Blazor/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Blazored.LocalStorage;
using BlazorShared;
using FrontDesk.Blazor.Services;
using FrontDesk.Blazor.Shared.SchedulerComponent;
Expand Down Expand Up @@ -39,6 +40,9 @@ public static Task Main(string[] args)
builder.Services.AddScoped<ToastService>();
builder.Services.AddScoped<SchedulerService>();

// Blazor WebAssembly
builder.Services.AddBlazoredLocalStorage();

// register the Telerik services
builder.Services.AddTelerikBlazor();

Expand Down

0 comments on commit eefa323

Please sign in to comment.