Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add possibility to edit redirects #142

Merged
merged 10 commits into from
Nov 26, 2024
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@model Geta.NotFoundHandler.Admin.Pages.Geta.NotFoundHandler.Admin.Components.Pager.PagerViewModel

<nav aria-label="Table pagination">
<ul class="pagination">
<ul class="pagination pagination-spacing">
<li class="page-item @(Model.HasPreviousPage ? string.Empty : "disabled")">
<a class="page-link" href="@Model.PageUrl(Model.PageNumber-1)" aria-label="Previous">
<span aria-hidden="true">&laquo;</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

@await Component.InvokeAsync("Card", new { message = Model.Message })

<!-- Search form -->
<form method="get">
<div class="search-container input-group flex-nowrap">
<input class="form-control w-100 border-end-0" type="text" placeholder="Search" aria-label="Search" id="search" name="q" value="@Model.Query">
Expand All @@ -22,20 +23,21 @@
</div>
</form>

<form method="post">
<div class="table-responsive mt-3">
<table class="table table-hover table-sm" aria-label="Redirects">
<thead>
<tr>
<th>Old URL</th>
<th>New URL</th>
<th class="col-1 text-center">Wildcard</th>
<th class="col-1">Redirect Type</th>
<th class="col-1"></th>
</tr>
</thead>
<tbody>
<tr>
<!-- Redirect table -->
<div class="table-responsive mt-3">
<table class="table table-hover table-sm" aria-label="Redirects">
<thead>
<tr>
<th>Old URL</th>
<th>New URL</th>
<th class="col-1 text-center">Wildcard</th>
<th class="col-1">Redirect Type</th>
<th class="col-1"></th>
</tr>
</thead>
<tbody>
<tr>
<form method="post">
<td>
<input type="text" class="form-control" asp-for="CustomRedirect.OldUrl">
<span asp-validation-for="CustomRedirect.OldUrl" class="text-danger"></span>
Expand All @@ -50,45 +52,161 @@
<td>
<select class="form-select" asp-for="CustomRedirect.RedirectType">
<option value="@RedirectType.Temporary"
selected="@(Options.Value.DefaultRedirectType == RedirectType.Temporary)">
@RedirectType.Temporary
</option>
selected="@(Options.Value.DefaultRedirectType == RedirectType.Temporary)">@RedirectType.Temporary</option>
<option value="@RedirectType.Permanent"
selected="@(Options.Value.DefaultRedirectType == RedirectType.Permanent)">
@RedirectType.Permanent
</option>
@RedirectType.Permanent</option>
</select>
</td>
<td>
<div class="d-grid gap-2">
<button type="submit" class="btn btn-primary"
asp-page-handler="create">
<button type="submit" class="btn btn-primary" asp-page-handler="create">
<span data-feather="plus"></span> add
</button>
</div>
</td>
</tr>
@foreach (var item in Model.Items)
{
<tr class="align-middle">
<td>@item.OldUrl</td>
<td>@item.NewUrl</td>
<td class="text-center align-middle">
@await Component.InvokeAsync("CheckboxReadonly", new { isChecked = item.WildCardSkipAppend })
</td>
<td>@item.RedirectType</td>
<td>
<div class="d-grid gap-2">
<button type="submit" class="btn btn-danger"
asp-page-handler="delete" asp-route-oldurl="@item.OldUrl">
</form>
</tr>

@for (int i = 0; i < Model.Items.Count; i++)
{
var item = Model.Items[i];
<tr class="align-middle">

<!-- Display Mode -->
<td>@item.OldUrl</td>
<td>@item.NewUrl</td>
<td class="text-center align-middle">
@await Component.InvokeAsync("CheckboxReadonly", new { isChecked = item.WildCardSkipAppend })
</td>
<td>@item.RedirectType</td>
<td>
@{
var editModalId = $"editModal{i}";
var modalTitleId = $"modalTitle{i}";
}

<div class="modal fade" id="@editModalId" tabindex="-1" aria-labelledby="@modalTitleId">
<div class="modal-dialog">
<div class="modal-content edit-modal">
<div class="modal-header">
<h5 class="modal-title" id="@modalTitleId">Edit</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"
aria-label="Close"></button>
</div>
<div class="modal-body">
<form method="post" id="form-@editModalId">

<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text" id="basic-addon1">OldUrl</span>
</div>
<textarea class="form-control" id="EditRedirect.OldUrl"
name="EditRedirect.OldUrl" rows="1">@item.OldUrl</textarea>
<span asp-validation-for="EditRedirect.OldUrl" class="text-danger"></span>
</div>

<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text" id="basic-addon1">NewUrl</span>
</div>
<textarea class="form-control" id="EditRedirect.NewUrl"
name="EditRedirect.NewUrl" rows="1">@item.NewUrl</textarea>
<span asp-validation-for="EditRedirect.NewUrl" class="text-danger"></span>
</div>

<div class="d-flex flex-row">
<div class="flex-fill">
<label>Wildcard</label>
<div class="input-group mb-3 text-center align-middle">
@if (item.WildCardSkipAppend)
{
<input type="checkbox" class="form-check-input"
asp-for="EditRedirect.WildCardSkipAppend" value="true"
checked="checked"/>
}
else
{
<input type="checkbox" class="form-check-input"
asp-for="EditRedirect.WildCardSkipAppend" value="true"/>
}
</div>
</div>

<div class="flex-fill">
<label>RedirectType</label>
<div class="input-group mb-3">
<select class="form-select" asp-for="EditRedirect.RedirectType">
@if (item.RedirectType == RedirectType.Temporary)
{
<option value="@RedirectType.Temporary"
selected>@RedirectType.Temporary</option>
}
else
{
<option
value="@RedirectType.Temporary">@RedirectType.Temporary</option>
}

@if (item.RedirectType == RedirectType.Permanent)
{
<option value="@RedirectType.Permanent"
selected>@RedirectType.Permanent</option>
}
else
{
<option
value="@RedirectType.Permanent">@RedirectType.Permanent</option>
}
</select>
</div>
</div>
</div>


<input type="hidden" asp-for="EditRedirect.Id" value="@item.Id"/>
<input type="hidden" id="PageNumber" name="PageNumber" value="@Model.Paging.PageNumber"/>
<input type="hidden" id="Query" name="Query" value="@Model.Query"/>

<button type="submit"
class="btn btn-primary"
asp-page-handler="update">Update
</button>
</form>
</div>
</div>
</div>
</div>

<div class="d-grid gap-2">
<button
type="submit"
class="btn btn-primary modal-trigger"
data-bs-toggle="modal"
data-bs-target="#@editModalId">
<span data-feather="edit"></span> edit
</button>
</div>
</td>
<td>
<div class="d-grid gap-2">
<form method="post">
<button
type="submit"
class="btn btn-danger"
asp-page-handler="delete"
asp-route-id="@item.Id"
asp-route-pagenumber="@Model.Items.PageNumber"
asp-route-query="@Model.Query">
<span data-feather="trash-2"></span> delete
</button>
</div>
</td>
</tr>
}
</tbody>
</table>
@await Component.InvokeAsync(typeof(Geta.NotFoundHandler.Admin.Pages.Geta.NotFoundHandler.Admin.Components.Pager.PagerViewComponent), new { Model.Items })
</div>
</form>
</form>
</div>
</td>
</tr>
}
</tbody>
</table>

