C
C#9mo ago
Naarfy

Blazor page reloading after db update ?

I have a comments table. I querry my Users table to get the DateTIme where the user last saw the comments and I group the comments based on that to get oldComments, newComments. Then I update my user. The issue I'm having it my new comment shows up for half a second in "New comments" before moving to "Old Comments"
protected override async Task OnInitializedAsync()
{
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
comments = await dbContext.Comments.ToListAsync();
totalComments = comments.Count;
var user = authState.User;

if (user.Identity.IsAuthenticated)
{
loggedInUserGuid = user.FindFirst(System.Security.Claims.ClaimTypes.NameIdentifier)?.Value;
}

appUser = await dbContext.Users.FindAsync(loggedInUserGuid);
lastSeenComments = appUser.LastSeenComments;
var groupedComments = comments.GroupBy(c => c.CreatedAt <= lastSeenComments)
.ToDictionary(g => g.Key, g => g.ToList());

oldComments = groupedComments.GetValueOrDefault(true, new List<Comment>());
newComments = groupedComments.GetValueOrDefault(false, new List<Comment>());

appUser.LastSeenComments = DateTime.UtcNow;
dbContext.Users.Update(appUser);
await dbContext.SaveChangesAsync();

}
protected override async Task OnInitializedAsync()
{
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
comments = await dbContext.Comments.ToListAsync();
totalComments = comments.Count;
var user = authState.User;

if (user.Identity.IsAuthenticated)
{
loggedInUserGuid = user.FindFirst(System.Security.Claims.ClaimTypes.NameIdentifier)?.Value;
}

appUser = await dbContext.Users.FindAsync(loggedInUserGuid);
lastSeenComments = appUser.LastSeenComments;
var groupedComments = comments.GroupBy(c => c.CreatedAt <= lastSeenComments)
.ToDictionary(g => g.Key, g => g.ToList());

oldComments = groupedComments.GetValueOrDefault(true, new List<Comment>());
newComments = groupedComments.GetValueOrDefault(false, new List<Comment>());

appUser.LastSeenComments = DateTime.UtcNow;
dbContext.Users.Update(appUser);
await dbContext.SaveChangesAsync();

}
1 Reply
Naarfy
NaarfyOP9mo ago
@if (oldComments != null && oldComments.Any())
{
<div>
<h3>Old Comments</h3>
<ul>
@foreach (var comment in oldComments)
{
<li>
<div>
<strong>@comment.Author</strong>
<span>@comment.CreatedAt</span>
</div>
<p>@comment.Content</p>
</li>
}
</ul>
</div>
}

@if (newComments != null && newComments.Any())
{
<div>
<h3>New Comments</h3>
<ul>
@foreach (var comment in newComments)
{
<li>
<div>
<strong>@comment.Author</strong>
<span>@comment.CreatedAt</span>
</div>
<p>@comment.Content</p>
</li>
}
</ul>
</div>
}
@if (oldComments != null && oldComments.Any())
{
<div>
<h3>Old Comments</h3>
<ul>
@foreach (var comment in oldComments)
{
<li>
<div>
<strong>@comment.Author</strong>
<span>@comment.CreatedAt</span>
</div>
<p>@comment.Content</p>
</li>
}
</ul>
</div>
}

@if (newComments != null && newComments.Any())
{
<div>
<h3>New Comments</h3>
<ul>
@foreach (var comment in newComments)
{
<li>
<div>
<strong>@comment.Author</strong>
<span>@comment.CreatedAt</span>
</div>
<p>@comment.Content</p>
</li>
}
</ul>
</div>
}
Ok I solved it by adding a time buffer "
int timeBuffer = 5;

var groupedComments = comments.GroupBy(c => (DateTime.UtcNow - c.CreatedAt).TotalSeconds > timeBuffer)
.ToDictionary(g => g.Key, g => g.ToList());
int timeBuffer = 5;

var groupedComments = comments.GroupBy(c => (DateTime.UtcNow - c.CreatedAt).TotalSeconds > timeBuffer)
.ToDictionary(g => g.Key, g => g.ToList());
I'm open to suggestions if it's not the right way to go
Want results from more Discord servers?
Add your server