Skip to content

Commit

Permalink
Start working on showing blog posts
Browse files Browse the repository at this point in the history
  • Loading branch information
codemonkey85 committed Dec 18, 2024
1 parent 37a77c4 commit 99e6f0b
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 1 deletion.
15 changes: 15 additions & 0 deletions AboutMe/Wasm/Pages/Blog.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
@page "/Blog"
@inject HttpClient Http

<h3>@feedTitle</h3>
<ul>
@foreach (var post in posts)
{
<li>
<strong>@post.Title</strong><br/>
Date Published: @post.DatePublished.ToLocalTime().ToString("f")
<div>@(new MarkupString(post.ContentHtml))</div>
<a href="@post.Url" target="_blank">Read more</a>
</li>
}
</ul>
68 changes: 68 additions & 0 deletions AboutMe/Wasm/Pages/Blog.razor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System.Text.Json;
using System.Text.Json.Serialization;

namespace AboutMe.Wasm.Pages;

// ReSharper disable once UnusedType.Global
public partial class Blog
{
private string feedTitle = string.Empty;
private List<Item> posts = [];

protected override async Task OnInitializedAsync()
{
try
{
const string feedUrl = "https://micro.bondcodes.com/feed.json";
var jsonResponse = await Http.GetStringAsync(feedUrl);

var feed = JsonSerializer.Deserialize<Feed>(jsonResponse,
new JsonSerializerOptions { PropertyNameCaseInsensitive = true });

if (feed != null)
{
feedTitle = feed.Title;
posts = feed.Items.Take(10).ToList(); // Get the last 10 posts
}
}
catch (Exception ex)
{
Console.WriteLine($"Error loading feed: {ex.Message}");
}
}
}

[JsonSerializable(typeof(Feed))]
public class Feed

Check warning on line 36 in AboutMe/Wasm/Pages/Blog.razor.cs

View workflow job for this annotation

GitHub Actions / Build and Deploy

The type 'AboutMe.Wasm.Pages.Feed' has been annotated with JsonSerializableAttribute but does not derive from JsonSerializerContext. No source code will be generated. (https://learn.microsoft.com/dotnet/fundamentals/syslib-diagnostics/syslib1224)

Check warning on line 36 in AboutMe/Wasm/Pages/Blog.razor.cs

View workflow job for this annotation

GitHub Actions / Build and Deploy

The type 'AboutMe.Wasm.Pages.Feed' has been annotated with JsonSerializableAttribute but does not derive from JsonSerializerContext. No source code will be generated. (https://learn.microsoft.com/dotnet/fundamentals/syslib-diagnostics/syslib1224)
{
[JsonPropertyName("version")]
public string Version { get; set; } = "";
[JsonPropertyName("title")]
public string Title { get; set; } = "";
[JsonPropertyName("icon")]
public string Icon { get; set; } = "";
[JsonPropertyName("home_page_url")]
public string HomePageUrl { get; set; } = "";
[JsonPropertyName("feed_url")]
public string FeedUrl { get; set; } = "";

// ReSharper disable once CollectionNeverUpdated.Global
[JsonPropertyName("items")]
public List<Item> Items { get; set; } = [];
}

// ReSharper disable once ClassNeverInstantiated.Global
public class Item
{
// ReSharper disable once UnusedMember.Global
[JsonPropertyName("id")]
public string Id { get; set; } = "";
[JsonPropertyName("title")]
public string Title { get; set; } = "";
[JsonPropertyName("content_text")]
public string ContentHtml { get; set; } = "";
[JsonPropertyName("date_published")]
public DateTime DatePublished { get; set; }
[JsonPropertyName("url")]
public string Url { get; set; } = "";
}
5 changes: 4 additions & 1 deletion AboutMe/Wasm/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
var builder = WebAssemblyHostBuilder.CreateDefault(args);
var services = builder.Services;

builder.Services.AddMudServices();
services
.AddMudServices()
.AddScoped(_ => new HttpClient { BaseAddress = new(builder.HostEnvironment.BaseAddress) });

builder.RootComponents.Add<App>("#app");
builder.RootComponents.Add<HeadOutlet>("head::after");
Expand Down

0 comments on commit 99e6f0b

Please sign in to comment.