@await Component.InvokeAsync(typeof(Geta.NotFoundHandler.Admin.Pages.Geta.NotFoundHandler.Admin.Components.Pager.PagerViewComponent), new { Model.Items })
</div>
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System.Collections.Generic;
using System.Linq;
using Geta.NotFoundHandler.Admin.Areas.GetaNotFoundHandlerAdmin.Pages;
using Geta.NotFoundHandler.Admin.Pages.Geta.NotFoundHandler.Admin.Models;
using Geta.NotFoundHandler.Core.Redirects;
using Geta.NotFoundHandler.Infrastructure;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.RazorPages;
using X.PagedList;

Expand Down Expand Up @@ -35,33 +37,72 @@ public IndexModel(IRedirectsService redirectsService)

public bool HasQuery => !string.IsNullOrEmpty(Query);

public void OnGet()
[BindProperty]
public RedirectModel EditRedirect { get; set; }

public void OnGet(RedirectsRequest request)
{
ApplyRequest(request);

Load();
}

public IActionResult OnPostCreate()
{
if (!ModelState.IsValid)
if (ModelState.GetValidationState($"{nameof(CustomRedirect)}.{nameof(CustomRedirect.OldUrl)}") ==
ModelValidationState.Valid &&
ModelState.GetValidationState($"{nameof(CustomRedirect)}.{nameof(CustomRedirect.NewUrl)}") ==
ModelValidationState.Valid)
{
Load();
return Page();
var customRedirect = new CustomRedirect(CustomRedirect.OldUrl,
CustomRedirect.NewUrl,
CustomRedirect.WildCardSkipAppend,
CustomRedirect.RedirectType);

_redirectsService.AddOrUpdate(customRedirect);

return RedirectToPage();
}

var customRedirect = new CustomRedirect(CustomRedirect.OldUrl,
CustomRedirect.NewUrl,
CustomRedirect.WildCardSkipAppend,
CustomRedirect.RedirectType);
Load();
return Page();
}

public IActionResult OnPostDelete(RedirectsRequest request)
{
ModelState.Clear();

_redirectsService.DeleteById(request.Id);

ApplyRequest(request);

_redirectsService.AddOrUpdate(customRedirect);
Load();

return RedirectToPage();
return RedirectToPage(request);
}

public IActionResult OnPostDelete(string oldUrl)
public IActionResult OnPostUpdate(RedirectsRequest request)
{
_redirectsService.DeleteByOldUrl(oldUrl);
return RedirectToPage();
if (ModelState.GetValidationState($"{nameof(EditRedirect)}.{nameof(EditRedirect.OldUrl)}") ==
jevgenijsp marked this conversation as resolved.
Show resolved Hide resolved
ModelValidationState.Valid &&
ModelState.GetValidationState($"{nameof(EditRedirect)}.{nameof(EditRedirect.NewUrl)}") ==
ModelValidationState.Valid &&
EditRedirect.Id != null)
{
_redirectsService.AddOrUpdate(new CustomRedirect
{
Id = EditRedirect.Id,
OldUrl = EditRedirect.OldUrl,
RedirectType = EditRedirect.RedirectType,
NewUrl = EditRedirect.NewUrl,
WildCardSkipAppend = EditRedirect.WildCardSkipAppend
});

return RedirectToPage(request);
}

Load();
return Page();
}

private void Load()
Expand All @@ -75,4 +116,14 @@ private IEnumerable<CustomRedirect> FindRedirects()
{
return HasQuery ? _redirectsService.Search(Query) : _redirectsService.GetSaved();
}

private void ApplyRequest(RedirectsRequest request)
{
if (request.PageNumber.HasValue)
{
Paging.PageNumber = request.PageNumber.Value;
}

Query = request.Query ?? Query;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;

namespace Geta.NotFoundHandler.Admin.Areas.GetaNotFoundHandlerAdmin.Pages;

public class RedirectsRequest
{
public string Query { get; set; }
public int? PageNumber { get; set; }
public Guid Id { get; set; }
}
Loading
Loading