fixed Home Page endless loading on 0 articles

This commit is contained in:
Mia Rose Winter 2024-01-21 17:26:22 +01:00
parent f23ce5261c
commit 305d60fd74
Signed by: miawinter
GPG key ID: 4B6F6A83178F595E

View file

@ -12,40 +12,42 @@
<h1 class="text-3xl lg:text-5xl font-light mb-3">@Localizer["Title"]</h1>
@if (Articles.Count < 1) {
<!-- TODO: somehow get status message -->
@if (Busy) {
<div class="flex place-content-center h-full text-primary">
<span class="loading loading-spinner loading-lg"></span>
</div>
}
@if (Articles.FirstOrDefault() is {} featured) {
<article class="mb-6">
<a href="/article/@featured.Id">
<div class="hero bg-neutral text-neutral-content">
<div class="hero-content">
<div class="flex flex-col space-y-6">
<h2 class="text-2xl lg:text-4xl font-bold">
@featured.Title<br />
<small class="text-sm">@featured.PublishDate.ToString("g")</small>
</h2>
<p class="line-clamp-6">
@featured.Body[..Math.Min(400, featured.Body.Length)]
</p>
<div class="flex">
<ProfilePill Profile="featured.Author" />
} else {
@if (Articles.FirstOrDefault() is {} featured) {
<article class="mb-6">
<a href="/article/@featured.Id">
<div class="hero bg-neutral text-neutral-content">
<div class="hero-content">
<div class="flex flex-col space-y-6">
<h2 class="text-2xl lg:text-4xl font-bold">
@featured.Title<br />
<small class="text-sm">@featured.PublishDate.ToString("g")</small>
</h2>
<p class="line-clamp-6">
@featured.Body[..Math.Min(400, featured.Body.Length)]
</p>
<div class="flex">
<ProfilePill Profile="featured.Author" />
</div>
</div>
</div>
</div>
</div>
</a>
</article>
}
<div class="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 xl:grid-cols-5 gap-x-8 gap-y-4 mb-6">
@foreach (var article in Articles.Skip(1)) {
<ArticleTile Article="article" />
</a>
</article>
}
</div>
<div class="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 xl:grid-cols-5 gap-x-8 gap-y-4 mb-6">
@foreach (var article in Articles.Skip(1)) {
<ArticleTile Article="article" />
}
</div>
}
@if (HasMore) {
<div class="flex place-content-center">
@ -59,36 +61,45 @@
private List<Article> Articles { get; } = [];
private bool HasMore { get; set; }
private bool Busy { get; set; } = true;
protected override async Task OnAfterRenderAsync(bool firstRender) {
if (firstRender) {
try {
await using var context = await ContextFactory.CreateDbContextAsync();
var now = DateTimeOffset.UtcNow;
var query = context.Set<Article>()
.Include(a => a.Author)
.Where(a => a.Status >= ArticleStatus.Published && a.PublishDate <= now)
.OrderByDescending(a => a.PublishDate);
var articles = await query.Take(11).ToListAsync();
HasMore = (await query.CountAsync()) > 11;
Articles.AddRange(articles);
} finally {
Busy = false;
await InvokeAsync(StateHasChanged);
}
}
}
private async Task More() {
try {
Busy = HasMore = true;
await using var context = await ContextFactory.CreateDbContextAsync();
var now = DateTimeOffset.UtcNow;
var query = context.Set<Article>()
.Include(a => a.Author)
.Where(a => a.Status >= ArticleStatus.Published && a.PublishDate <= now)
.OrderByDescending(a => a.PublishDate);
var articles = await query.Take(11).ToListAsync();
HasMore = (await query.CountAsync()) > 11;
.OrderByDescending(a => a.PublishDate)
.Skip(Articles.Count);
var articles = await query.Take(10).ToListAsync();
Articles.AddRange(articles);
await InvokeAsync(StateHasChanged);
HasMore = (await query.CountAsync()) > 10;
} finally {
Busy = false;
}
}
private async Task More() {
HasMore = false;
await using var context = await ContextFactory.CreateDbContextAsync();
var now = DateTimeOffset.UtcNow;
var query = context.Set<Article>()
.Include(a => a.Author)
.Where(a => a.Status >= ArticleStatus.Published && a.PublishDate <= now)
.OrderByDescending(a => a.PublishDate)
.Skip(Articles.Count);
var articles = await query.Take(10).ToListAsync();
Articles.AddRange(articles);
HasMore = (await query.CountAsync()) > 10;
}
}