@using Shogi.Contracts.Types @using System.ComponentModel.DataAnnotations @using System.Net @inject IShogiApi ShogiApi; @inject ShogiSocket ShogiSocket;
@if (!sessions.Any()) {

No games exist

} @foreach (var session in sessions) { }

Start a new session

@if (createSessionStatusCode == HttpStatusCode.Created) { } else if (createSessionStatusCode == HttpStatusCode.Conflict) { }
@code { [Parameter] public Action? ActiveSessionChanged { get; set; } CreateForm createForm = new(); SessionMetadata[] sessions = Array.Empty(); SessionMetadata? activeSession; HttpStatusCode? createSessionStatusCode; protected override async Task OnInitializedAsync() { ShogiSocket.OnCreateGameMessage += async (sender, message) => await FetchSessions(); await FetchSessions(); } string ActiveCss(SessionMetadata s) => s == activeSession ? "active" : string.Empty; void OnClickSession(SessionMetadata s) { activeSession = s; ActiveSessionChanged?.Invoke(s); } async Task FetchSessions() { var sessions = await ShogiApi.GetSessions(); if (sessions != null) { this.sessions = sessions.PlayerHasJoinedSessions.Concat(sessions.AllOtherSessions).ToArray(); } } async Task CreateSession() { createSessionStatusCode = await ShogiApi.PostSession(createForm.Name, createForm.IsPrivate); } private class CreateForm { [Required] public string Name { get; set; } = string.Empty; public bool IsPrivate { get; set; } } }