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> <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"> <div class="flex place-content-center h-full text-primary">
<span class="loading loading-spinner loading-lg"></span> <span class="loading loading-spinner loading-lg"></span>
</div> </div>
} } else {
@if (Articles.FirstOrDefault() is {} featured) {
@if (Articles.FirstOrDefault() is {} featured) { <article class="mb-6">
<article class="mb-6"> <a href="/article/@featured.Id">
<a href="/article/@featured.Id"> <div class="hero bg-neutral text-neutral-content">
<div class="hero bg-neutral text-neutral-content"> <div class="hero-content">
<div class="hero-content"> <div class="flex flex-col space-y-6">
<div class="flex flex-col space-y-6"> <h2 class="text-2xl lg:text-4xl font-bold">
<h2 class="text-2xl lg:text-4xl font-bold"> @featured.Title<br />
@featured.Title<br /> <small class="text-sm">@featured.PublishDate.ToString("g")</small>
<small class="text-sm">@featured.PublishDate.ToString("g")</small> </h2>
</h2> <p class="line-clamp-6">
<p class="line-clamp-6"> @featured.Body[..Math.Min(400, featured.Body.Length)]
@featured.Body[..Math.Min(400, featured.Body.Length)] </p>
</p> <div class="flex">
<div class="flex"> <ProfilePill Profile="featured.Author" />
<ProfilePill Profile="featured.Author" /> </div>
</div> </div>
</div> </div>
</div> </div>
</div> </a>
</a> </article>
</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" />
} }
</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) { @if (HasMore) {
<div class="flex place-content-center"> <div class="flex place-content-center">
@ -59,36 +61,45 @@
private List<Article> Articles { get; } = []; private List<Article> Articles { get; } = [];
private bool HasMore { get; set; } private bool HasMore { get; set; }
private bool Busy { get; set; } = true;
protected override async Task OnAfterRenderAsync(bool firstRender) { protected override async Task OnAfterRenderAsync(bool firstRender) {
if (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(); await using var context = await ContextFactory.CreateDbContextAsync();
var now = DateTimeOffset.UtcNow; var now = DateTimeOffset.UtcNow;
var query = context.Set<Article>() var query = context.Set<Article>()
.Include(a => a.Author) .Include(a => a.Author)
.Where(a => a.Status >= ArticleStatus.Published && a.PublishDate <= now) .Where(a => a.Status >= ArticleStatus.Published && a.PublishDate <= now)
.OrderByDescending(a => a.PublishDate); .OrderByDescending(a => a.PublishDate)
var articles = await query.Take(11).ToListAsync(); .Skip(Articles.Count);
HasMore = (await query.CountAsync()) > 11; var articles = await query.Take(10).ToListAsync();
Articles.AddRange(articles); 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;
}
} }