Add counts to Dashboard (#2)

Also refactored database parameters with a few extension methods; ready for profile view page
This commit is contained in:
2021-01-04 23:05:53 -05:00
parent fe3510b818
commit 97b3de1cea
15 changed files with 325 additions and 93 deletions

View File

@@ -1,47 +1,41 @@
@page "/citizen/dashboard"
@inject HttpClient http
@inject AppState state
<h3>Welcome, @state.User!.Name!</h3>
<h3>Welcome, @State.User!.Name!</h3>
@if (retrievingProfile)
@if (RetrievingData)
{
<p>Retrieving your employment profile...</p>
}
else if (profile != null)
{
<p>Your employment profile was last updated @profile.LastUpdatedOn</p>
}
else
{
<p>You do not have an employment profile established; click &ldquo;Profile&rdquo;* in the menu to get started!</p>
<p><em>* Once it's there...</em></p>
}
@if (errorMessage != null)
{
<p>@errorMessage</p>
}
@code {
bool retrievingProfile = true;
Profile? profile = null;
string? errorMessage = null;
protected override async Task OnInitializedAsync()
if (Profile != null)
{
<p>
Your employment profile was last updated <FullDateTime TheDate="@Profile.LastUpdatedOn" />. Your profile currently
lists @SkillCount skill@(SkillCount != 1 ? "s" : "").
</p>
}
else
{
<p>You do not have an employment profile established; click &ldquo;Profile&rdquo;* in the menu to get started!</p>
}
<p>
There @(ProfileCount == 1 ? "is" : "are") @(ProfileCount == 0 ? "no" : ProfileCount) employment
profile@(ProfileCount != 1 ? "s" : "") from citizens of Gitmo Nation.
@if (ProfileCount > 0)
{
if (state.User != null)
{
var profileResult = await ServerApi.RetrieveProfile(http, state);
if (profileResult.IsOk)
{
profile = profileResult.Ok;
}
else
{
errorMessage = profileResult.Error;
}
retrievingProfile = false;
}
<text>Take a look around and see if you can help them find work!</text>
}
</p>
}
@if (ErrorMessages.Count > 0)
{
<p><strong>The following error(s) occurred:</strong></p>
<p>
@foreach (var msg in ErrorMessages)
{
@msg<br>
}
</p>
}

View File

@@ -0,0 +1,96 @@
using JobsJobsJobs.Shared;
using JobsJobsJobs.Shared.Api;
using Microsoft.AspNetCore.Components;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
namespace JobsJobsJobs.Client.Pages.Citizen
{
/// <summary>
/// The first page a user sees after signing in
/// </summary>
public partial class Dashboard : ComponentBase
{
/// <summary>
/// Whether the data is being retrieved
/// </summary>
private bool RetrievingData { get; set; } = true;
/// <summary>
/// The user's profile
/// </summary>
private Profile? Profile { get; set; } = null;
/// <summary>
/// The number of skills in the user's profile
/// </summary>
private long SkillCount { get; set; } = 0L;
/// <summary>
/// The number of profiles
/// </summary>
private long ProfileCount { get; set; } = 0L;
/// <summary>
/// Error messages from data access
/// </summary>
private IList<string> ErrorMessages { get; } = new List<string>();
/// <summary>
/// The HTTP client to use for API access
/// </summary>
[Inject]
public HttpClient Http { get; set; } = default!;
/// <summary>
/// The current application state
/// </summary>
[Inject]
public AppState State { get; set; } = default!;
protected override async Task OnInitializedAsync()
{
if (State.User != null)
{
ServerApi.SetJwt(Http, State);
var profileTask = ServerApi.RetrieveProfile(Http, State);
var profileCountTask = ServerApi.RetrieveOne<Count>(Http, "profile/count");
var skillCountTask = ServerApi.RetrieveOne<Count>(Http, "profile/skill-count");
await Task.WhenAll(profileTask, profileCountTask, skillCountTask);
if (profileTask.Result.IsOk)
{
Profile = profileTask.Result.Ok;
}
else
{
ErrorMessages.Add(profileTask.Result.Error);
}
if (profileCountTask.Result.IsOk)
{
ProfileCount = profileCountTask.Result.Ok?.Value ?? 0;
}
else
{
ErrorMessages.Add(profileCountTask.Result.Error);
}
if (skillCountTask.Result.IsOk)
{
SkillCount = skillCountTask.Result.Ok?.Value ?? 0;
}
else
{
ErrorMessages.Add(skillCountTask.Result.Error);
}
RetrievingData = false;
}
}
}
}

View File

@@ -1,4 +1,5 @@
using JobsJobsJobs.Shared;
using Blazored.Toast.Services;
using JobsJobsJobs.Shared;
using JobsJobsJobs.Shared.Api;
using Microsoft.AspNetCore.Components;
using System.Collections.Generic;
@@ -51,6 +52,12 @@ namespace JobsJobsJobs.Client.Pages.Citizen
[Inject]
private AppState State { get; set; } = default!;
/// <summary>
/// Toast service
/// </summary>
[Inject]
private IToastService Toasts { get; set; } = default!;
protected override async Task OnInitializedAsync()
{
ServerApi.SetJwt(Http, State);
@@ -128,12 +135,13 @@ namespace JobsJobsJobs.Client.Pages.Citizen
var res = await Http.PostAsJsonAsync("/api/profile/save", ProfileForm);
if (res.IsSuccessStatusCode)
{
// TODO: success notification
Toasts.ShowSuccess("Profile Saved Successfully");
}
else
{
// TODO: probably not the best way to handle this...
ErrorMessages.Add(await res.Content.ReadAsStringAsync());
var error = await res.Content.ReadAsStringAsync();
if (!string.IsNullOrEmpty(error)) error = $"- {error}";
Toasts.ShowError($"{(int)res.StatusCode} {error}");
}
}