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

@@ -6,6 +6,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Blazored.Toast" Version="3.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="5.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="5.0.1" PrivateAssets="all" />
<PackageReference Include="Microsoft.AspNetCore.WebUtilities" Version="2.2.0" />

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}");
}
}

View File

@@ -1,13 +1,10 @@
using Blazored.Toast;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NodaTime;
using NodaTime.Serialization.SystemTextJson;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
@@ -23,6 +20,7 @@ namespace JobsJobsJobs.Client
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
builder.Services.AddSingleton(new AppState());
builder.Services.AddSingleton(new JsonSerializerOptions().ConfigureForNodaTime(DateTimeZoneProviders.Tzdb));
builder.Services.AddBlazoredToast();
await builder.Build().RunAsync();
}
}

View File

@@ -131,7 +131,7 @@ namespace JobsJobsJobs.Client
/// </summary>
/// <typeparam name="T">The type of item expected</typeparam>
/// <param name="http">The HTTP client to use for server communication</param>
/// <param name="url">The API URL to use call</param>
/// <param name="url">The API URL to call</param>
/// <returns>A result with the items, or an error if one occurs</returns>
/// <remarks>The caller is responsible for setting the JWT on the HTTP client</remarks>
public static async Task<Result<IEnumerable<T>>> RetrieveMany<T>(HttpClient http, string url)
@@ -150,5 +150,29 @@ namespace JobsJobsJobs.Client
return Result<IEnumerable<T>>.AsError($"Unable to parse result: {ex.Message}");
}
}
/// <summary>
/// Retrieve one item from the given URL
/// </summary>
/// <typeparam name="T">The type of item expected</typeparam>
/// <param name="http">The HTTP client to use for server communication</param>
/// <param name="url">The API URL to call</param>
/// <returns>A result with the item (possibly null), or an error if one occurs</returns>
/// <remarks>The caller is responsible for setting the JWT on the HTTP client</remarks>
public static async Task<Result<T?>> RetrieveOne<T>(HttpClient http, string url)
{
try
{
return Result<T?>.AsOk(await http.GetFromJsonAsync<T>($"/api/{url}", _serializerOptions));
}
catch (HttpRequestException ex)
{
return Result<T?>.AsError(ex.Message);
}
catch (JsonException ex)
{
return Result<T?>.AsError($"Unable to parse result: {ex.Message}");
}
}
}
}

View File

@@ -0,0 +1,23 @@
@using NodaTime
@using NodaTime.Text
@using System.Globalization
@Translated
@code {
/// <summary>
/// The pattern with which dates will be formatted
/// </summary>
private static InstantPattern pattern = InstantPattern.Create("ld<D> ' at ' lt<t>", CultureInfo.CurrentCulture);
/// <summary>
/// The date to be formatted
/// </summary>
[Parameter]
public Instant TheDate { get; set; }
/// <summary>
/// The formatted date
/// </summary>
private string Translated => pattern.Format(TheDate);
}

View File

@@ -1,5 +1,6 @@
@inherits LayoutComponentBase
@inject IJSRuntime js
@using Blazored.Toast.Configuration
<div class="page">
<div class="sidebar">
@@ -20,6 +21,8 @@
</div>
</div>
<BlazoredToasts Position="ToastPosition.BottomRight"
ShowProgressBar="true" />
@code {
async void PlayJobs() => await js.InvokeVoidAsync("Audio.play", "pelosijobs");

View File

@@ -1,4 +1,6 @@
@using System.Net.Http
@using Blazored.Toast
@using Blazored.Toast.Services
@using System.Net.Http
@using System.Net.Http.Json
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Routing

View File

@@ -2,13 +2,14 @@
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<title>JobsJobsJobs</title>
<base href="/" />
<link href="css/bootstrap/bootstrap.min.css" rel="stylesheet" />
<link href="css/app.css" rel="stylesheet" />
<link href="JobsJobsJobs.Client.styles.css" rel="stylesheet" />
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>JobsJobsJobs</title>
<base href="/">
<link href="css/bootstrap/bootstrap.min.css" rel="stylesheet">
<link href="css/app.css" rel="stylesheet">
<link href="JobsJobsJobs.Client.styles.css" rel="stylesheet">
<link href="_content/Blazored.Toast/blazored-toast.min.css" rel="stylesheet">
</head>
<body>