Skip to content

Commit

Permalink
Improved Csb Samples
Browse files Browse the repository at this point in the history
Added Ssb Sample
  • Loading branch information
EdCharbeneau committed Jan 10, 2020
1 parent dbb2467 commit 3f919ab
Show file tree
Hide file tree
Showing 34 changed files with 1,491 additions and 17 deletions.
7 changes: 7 additions & 0 deletions BlazorPro.Spinkit.sln
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CsbSample", "samples\CsbSam
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorPro.Spinkit.Tests", "tests\BlazorPro.Spinkit.Tests\BlazorPro.Spinkit.Tests.csproj", "{C90B6DB7-516A-4051-9029-E4CC4CC5575A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SsbSample", "samples\SsbSample\SsbSample.csproj", "{545B7476-5A55-40A7-9EC5-E93D9475655F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -31,13 +33,18 @@ Global
{C90B6DB7-516A-4051-9029-E4CC4CC5575A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C90B6DB7-516A-4051-9029-E4CC4CC5575A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C90B6DB7-516A-4051-9029-E4CC4CC5575A}.Release|Any CPU.Build.0 = Release|Any CPU
{545B7476-5A55-40A7-9EC5-E93D9475655F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{545B7476-5A55-40A7-9EC5-E93D9475655F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{545B7476-5A55-40A7-9EC5-E93D9475655F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{545B7476-5A55-40A7-9EC5-E93D9475655F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{0F77BA0C-F6DD-48B1-B6D5-2F118EB4063C} = {87121563-5E71-4700-840E-96D9EAD94C13}
{C90B6DB7-516A-4051-9029-E4CC4CC5575A} = {37CC6A35-3FC9-4940-8324-D6D302BECDF6}
{545B7476-5A55-40A7-9EC5-E93D9475655F} = {87121563-5E71-4700-840E-96D9EAD94C13}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {EA93A371-BDE7-45ED-82FD-F8B9A67EF268}
Expand Down
5 changes: 3 additions & 2 deletions samples/CsbSample/Pages/FetchData.razor
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
<h1>Weather forecast</h1>

<p>This component demonstrates fetching data from the server.</p>
<p>An artificial delay of 3000ms is used to show a &lt;SpinLoader&gt; component.</p>

<SpinLoader IsLoading="isLoading" IsFaulted="hasError" Size="46px" Color="#3498db" Center="true">
<ContentTemplate>
<table class="table">
Expand Down Expand Up @@ -37,7 +39,6 @@
</FaultedContentTemplate>
</SpinLoader>


@code {

private WeatherForecast[] forecasts;
Expand All @@ -48,7 +49,7 @@
protected override async Task OnInitializedAsync()
{
isLoading = true;
await Task.Delay(5000);
await Task.Delay(3000);
try
{
forecasts = await Http.GetJsonAsync<WeatherForecast[]>("sample-data/weather.json");
Expand Down
2 changes: 1 addition & 1 deletion samples/CsbSample/Pages/Index.razor
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@page "/"

<h1>Individual Spinners</h1>
<div class="row">
<div class="col">
<p>Chase</p>
Expand Down
37 changes: 27 additions & 10 deletions samples/CsbSample/Pages/LoadingTemplate.razor
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,25 @@
<h1>Weather forecast</h1>

<p>This component demonstrates fetching data from the server.</p>
<p>
The &lt;SpinLoader&gt; component supports three template regions:
<ul>
<li>LoadingTemplate (optional) - content to display while the IsLoading property is <b>true</b>.</li>
<li>ContentTemplate - content to display when the IsLoading is <b>false</b></li>
<li>FaultedTemplate - content to display when IsFaulted is <b>true</b> and IsLoading is <b>false</b></li>
</ul>
</p>
<div class="form-row align-items-center">
<div class="col-auto">
<div class="form-check mb-2">
<input id="forceException" type="checkbox" @bind="forceException" class="form-check-input" />
<label class="form-check-label" for="forceException">Force exception</label>
</div>
</div>
<div class="col-auto">
<button class="btn btn-primary mb-2" @onclick="LoadData">Retry</button>
</div>
</div>
<table class="table">
<thead>
<tr>
Expand Down Expand Up @@ -44,16 +63,13 @@
</tbody>
</table>

<button @onclick="LoadData">Retry</button>
<input type="checkbox" @bind="forceException" />

@code {

WeatherForecast[] forecasts;
bool isFaulted = false;
bool isLoading = true;
int delay = 2000;
bool forceException = false;
WeatherForecast[] forecasts;
bool isFaulted = false;
bool isLoading = true;
int delay = 2000;
bool forceException = false;

protected override async Task OnInitializedAsync()
{
Expand All @@ -70,15 +86,14 @@

void SuccessPath(WeatherForecast[] data)
{
isFaulted = false;
// do work
forecasts = data;
}

void FaultedPath(Exception e)
{
// log message, don't share it with the user
var fakeLog = e.Message;
isFaulted = true;
}

async Task TryLoadingData(Action<WeatherForecast[]> onSuccess, Action<Exception> onFaulted)
Expand All @@ -92,10 +107,12 @@
throw new NotSupportedException();
}
var data = await Http.GetJsonAsync<WeatherForecast[]>("sample-data/weather.json");
isFaulted = false;
onSuccess(data);
}
catch (Exception e)
{
isFaulted = true;
onFaulted(e);
}
finally
Expand Down
21 changes: 17 additions & 4 deletions samples/CsbSample/Pages/SizeColor.razor
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
@page "/size-color"

<Circle Color="@color" Size="@Size"></Circle>

<hr />
<input @bind="color" type="color" />
<input @bind="size" type="range" min="10" max="200"/>
<form>
<div class="form-row align-items-center">
<div class="col-sm-3 my-1">
<label>Color</label>
<input @bind="color" type="color" class="form-control" />
</div>
<div class="col-auto my-1">
<label>Size</label>
<input @bind="size" type="range" min="10" max="200" class="form-control" />
</div>
</div>
</form>

<div style="display: flex;align-items: center;justify-content: center;flex-direction: column;height:400px;">
<Circle Center="true" Color="@color" Size="@Size"></Circle>
</div>

@code {
string color = "#9b59b6";

Expand Down
10 changes: 10 additions & 0 deletions samples/SsbSample/App.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Router AppAssembly="@typeof(Program).Assembly">
<Found Context="routeData">
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
</Found>
<NotFound>
<LayoutView Layout="@typeof(MainLayout)">
<p>Sorry, there's nothing at this address.</p>
</LayoutView>
</NotFound>
</Router>
15 changes: 15 additions & 0 deletions samples/SsbSample/Data/WeatherForecast.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;

namespace SsbSample.Data
{
public class WeatherForecast
{
public DateTime Date { get; set; }

public int TemperatureC { get; set; }

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

public string Summary { get; set; }
}
}
26 changes: 26 additions & 0 deletions samples/SsbSample/Data/WeatherForecastService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Linq;
using System.Threading.Tasks;

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

public async Task<WeatherForecast[]> GetForecastAsync(DateTime startDate)
{
await Task.Delay(3000);
var rng = new Random();
return await Task.FromResult(Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = startDate.AddDays(index),
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)]
}).ToArray());
}
}
}
16 changes: 16 additions & 0 deletions samples/SsbSample/Pages/Error.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
@page "/error"


<h1 class="text-danger">Error.</h1>
<h2 class="text-danger">An error occurred while processing your request.</h2>

<h3>Development Mode</h3>
<p>
Swapping to <strong>Development</strong> environment will display more 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>
124 changes: 124 additions & 0 deletions samples/SsbSample/Pages/FetchData.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
@page "/fetchdata"
@using SsbSample.Data
@inject WeatherForecastService WeatherService

<h1>Weather forecast</h1>

<p>This component demonstrates fetching data from the server.</p>
<p>
The &lt;SpinLoader&gt; component supports three template regions:
<ul>
<li>LoadingTemplate (optional) - content to display while the IsLoading property is <b>true</b>.</li>
<li>ContentTemplate - content to display when the IsLoading is <b>false</b></li>
<li>FaultedTemplate - content to display when IsFaulted is <b>true</b> and IsLoading is <b>false</b></li>
</ul>
</p>
<div class="form-row align-items-center">
<div class="col-auto">
<div class="form-check mb-2">
<input id="forceException" type="checkbox" @bind="forceException" class="form-check-input" />
<label class="form-check-label" for="forceException">Force exception</label>
</div>
</div>
<div class="col-auto">
<button class="btn btn-primary mb-2" @onclick="LoadData">Retry</button>
</div>
</div>
<table class="table">
<thead>
<tr>
<th>Date</th>
<th>Temp. (C)</th>
<th>Temp. (F)</th>
<th>Summary</th>
</tr>
</thead>
<tbody>
<SpinLoader IsLoading="isLoading" IsFaulted="isFaulted">
<LoadingTemplate>
<tr>
<td colspan="4" style="vertical-align: middle;background-color: rgb(0, 0, 0, .2); height:300px;">
<Circle Color="#e67e22" Size="60px" Center="true" />
</td>
</tr>
</LoadingTemplate>
<ContentTemplate>
@foreach (var forecast in forecasts)
{
<tr>
<td>@forecast.Date.ToShortDateString()</td>
<td>@forecast.TemperatureC</td>
<td>@forecast.TemperatureF</td>
<td>@forecast.Summary</td>
</tr>
}
</ContentTemplate>
<FaultedContentTemplate>
<tr>
<td colspan="4">
<div class="alert alert-danger">Fail</div>
</td>
</tr>
</FaultedContentTemplate>
</SpinLoader>
</tbody>
</table>

@code {

WeatherForecast[] forecasts;
bool isFaulted = false;
bool isLoading = true;
int delay = 2000;
bool forceException = false;

protected override async Task OnInitializedAsync()
{
await LoadData();
}

async Task LoadData()
{
await TryLoadingData(
onSuccess: SuccessPath,
onFaulted: FaultedPath
);
}

void SuccessPath(WeatherForecast[] data)
{
// do work
forecasts = data;
}

void FaultedPath(Exception e)
{
// log message, don't share it with the user
var fakeLog = e.Message;
}

async Task TryLoadingData(Action<WeatherForecast[]> onSuccess, Action<Exception> onFaulted)
{
isLoading = true;
try
{
if (forceException)
{
throw new NotSupportedException();
}
var data = await WeatherService.GetForecastAsync(DateTime.Now);
isFaulted = false;
onSuccess(data);
}
catch (Exception e)
{
isFaulted = true;
onFaulted(e);
}
finally
{
isLoading = false;
}
}

}
Loading

0 comments on commit 3f919ab

Please sign in to comment.