1
0
Fork 0
mirror of https://github.com/miawinter98/just-short-it.git synced 2024-11-25 09:19:55 +00:00

changed: migrated index page to razor component

This commit is contained in:
Mia Rose Winter 2023-11-18 14:35:05 +01:00
parent b00ed1669e
commit 70dad9ccdb
Signed by: miawinter
GPG key ID: 4B6F6A83178F595E
3 changed files with 37 additions and 48 deletions

37
Components/Index.razor Normal file
View file

@ -0,0 +1,37 @@
@page "/"
@page "/{Id}"
@using Microsoft.Extensions.Caching.Distributed
@inject IDistributedCache Db
@inject NavigationManager Navigation
<PageTitle>Startpage - Just Short It</PageTitle>
<div class="text-center flex flex-col items-center gap-4">
@if (Id is null) {
<h1 class="text-3xl lg:text-5xl font-bold text-primary-content">Welcome to Just Short It!</h1>
<h2 class="text-xl lg:text-3xl font-bold text-secondary-content">The KISS single-user URL shortener!</h2>
<a class="btn btn-primary btn-lg min-h-0 h-12" href="/Urls">Start shorting URLs</a>
} else {
<p class="text-error" aria-role="alert">@ErrorMessage</p>
}
</div>
@code {
[Parameter]
public string? Id { get; set; }
private string? ErrorMessage { get; set; }
protected override async Task OnInitializedAsync() {
if (Id is not null) {
string? data = await Db.GetStringAsync(Id);
if (data is not null) {
Navigation.NavigateTo(data, true);
} else {
ErrorMessage = "Redirect ID not found, it may have been deleted or expired";
}
}
}
}

View file

@ -1,15 +0,0 @@
@page "{Id?}"
@model IndexModel
@{
ViewData["Title"] = "Startpage";
}
<div class="text-center flex flex-col items-center gap-4">
@if (Model.Id is null) {
<h1 class="text-3xl lg:text-5xl font-bold text-primary-content">Welcome to Just Short It!</h1>
<h2 class="text-xl lg:text-3xl font-bold text-secondary-content">The KISS single-user URL shortener!</h2>
<a class="btn btn-primary btn-lg min-h-0 h-12" asp-page="Urls">Start shorting URLs</a>
} else {
<p class="text-error">@Model.ErrorMessage</p>
}
</div>

View file

@ -1,33 +0,0 @@
using System.Text;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Caching.Distributed;
namespace JustShortIt.Pages;
public class IndexModel : PageModel {
private ILogger<IndexModel> Logger { get; }
private IDistributedCache Db { get; set; }
// Bound property
public string? Id { get; set; }
public string? ErrorMessage { get; set; }
public IndexModel(ILogger<IndexModel> logger, IDistributedCache distributedCache) {
Logger = logger;
Db = distributedCache;
}
public async Task<IActionResult> OnGetAsync(string? id) {
Id = id;
if (Id is not null) {
string? data = await Db.GetStringAsync(Id);
if (data is not null) return Redirect(data);
ErrorMessage = "Redirect ID not found, it may have been deleted or expired";
}
return Page();
}
}