Skip to content

Commit

Permalink
Add server sample app, fix watch position bug, and fix publish nuget …
Browse files Browse the repository at this point in the history
…bug.
  • Loading branch information
IEvangelist committed Jan 11, 2023
1 parent b22026a commit fd7bb93
Show file tree
Hide file tree
Showing 39 changed files with 1,466 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish-nuget.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
run: |
dotnet test ./tests/Blazor.ExampleConsumer.EndToEndTests/Blazor.ExampleConsumer.EndToEndTests.csproj --verbosity normal
publish:
publish:
needs: test
runs-on: ubuntu-latest
strategy:
Expand Down
9 changes: 8 additions & 1 deletion blazorators.sln
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Blazor.SpeechRecognition",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Blazor.Permissions.WebAssembly", "src\Blazor.Permissions.WebAssembly\Blazor.Permissions.WebAssembly.csproj", "{FD387378-9B27-4852-8A77-684C33FDE844}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Blazor.ExampleConsumer.EndToEndTests", "tests\Blazor.ExampleConsumer.EndToEndTests\Blazor.ExampleConsumer.EndToEndTests.csproj", "{4D04B168-6971-4660-BC22-FA7F18150018}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Blazor.ExampleConsumer.EndToEndTests", "tests\Blazor.ExampleConsumer.EndToEndTests\Blazor.ExampleConsumer.EndToEndTests.csproj", "{4D04B168-6971-4660-BC22-FA7F18150018}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlazorServer.ExampleConsumer", "samples\BlazorServer.ExampleConsumer\BlazorServer.ExampleConsumer.csproj", "{31D54C48-6325-4591-8612-87A917D49028}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -126,6 +128,10 @@ Global
{4D04B168-6971-4660-BC22-FA7F18150018}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4D04B168-6971-4660-BC22-FA7F18150018}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4D04B168-6971-4660-BC22-FA7F18150018}.Release|Any CPU.Build.0 = Release|Any CPU
{31D54C48-6325-4591-8612-87A917D49028}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{31D54C48-6325-4591-8612-87A917D49028}.Debug|Any CPU.Build.0 = Debug|Any CPU
{31D54C48-6325-4591-8612-87A917D49028}.Release|Any CPU.ActiveCfg = Release|Any CPU
{31D54C48-6325-4591-8612-87A917D49028}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -147,6 +153,7 @@ Global
{7FB98CAE-0CDB-4EF8-B9BB-6C51D62252CD} = {537EB83C-6982-40B0-801A-479DF3B17DBE}
{FD387378-9B27-4852-8A77-684C33FDE844} = {537EB83C-6982-40B0-801A-479DF3B17DBE}
{4D04B168-6971-4660-BC22-FA7F18150018} = {A644CEC3-BD94-4EB6-9BF0-86B562806BF9}
{31D54C48-6325-4591-8612-87A917D49028} = {91A35318-B03F-4D41-AE18-2C21B9D9C3F3}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3F86284A-32D2-4F79-B23C-7A0CB8775971}
Expand Down
38 changes: 38 additions & 0 deletions samples/Blazor.ExampleConsumer/Pages/Track.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
@page "/track"

@inject IGeolocationService Geolocation

<PageTitle>Geolocation</PageTitle>
<h1>
Geolocation
@if (_isLoading)
{
<span class="spinner-border text-primary" style="border-width: .12em;" role="status">
<span class="visually-hidden">Loading...</span>
</span>
}
@if (_position is not null)
{
<span class="pe-1">:
<code>
@(_position.TimestampAsUtcDateTime.ToLocalTime().ToString())
</code>
</span>
<a href="https://www.nuget.org/packages/Blazor.LocalStorage.WebAssembly" target="_blank"
rel="noopener noreferrer nofollow">
<span class="oi oi-external-link"></span>
</a>
}
</h1>

@if (_isLoading)
{
<p>This page demonstrates the source generated <code>Blazor.Geolocation.WebAssembly</code> package.</p>
}

@{
<Code Value=@_position T=GeolocationPosition IsError=false />
<Code Value=@_positionError T=GeolocationPositionError IsError=true />

<BingMap Position=@_position />
}
50 changes: 50 additions & 0 deletions samples/Blazor.ExampleConsumer/Pages/Track.razor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) David Pine. All rights reserved.
// Licensed under the MIT License.

