Changed Home Page to SSR (fixes #1)

This commit is contained in:
Mia Rose Winter 2024-02-21 22:51:11 +01:00
parent 16fde0644c
commit 587b073b20
Signed by: miawinter
GPG key ID: 4B6F6A83178F595E
2 changed files with 87 additions and 76 deletions

View file

@ -0,0 +1,24 @@
@typeparam T
@foreach (var item in Items) {
@ChildContent(item)
}
@code {
[SupplyParameterFromQuery]
public int Page { get; set; } = 0;
[Parameter]
public required RenderFragment<T> ChildContent { get; set; }
[Parameter]
public required Func<int, int, ValueTask<IEnumerable<T>>> LoadCallback { get; set; }
[Parameter]
public int ItemsPerPage { get; set; } = 10;
private List<T> Items { get; } = [];
protected override async Task OnInitializedAsync() {
foreach (var i in await LoadCallback(Page * ItemsPerPage, ItemsPerPage)) Items.Add(i);
}
}

View file

@ -4,13 +4,12 @@
@using Wave.Data @using Wave.Data
@using Wave.Utilities @using Wave.Utilities
@rendermode InteractiveServer
@attribute [StreamRendering]
@inject IOptions<Customization> Customizations @inject IOptions<Customization> Customizations
@inject IOptions<Features> Features @inject IOptions<Features> Features
@inject NavigationManager Navigation @inject NavigationManager Navigation
@inject IDbContextFactory<ApplicationDbContext> ContextFactory; @inject IDbContextFactory<ApplicationDbContext> ContextFactory;
@inject IStringLocalizer<Home> Localizer @inject IStringLocalizer<Home> Localizer
@inject IMessageDisplay Message
<HeadContent> <HeadContent>
<meta property="og:type" content="website"> <meta property="og:type" content="website">
@ -62,14 +61,7 @@
} }
</section> </section>
<!-- TODO: somehow get status message --> @if (Featured is {} featured) {
@if (Busy) {
<div class="flex place-content-center h-full text-primary">
<span class="loading loading-spinner loading-lg"></span>
</div>
} else {
@if (Articles.FirstOrDefault() is {} featured) {
<article class="mb-6"> <article class="mb-6">
<ArticleLink Article="featured"> <ArticleLink Article="featured">
<div class="hero bg-secondary text-secondary-content border-l-8 border-current shadow rounded-box"> <div class="hero bg-secondary text-secondary-content border-l-8 border-current shadow rounded-box">
@ -103,59 +95,54 @@
} }
<ArticleTileArray> <ArticleTileArray>
@foreach (var article in Articles.Skip(1)) { <PageComponent LoadCallback="LoadArticles">
<ArticleTile Article="article" /> <ArticleTile Article="context" />
} </PageComponent>
</ArticleTileArray> </ArticleTileArray>
}
@if (HasMore) { <div class="grid place-content-center my-3">
<div class="flex place-content-center"> <div class="join">
<button class="btn btn-wide" @onclick="More">@Localizer["More"]</button> <a class="join-item btn" href="/?page=@(Page-1)" disabled="@(Page < 1)">«</a>
<button class="join-item btn">Page @(Page+1)</button>
<a class="join-item btn" href="/?page=@(Page+1)" disabled="@(Page >= TotalPages-1)">»</a>
</div>
</div> </div>
}
@code { @code {
[CascadingParameter(Name = "TitlePrefix")] [CascadingParameter(Name = "TitlePrefix")]
private string TitlePrefix { get; set; } = default!; private string TitlePrefix { get; set; } = default!;
[SupplyParameterFromQuery]
public int Page { get; set; } = 0;
private List<Article> Articles { get; } = []; private int TotalPages { get; set; } = 1;
private bool HasMore { get; set; }
private bool Busy { get; set; } = true;
protected override async Task OnAfterRenderAsync(bool firstRender) { private Article? Featured { get; set; }
if (firstRender) {
protected override async Task OnInitializedAsync() {
try { try {
await using var context = await ContextFactory.CreateDbContextAsync(); await using var context = await ContextFactory.CreateDbContextAsync();
var query = context.Set<Article>() var query = context.Set<Article>()
.Include(a => a.Author).Include(a => a.Categories) .Include(a => a.Author).Include(a => a.Categories)
.OrderByDescending(a => a.PublishDate); .OrderByDescending(a => a.PublishDate).ThenBy(a => a.Id);
var articles = await query.Take(11).ToListAsync();
HasMore = (await query.CountAsync()) > 11; Featured = await query.FirstOrDefaultAsync();
Articles.AddRange(articles); TotalPages = (int) Math.Ceiling((await query.CountAsync() - 1) / 10.0);
} finally { } catch {
Busy = false; Message.ShowError(Localizer["Articles_Load_Error"]);
await InvokeAsync(StateHasChanged);
}
} }
} }
private async Task More() { private async ValueTask<IEnumerable<Article>> LoadArticles(int page, int count) {
try { try {
Busy = HasMore = true;
await using var context = await ContextFactory.CreateDbContextAsync(); await using var context = await ContextFactory.CreateDbContextAsync();
return await context.Set<Article>()
var query = context.Set<Article>()
.Include(a => a.Author).Include(a => a.Categories) .Include(a => a.Author).Include(a => a.Categories)
.OrderByDescending(a => a.PublishDate) .OrderByDescending(a => a.PublishDate).ThenBy(a => a.Id)
.Skip(Articles.Count); .Skip(page + 1).Take(count).ToListAsync();
var articles = await query.Take(10).ToListAsync(); } catch {
Articles.AddRange(articles); Message.ShowError(Localizer["Articles_Load_Error"]);
HasMore = (await query.CountAsync()) > 10; return [];
} finally {
Busy = false;
} }
} }
} }