Skip to content

Commit

Permalink
Removed things
Browse files Browse the repository at this point in the history
- Use razor pages as the WASM host
- Removed client side routing
- Renamed the App component to Todo
- Added delete todo
  • Loading branch information
davidfowl committed Nov 27, 2022
1 parent f4b05b0 commit 90338b9
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 38 deletions.
5 changes: 0 additions & 5 deletions Todo.Web/Client/MainLayout.razor

This file was deleted.

3 changes: 0 additions & 3 deletions Todo.Web/Client/Program.cs
@@ -1,10 +1,7 @@
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Todo.Web.Client;

var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("#app");
builder.RootComponents.Add<HeadOutlet>("head::after");

builder.Services.AddHttpClient<TodoClient>(client =>
{
Expand Down
46 changes: 24 additions & 22 deletions Todo.Web/Client/App.razor → Todo.Web/Client/Todo.razor
@@ -1,7 +1,5 @@
@inject TodoClient Client

<h1>Todos</h1>

@if (todos is null)
{
if (statusCode is null)
Expand All @@ -27,28 +25,22 @@ else
{
<input type="button" value="Logout" onclick="@(() => Logout())" />

<section class="todoapp">
<header class="header">
<h1>todos</h1>
<form onsubmit="@(() => AddTodo())">
<input class="new-todo" placeholder="What needs to be done?" @bind="todoItem" />
</form>
</header>
<section class="main" style="display: block">
<ul class="todo-list">
<form onsubmit="@(() => AddTodo())">
<input class="new-todo" placeholder="What needs to be done?" @bind="todoItem" />
</form>
<ul class="todo-list">
@foreach (var todo in todos)
{
<li class=@(todo.IsComplete ? "completed" : "") @key="@todo.Id">
var id = todo.Id;
<li class=@(todo.IsComplete ? "completed" : "") @key="id">
<div class="view">
<input class="toggle" type="checkbox" checked="@todo.IsComplete" />
<label>@todo.Title</label>
<button class="destroy"></button>
<input class="toggle" type="checkbox" @bind="@todo.IsComplete" />
<label>@todo.Title</label>
<button class="destroy" onclick="@(() => DeleteTodo(id))"></button>
</div>
</li>
</li>
}
</ul>
</section>
</section>
</ul>
}

@code {
Expand All @@ -60,7 +52,7 @@ else

string? todoItem;

private async Task AddTodo()
async Task AddTodo()
{
if (todoItem is { Length: > 0 })
{
Expand All @@ -73,7 +65,17 @@ else
}
}

private async Task Logout()
async Task DeleteTodo(int id)
{
if (await Client.DeleteTodoAsync(id))
{
await OnInitializedAsync();

StateHasChanged();
}
}

async Task Logout()
{
if (await Client.LogoutAsync())
{
Expand All @@ -82,7 +84,7 @@ else
}
}

private async Task Login()
async Task Login()
{
if (await Client.LoginAsync(username, password))
{
Expand Down
6 changes: 6 additions & 0 deletions Todo.Web/Client/TodoClient.cs
Expand Up @@ -17,6 +17,12 @@ public async Task<bool> AddTodoAsync(string title)
return response.IsSuccessStatusCode;
}

public async Task<bool> DeleteTodoAsync(int id)
{
var response = await _client.DeleteAsync($"todos/{id}");
return response.IsSuccessStatusCode;
}

public async Task<(HttpStatusCode, TodoItem[]?)> GetTodosAsync()
{
var response = await _client.GetAsync("todos");
Expand Down
1 change: 0 additions & 1 deletion Todo.Web/Client/_Imports.razor
Expand Up @@ -5,4 +5,3 @@
@using Microsoft.AspNetCore.Components.Web
@using Microsoft.AspNetCore.Components.WebAssembly.Http
@using Microsoft.JSInterop
@using Todo.Web.Client
@@ -1,3 +1,10 @@
@page
@model Todo.Web.Server.Pages.IndexModel
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

@{
}

<!DOCTYPE html>
<html lang="en">

Expand All @@ -6,13 +13,21 @@
<title>Todo.Web</title>
<base href="/" />
<link href="css/app.css" rel="stylesheet" />

<!-- If you add any scoped CSS files, uncomment the following to load them
<link href="Todo.Web.Client.styles.css" rel="stylesheet" /> -->
</head>

<body>
<div id="app">Loading...</div>
<section class="todoapp">
<header class="header">
<h1>todos</h1>
</header>

<section class="main" style="display: block">
<component type="typeof(Todo.Web.Client.Todo)" render-mode="WebAssembly" />
</section>
</section>

<div id="blazor-error-ui">
An unhandled error has occurred.
Expand Down
12 changes: 12 additions & 0 deletions Todo.Web/Server/Pages/_Host.cshtml.cs
@@ -0,0 +1,12 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace Todo.Web.Server.Pages
{
public class IndexModel : PageModel
{
public void OnGet()
{
}
}
}
5 changes: 4 additions & 1 deletion Todo.Web/Server/Program.cs
Expand Up @@ -9,6 +9,9 @@
});
builder.Services.AddAuthorizationBuilder();

// Add razor pages so we can render the Blazor WASM todo component
builder.Services.AddRazorPages();

// Add the forwarder to make sending requests to the backend easier
builder.Services.AddHttpForwarder();

Expand Down Expand Up @@ -42,7 +45,7 @@
app.UseAuthentication();
app.UseAuthorization();

app.MapFallbackToFile("index.html");
app.MapFallbackToPage("/_Host");

// Configure the APIs
app.MapAuth();
Expand Down
2 changes: 1 addition & 1 deletion Todo.Web/Server/Todo.Web.Server.csproj
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
Expand Down
6 changes: 3 additions & 3 deletions TodoApi.sln
Expand Up @@ -13,11 +13,11 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
README.md = README.md
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Todo.Web.Server", "Todo.Web\Server\Todo.Web.Server.csproj", "{9459B25A-3A37-43C4-A7DE-6113C1E2CDD7}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Todo.Web.Server", "Todo.Web\Server\Todo.Web.Server.csproj", "{9459B25A-3A37-43C4-A7DE-6113C1E2CDD7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Todo.Web.Client", "Todo.Web\Client\Todo.Web.Client.csproj", "{699CCBA9-9DE8-48C3-9D2F-226E349C9253}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Todo.Web.Client", "Todo.Web\Client\Todo.Web.Client.csproj", "{699CCBA9-9DE8-48C3-9D2F-226E349C9253}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Todo.Web.Shared", "Todo.Web\Shared\Todo.Web.Shared.csproj", "{272942F6-94E8-4D6B-8AD8-C4CCA305836D}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Todo.Web.Shared", "Todo.Web\Shared\Todo.Web.Shared.csproj", "{272942F6-94E8-4D6B-8AD8-C4CCA305836D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down

0 comments on commit 90338b9

Please sign in to comment.