namespace Blazor.ExampleConsumer.Pages;

public partial class Track : IDisposable
{
readonly JsonSerializerOptions _opts = new()
{
WriteIndented = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
};
readonly PositionOptions _options = new()
{
EnableHighAccuracy = true,
MaximumAge = null,
Timeout = 15_000
};

GeolocationPosition? _position;
GeolocationPositionError? _positionError;
double _watchId;
bool _isLoading = true;

protected override void OnInitialized() =>
_watchId = Geolocation.WatchPosition(
component: this,
onSuccessCallbackMethodName: nameof(OnPositionRecieved),
onErrorCallbackMethodName: nameof(OnPositionError),
options: _options);

[JSInvokable]
public void OnPositionRecieved(GeolocationPosition position)
{
_isLoading = false;
_position = position;
StateHasChanged();
}

[JSInvokable]
public void OnPositionError(GeolocationPositionError positionError)
{
_isLoading = false;
_positionError = positionError;
StateHasChanged();
}

public void Dispose() => Geolocation.ClearWatch(_watchId);
}
5 changes: 5 additions & 0 deletions samples/Blazor.ExampleConsumer/Shared/NavMenu.razor
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@
<span class="oi oi-microphone" aria-hidden="true"></span> Speech-to-text
</NavLink>
</div>
<div class="nav-item px-3">
<NavLink id="track" class="nav-link" href="track">
<span class="oi oi-compass" aria-hidden="true"></span> Track
</NavLink>
</div>
<div class="nav-item px-3">
<NavLink id="sandbox" class="nav-link" href="sandbox">
<span class="oi oi-inbox" aria-hidden="true"></span> Sandbox
Expand Down
12 changes: 12 additions & 0 deletions samples/BlazorServer.ExampleConsumer/App.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Router AppAssembly="@typeof(App).Assembly">
<Found Context="routeData">
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
<FocusOnNavigate RouteData="@routeData" Selector="h1" />
</Found>
<NotFound>
<PageTitle>Not found</PageTitle>
<LayoutView Layout="@typeof(MainLayout)">
<p role="alert">Sorry, there's nothing at this address.</p>
</LayoutView>
</NotFound>
</Router>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Blazor.Geolocation\Blazor.Geolocation.csproj" />
<ProjectReference Include="..\..\src\Blazor.LocalStorage\Blazor.LocalStorage.csproj" />
<ProjectReference Include="..\..\src\Blazor.Serialization\Blazor.Serialization.csproj" />
<ProjectReference Include="..\..\src\Blazor.SessionStorage\Blazor.SessionStorage.csproj" />
<ProjectReference Include="..\..\src\Blazor.SpeechRecognition\Blazor.SpeechRecognition.csproj" />
<ProjectReference Include="..\..\src\Blazor.SpeechSynthesis\Blazor.SpeechSynthesis.csproj" />
</ItemGroup>

</Project>
15 changes: 15 additions & 0 deletions samples/BlazorServer.ExampleConsumer/Data/WeatherForecast.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) David Pine. All rights reserved.
// Licensed under the MIT License.

namespace BlazorServer.ExampleConsumer.Data;

public class WeatherForecast
{
public DateOnly Date { get; set; }

public int TemperatureC { get; set; }

public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);

public string? Summary { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) David Pine. All rights reserved.
// Licensed under the MIT License.

namespace BlazorServer.ExampleConsumer.Data;

public class WeatherForecastService
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};

public Task<WeatherForecast[]> GetForecastAsync(DateOnly startDate)
{
return Task.FromResult(Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = startDate.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
}).ToArray());
}
}
18 changes: 18 additions & 0 deletions samples/BlazorServer.ExampleConsumer/Pages/Counter.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
@page "/counter"

<PageTitle>Counter</PageTitle>

<h1>Counter</h1>

<p role="status">Current count: @currentCount</p>

<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>

@code {
private int currentCount = 0;

private void IncrementCount()
{
currentCount++;
}
}
42 changes: 42 additions & 0 deletions samples/BlazorServer.ExampleConsumer/Pages/Error.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
@page
@model BlazorServer.ExampleConsumer.Pages.ErrorModel

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

