Skip to content

Commit

Permalink
Added more examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
Pacman1988 committed Apr 9, 2023
1 parent 23c1fc4 commit d0870b3
Show file tree
Hide file tree
Showing 11 changed files with 198 additions and 4 deletions.
2 changes: 1 addition & 1 deletion BlazorExamples.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.33516.290
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlazorExamples", "BlazorExamples\BlazorExamples.csproj", "{0747DF0F-7DC1-4BB0-9AA9-71895FC4B148}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorExamples", "BlazorExamples\BlazorExamples.csproj", "{0747DF0F-7DC1-4BB0-9AA9-71895FC4B148}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
9 changes: 7 additions & 2 deletions BlazorExamples/App.razor
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
<CascadingAuthenticationState>
<Router AppAssembly="@typeof(App).Assembly">
<Found Context="routeData">
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
<FocusOnNavigate RouteData="@routeData" Selector="h1" />
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
<NotAuthorized>
<LoginDisplay />
<p>No view without login!</p>
<img alt="Gandalf saying you shall not pass" width="100%" src="img/gandalf.gif" />
</NotAuthorized>
</AuthorizeRouteView>
</Found>
<NotFound>
<PageTitle>Not found</PageTitle>
Expand Down
4 changes: 4 additions & 0 deletions BlazorExamples/BlazorExamples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.14" />
</ItemGroup>

<ItemGroup>
<Folder Include="wwwroot\img\" />
</ItemGroup>

</Project>
12 changes: 12 additions & 0 deletions BlazorExamples/Models/PersonModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace BlazorExamples.Models;

public class PersonModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
10 changes: 10 additions & 0 deletions BlazorExamples/Pages/Authorization/AuthRequired.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@page "/auth-required"
@attribute [Authorize]

<h3>Auth Required</h3>

<p class="alert alert-success" role="alert">Hooray, when you can see this your successfully authorized!</p>

@code {

}
18 changes: 18 additions & 0 deletions BlazorExamples/Pages/Authorization/PartitialAuth.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
@page "/partitial-auth"

<h3>Partitial Auth</h3>
<p>With Login you see the Power Counter!</p>
<AuthorizeView>
<Authorized>
<h4>Power Counter</h4>
<Counter IncrementBy="500" startingValue="1000"/>
</Authorized>
<NotAuthorized>
<h4>Normal Counter</h4>
<Counter />
</NotAuthorized>
</AuthorizeView>

@code {

}
15 changes: 14 additions & 1 deletion BlazorExamples/Pages/Counter.razor
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@page "/counter"
@page "/counter/{startingValue:int}"

<PageTitle>Counter</PageTitle>

Expand All @@ -11,8 +12,20 @@
@code {
private int currentCount = 0;

[Parameter]
public int startingValue { get; set; }

[Parameter]
public int IncrementBy { get; set; } = 1;

protected override void OnParametersSet()
{
currentCount = startingValue;
base.OnParametersSet();
}

private void IncrementCount()
{
currentCount++;
currentCount += IncrementBy;
}
}
30 changes: 30 additions & 0 deletions BlazorExamples/Pages/Forms/BasicForms.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
@page "/basic-forms"
@using BlazorExamples.Models;

<h3>User Form</h3>
<p>
@formResults
</p>
<p>
First Name: @person.FirstName
</p>
<p>
Last Name: @person.LastName
</p>

<EditForm Model="@person" OnValidSubmit="HandleValidSubmit">
<InputText id="firstName" @bind-Value="person.FirstName" />
<InputText id="lastName" @bind-Value="person.LastName" />
<button class="btn btn-primary" type="submit">Submit</button>
</EditForm>


@code {
private PersonModel person = new PersonModel();
private string formResults = "";

private void HandleValidSubmit()
{
formResults = $"{person.FirstName} {person.LastName} was just created.";
}
}
82 changes: 82 additions & 0 deletions BlazorExamples/Pages/Upload.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
@page "/upload"
@inject ILogger<Index> Logger

<h3>Upload Files</h3>

<p>
<label>
Max file size in MB:
<input type="number" @bind="maxFileSize" />
</label>
</p>

<p>
<label>
Max allowed files:
<input type="number" @bind="maxAllowedFiles" />
</label>
</p>

<p>
<label>
Upload up to @maxAllowedFiles of up to @maxFileSize MB:
<InputFile OnChange="@LoadFiles" multiple />
</label>
</p>

@if (isLoading)
{
<p>Uploading...</p>
}
else
{
@foreach (var file in loadedFiles)
{
<ul>
<li>Name: @file.Name</li>
<li>Last modified: @file.LastModified.ToString()</li>
<li>Size (bytes): @file.Size</li>
<li>Content type: @file.ContentType</li>
</ul>
<br />
}
}

@code {
private List<IBrowserFile> loadedFiles = new();
private long maxFileSize = 1;
private int maxAllowedFiles = 3;
private bool isLoading;

private async Task LoadFiles(InputFileChangeEventArgs e)
{
isLoading = true;
loadedFiles.Clear();

foreach (var file in e.GetMultipleFiles(maxAllowedFiles))
{
if (file.Size > (maxFileSize * 1024 * 1024))
{
continue;
}

try
{
loadedFiles.Add(file);

var path = Path.Combine(@"C:\Temp",
file.Name);

await using FileStream fs = new(path, FileMode.Create);
await file.OpenReadStream(maxFileSize * 1024 * 1024).CopyToAsync(fs);
}
catch (Exception ex)
{
Logger.LogError("File: {Filename} Error: {Error}",
file.Name, ex.Message);
}
}

isLoading = false;
}
}
20 changes: 20 additions & 0 deletions BlazorExamples/Shared/NavMenu.razor
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,26 @@
<span class="oi oi-collapse-down" aria-hidden="true"></span> Fetch data with API
</NavLink>
</div>
<div class="nav-item px-3">
<NavLink class="nav-link" href="auth-required">
<span class="oi oi-account-login" aria-hidden="true"></span> Authorization needed
</NavLink>
</div>
<div class="nav-item px-3">
<NavLink class="nav-link" href="partitial-auth">
<span class="oi oi-account-login" aria-hidden="true"></span> Partitial Authorization
</NavLink>
</div>
<div class="nav-item px-3">
<NavLink class="nav-link" href="upload">
<span class="oi oi-file" aria-hidden="true"></span> File Upload
</NavLink>
</div>
<div class="nav-item px-3">
<NavLink class="nav-link" href="basic-forms">
<span class="oi oi-caret-left" aria-hidden="true"></span> Basic Forms
</NavLink>
</div>
</nav>
</div>

Expand Down
Binary file added BlazorExamples/wwwroot/img/gandalf.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d0870b3

Please sign in to comment.