hn-dotnet/Apps/Client/Pages/Show.razor

68 lines
1.2 KiB
Plaintext

@page "/links/{id:guid}"
@inject LinksClient Links
@inject NotificationManager Notifications
@if (Item == null)
{
<p>Loading...</p>
}
else
{
<h1>@Item.Url</h1>
<div>
👍 @Item.UpVotes
👎 @Item.DownVotes
</div>
@if (Comments == null)
{
<p>Loading comments ...</p>
} else if(Comments.Count == 0)
{
<p>No comment yet!</p>
} else
{
foreach (var comment in Comments)
{
<Comment @key="comment.Id" Item="@comment" />
}
}
<AuthorizeView>
<CommentForm OnSubmit="@SubmitComment" Username="@context.User.Identity.Name" />
</AuthorizeView>
}
@code {
[Parameter]
public Guid Id { get; set; }
private LinkDto Item;
private ICollection<CommentDto> Comments;
protected override async Task OnInitializedAsync()
{
Item = await Links.GetLinkByIdAsync(Id);
await FetchComments();
}
protected async Task FetchComments()
{
Comments = await Links.CommentsAsync(Id);
}
private async Task SubmitComment(AddCommentViewModel command)
{
try
{
await Links.AddCommentAsync(Id, command);
await FetchComments();
Notifications.Add("comment added!");
}
catch
{
Notifications.Add("could not post a comment");
}
}
}