<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<title>Error</title>
<link href="~/css/bootstrap/bootstrap.min.css" rel="stylesheet" />
<link href="~/css/site.css" rel="stylesheet" asp-append-version="true" />
</head>

<body>
<div class="main">
<div class="content px-4">
<h1 class="text-danger">Error.</h1>
<h2 class="text-danger">An error occurred while processing your request.</h2>

@if (Model.ShowRequestId)
{
<p>
<strong>Request ID:</strong> <code>@Model.RequestId</code>
</p>
}

<h3>Development Mode</h3>
<p>
Swapping to the <strong>Development</strong> environment displays detailed information about the error that occurred.
</p>
<p>
<strong>The Development environment shouldn't be enabled for deployed applications.</strong>
It can result in displaying sensitive information from exceptions to end users.
For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong>
and restarting the app.
</p>
</div>
</div>
</body>

</html>
28 changes: 28 additions & 0 deletions samples/BlazorServer.ExampleConsumer/Pages/Error.cshtml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) David Pine. All rights reserved.
// Licensed under the MIT License.

using System.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace BlazorServer.ExampleConsumer.Pages;
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
[IgnoreAntiforgeryToken]
public class ErrorModel : PageModel
{
public string? RequestId { get; set; }

public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);

private readonly ILogger<ErrorModel> _logger;

public ErrorModel(ILogger<ErrorModel> logger)
{
_logger = logger;
}

public void OnGet()
{
RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier;
}
}
47 changes: 47 additions & 0 deletions samples/BlazorServer.ExampleConsumer/Pages/FetchData.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
@page "/fetchdata"
@using BlazorServer.ExampleConsumer.Data
@inject WeatherForecastService ForecastService

<PageTitle>Weather forecast</PageTitle>

<h1>Weather forecast</h1>

<p>This component demonstrates fetching data from a service.</p>

@if (forecasts == null)
{
<p><em>Loading...</em></p>
}
else
{
<table class="table">
<thead>
<tr>
<th>Date</th>
<th>Temp. (C)</th>
<th>Temp. (F)</th>
<th>Summary</th>
</tr>
</thead>
<tbody>
@foreach (var forecast in forecasts)
{
<tr>
<td>@forecast.Date.ToShortDateString()</td>
<td>@forecast.TemperatureC</td>
<td>@forecast.TemperatureF</td>
<td>@forecast.Summary</td>
</tr>
}
</tbody>
</table>
}

@code {
private WeatherForecast[]? forecasts;

protected override async Task OnInitializedAsync()
{
forecasts = await ForecastService.GetForecastAsync(DateOnly.FromDateTime(DateTime.Now));
}
}
9 changes: 9 additions & 0 deletions samples/BlazorServer.ExampleConsumer/Pages/Index.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@page "/"

<PageTitle>Index</PageTitle>

<h1>Hello, world!</h1>

Welcome to your new app.

<SurveyPrompt Title="How is Blazor working for you?" />
34 changes: 34 additions & 0 deletions samples/BlazorServer.ExampleConsumer/Pages/_Host.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
@page "/"
@using Microsoft.AspNetCore.Components.Web
@namespace BlazorServer.ExampleConsumer.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<base href="~/" />
<link rel="stylesheet" href="css/bootstrap/bootstrap.min.css" />
<link href="css/site.css" rel="stylesheet" />
<link href="BlazorServer.ExampleConsumer.styles.css" rel="stylesheet" />
<link rel="icon" type="image/png" href="favicon.png"/>
<component type="typeof(HeadOutlet)" render-mode="ServerPrerendered" />
</head>
<body>
<component type="typeof(App)" render-mode="ServerPrerendered" />

<div id="blazor-error-ui">
<environment include="Staging,Production">
An error has occurred. This application may no longer respond until reloaded.
</environment>
<environment include="Development">
An unhandled exception has occurred. See browser dev tools for details.
</environment>
<a href="" class="reload">Reload</a>
<a class="dismiss">🗙</a>
</div>

<script src="_framework/blazor.server.js"></script>
</body>
</html>
Loading

0 comments on commit fd7bb93

Please sign in to comment.