From 700a06e7e06159fb22e183121e440b962bd7d62b Mon Sep 17 00:00:00 2001 From: Julien LEICHER Date: Sun, 27 Dec 2020 12:29:46 +0100 Subject: [PATCH] some testing with blazor --- Apps/Client/Pages/Container.razor | 22 ++++++++++++++++++++ Apps/Client/Pages/Index.razor | 34 +++++++++++++++++++++++-------- Apps/Client/Pages/Show.razor | 24 ++++++++++++++++++++++ Apps/Client/Program.cs | 17 +++++++++++++--- Apps/Client/Shared/Link.razor | 2 +- Apps/Client/Shared/Names.razor | 15 ++++++++++++++ 6 files changed, 101 insertions(+), 13 deletions(-) create mode 100644 Apps/Client/Pages/Container.razor create mode 100644 Apps/Client/Pages/Show.razor create mode 100644 Apps/Client/Shared/Names.razor diff --git a/Apps/Client/Pages/Container.razor b/Apps/Client/Pages/Container.razor new file mode 100644 index 0000000..16c7768 --- /dev/null +++ b/Apps/Client/Pages/Container.razor @@ -0,0 +1,22 @@ +@page "/container" +@inject MyState State + +@foreach (var name in State.Names) +{ +

@name

+} + + + + + + +@code { + private string CurrentValue; + + private void OnSend() + { + State.AddName(CurrentValue); + CurrentValue = ""; + } +} \ No newline at end of file diff --git a/Apps/Client/Pages/Index.razor b/Apps/Client/Pages/Index.razor index dfc6d9c..bc5ee0d 100644 --- a/Apps/Client/Pages/Index.razor +++ b/Apps/Client/Pages/Index.razor @@ -1,23 +1,39 @@ @page "/" @inject LinksClient Links -

Hello, world!

+

Latest links

-Welcome to your new app. - -@foreach (var link in _links) +@if (_loading) { - +

Loading...

+} +else +{ + } - - - @code { private LinkDto[] _links = new LinkDto[] { }; + private bool _loading = false; protected override async Task OnInitializedAsync() { - _links = (await Links.GetLinksAsync()).ToArray(); + _loading = true; + + try + { + _links = (await Links.GetLinksAsync()).ToArray(); + } + finally + { + _loading = false; + } } } \ No newline at end of file diff --git a/Apps/Client/Pages/Show.razor b/Apps/Client/Pages/Show.razor new file mode 100644 index 0000000..73701ac --- /dev/null +++ b/Apps/Client/Pages/Show.razor @@ -0,0 +1,24 @@ +@page "/links/{id:guid}" +@inject LinksClient Links + +@if (Item == null) +{ +

Loading...

+} +else +{ +

Showing link @Item.Url with @Item.UpVotes / @Item.DownVotes

+} + +@code { + [Parameter] + public Guid Id { get; set; } + + private LinkDto Item; + + protected override async Task OnInitializedAsync() + { + Item = await Links.GetLinkByIdAsync(Id); + } + +} \ No newline at end of file diff --git a/Apps/Client/Program.cs b/Apps/Client/Program.cs index af45f1f..be64455 100644 --- a/Apps/Client/Program.cs +++ b/Apps/Client/Program.cs @@ -2,14 +2,24 @@ using System; using System.Net.Http; using System.Collections.Generic; using System.Threading.Tasks; -using System.Text; using Microsoft.AspNetCore.Components.WebAssembly.Hosting; -using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; namespace Client { + public class MyState + { + public List Names { get; } = new List(); + + public event Action OnChange; + + public void AddName(string name) + { + Names.Add(name); + OnChange?.Invoke(); + } + } + public class Program { public static async Task Main(string[] args) @@ -19,6 +29,7 @@ namespace Client builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri("http://localhost:8888/") }); builder.Services.AddScoped(); + builder.Services.AddSingleton(); await builder.Build().RunAsync(); } diff --git a/Apps/Client/Shared/Link.razor b/Apps/Client/Shared/Link.razor index 1b2d468..2959b71 100644 --- a/Apps/Client/Shared/Link.razor +++ b/Apps/Client/Shared/Link.razor @@ -1,4 +1,4 @@ -

@Item.Url

+@Item.Url @code { [Parameter] diff --git a/Apps/Client/Shared/Names.razor b/Apps/Client/Shared/Names.razor new file mode 100644 index 0000000..f1e1e71 --- /dev/null +++ b/Apps/Client/Shared/Names.razor @@ -0,0 +1,15 @@ +@inject MyState State + +

Got @State.Names.Count

+ +@code { + protected override void OnInitialized() + { + State.OnChange += StateHasChanged; + } + + public void Dispose() + { + State.OnChange -= StateHasChanged; + } +} \ No newline at end of file