Skip to content

Commit ef769cd

Browse files
committed
Allow todo lists to have different names than id's - change registry grain to be state-based journaled
1 parent 47bc8a5 commit ef769cd

19 files changed

+143
-78
lines changed

orleans/JournaledTodoList/JournaledTodoList.WebApp/Components/Layout/NavBar.razor

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
<i class="bi bi-house-door me-2"></i> Home
66
</NavLink>
77
</li>
8-
@foreach (var listId in TodoLists)
8+
@foreach (var list in TodoLists)
99
{
1010
<li class="nav-item">
11-
<NavLink class="nav-link" href="@($"todolist/{listId}")">
12-
<i class="bi bi-list-task me-1"></i> @listId
11+
<NavLink class="nav-link" href="@($"todolist/{list.Id}")">
12+
<i class="bi bi-list-task me-1"></i> @list.Name
1313
</NavLink>
1414
</li>
1515
}

orleans/JournaledTodoList/JournaledTodoList.WebApp/Components/Layout/NavBar.razor.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@ public partial class NavBar(TodoListService todoListService) : ITodoListRegistry
88
{
99
private IDisposable? subscription;
1010

11-
private ImmutableArray<string> TodoLists { get; set; } = [];
11+
private ImmutableArray<TodoListReference> TodoLists { get; set; } = [];
1212

1313
protected override async Task OnInitializedAsync()
1414
{
15-
TodoLists = await todoListService.GetAllTodoListsAsync();
1615
subscription = await todoListService.SubscribeAsync(this);
1716
}
1817

@@ -21,7 +20,7 @@ public void Dispose()
2120
subscription?.Dispose();
2221
}
2322

24-
async Task ITodoListRegistryObserver.OnTodoListsChanged(ImmutableArray<string> todoLists) => await InvokeAsync(() =>
23+
async Task ITodoListRegistryObserver.OnTodoListsChanged(ImmutableArray<TodoListReference> todoLists) => await InvokeAsync(() =>
2524
{
2625
TodoLists = todoLists;
2726
StateHasChanged();

orleans/JournaledTodoList/JournaledTodoList.WebApp/Components/Pages/HomePage.razor

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
</div>
1515

1616
<div class="list-group">
17-
@foreach (var listId in todoLists)
17+
@foreach (var list in TodoLists)
1818
{
19-
<a href="/todolist/@listId" class="list-group-item list-group-item-action">
20-
@listId
19+
<a href="/todolist/@(list.Id)" class="list-group-item list-group-item-action">
20+
@list.Name
2121
</a>
2222
}
2323
</div>
Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,40 @@
11
using System.Collections.Immutable;
2+
using JournaledTodoList.WebApp.Grains;
23
using JournaledTodoList.WebApp.Services;
34

45
namespace JournaledTodoList.WebApp.Components.Pages;
56

6-
public partial class HomePage(TodoListService todoListService)
7+
public partial class HomePage(TodoListService todoListService) : ITodoListRegistryObserver, IDisposable
78
{
9+
private IDisposable? subscription;
810
private string newListName = "";
9-
private ImmutableArray<string> todoLists = [];
11+
12+
private ImmutableArray<TodoListReference> TodoLists { get; set; } = [];
1013

1114
protected override async Task OnInitializedAsync()
1215
{
13-
todoLists = await todoListService.GetAllTodoListsAsync();
14-
await CreateNewList("Default");
16+
subscription = await todoListService.SubscribeAsync(this);
1517
}
1618

19+
public void Dispose()
20+
{
21+
subscription?.Dispose();
22+
}
23+
24+
async Task ITodoListRegistryObserver.OnTodoListsChanged(ImmutableArray<TodoListReference> todoLists) => await InvokeAsync(() =>
25+
{
26+
TodoLists = todoLists;
27+
StateHasChanged();
28+
});
29+
1730
private async Task CreateNewList(string listName)
1831
{
19-
var normalizedName = NormalizeListName(listName);
20-
if (string.IsNullOrWhiteSpace(normalizedName) || todoLists.Contains(normalizedName))
32+
if (string.IsNullOrWhiteSpace(listName) || TodoLists.Any(x => x.Name == listName))
2133
{
2234
return;
2335
}
2436

25-
await todoListService.CreateTodoListAsync(normalizedName);
26-
todoLists = await todoListService.GetAllTodoListsAsync();
37+
await todoListService.CreateTodoListAsync(listName);
2738
newListName = "";
2839
}
29-
30-
private static string NormalizeListName(string name)
31-
{
32-
// Replace spaces and special characters to ensure valid URL
33-
return name.Trim()
34-
.Replace(" ", "-")
35-
.Replace("/", "-")
36-
.Replace("\\", "-");
37-
}
3840
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
public static class Constants
2+
{
3+
public const string StateStorageProviderName = "StateStorage";
4+
}

orleans/JournaledTodoList/JournaledTodoList.WebApp/Grains/Events/TodoItemAdded.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[GenerateSerializer, Immutable]
44
public record class TodoItemAdded(int ItemId, DateTimeOffset Timestamp, string Title)
5-
: TodoListEvent(ItemId, Timestamp)
5+
: TodoListEvent(Timestamp)
66
{
77
public override string GetDescription() => $"Added item {ItemId}: {Title}";
88
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
namespace JournaledTodoList.WebApp.Grains.Events;
22

33
[GenerateSerializer, Immutable]
4-
public record class TodoItemRemoved(int ItemId, DateTimeOffset Timestamp) : TodoListEvent(ItemId, Timestamp)
4+
public record class TodoItemRemoved(int ItemId, DateTimeOffset Timestamp) : TodoListEvent(Timestamp)
55
{
66
public override string GetDescription() => $"Removed item {ItemId}";
77
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
namespace JournaledTodoList.WebApp.Grains.Events;
22

33
[GenerateSerializer, Immutable]
4-
public record class TodoItemToggled(int ItemId, DateTimeOffset Timestamp) : TodoListEvent(ItemId, Timestamp)
4+
public record class TodoItemToggled(int ItemId, DateTimeOffset Timestamp) : TodoListEvent(Timestamp)
55
{
66
public override string GetDescription() => $"Toggled completion status of item {ItemId}";
77
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
namespace JournaledTodoList.WebApp.Grains.Events;
22

33
[GenerateSerializer, Immutable]
4-
public record class TodoItemUpdated(int ItemId, DateTimeOffset Timestamp, string Title) : TodoListEvent(ItemId, Timestamp)
4+
public record class TodoItemUpdated(int ItemId, DateTimeOffset Timestamp, string Title) : TodoListEvent(Timestamp)
55
{
66
public override string GetDescription() => $"Updated item {ItemId}: {Title}";
77
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
namespace JournaledTodoList.WebApp.Grains.Events;
22

33
[GenerateSerializer, Immutable]
4-
public abstract record class TodoListEvent(int ItemId, DateTimeOffset Timestamp)
4+
public abstract record class TodoListEvent(DateTimeOffset Timestamp)
55
{
66
public abstract string GetDescription();
77
}

0 commit comments

Comments
 (0)