[backend/razor] Add edit rule page

This commit is contained in:
pancakes 2025-01-07 12:03:01 +10:00 committed by Laura Hausmann
parent 513635b7dc
commit d165af041c
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
2 changed files with 70 additions and 0 deletions

View file

@ -4,6 +4,7 @@
@using Iceshrimp.Backend.Core.Services
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.EntityFrameworkCore
@using Microsoft.AspNetCore.Components.Routing
@inherits AdminComponentBase
<AdminPageHeader Title="Rules"/>
@ -13,6 +14,7 @@
<thead>
<th>#</th>
<th>Text & Description</th>
<th>Actions</th>
</thead>
@foreach (var rule in _rules)
{
@ -30,6 +32,9 @@
<i>No description</i>
}
</td>
<td>
<NavLink href="@($"/admin/rules/{rule.Id}")">Edit</NavLink>
</td>
</tr>
}
</table>

View file

@ -0,0 +1,65 @@
@page "/admin/rules/{Id}"
@using Iceshrimp.Backend.Components.Admin
@using Iceshrimp.Backend.Core.Database.Tables
@using Iceshrimp.Backend.Core.Middleware
@using Iceshrimp.Backend.Core.Services
@using Microsoft.EntityFrameworkCore
@using Microsoft.AspNetCore.Components.Forms
@inject NavigationManager Nav;
@inherits AdminComponentBase
<AdminPageHeader Title="Edit Rule"/>
<EditForm FormName="edit-rule" Model="Model" OnSubmit="OnSubmit">
<label for="order">Order</label>
<InputSelect @bind-Value="@Model.Order" id="order" disabled="@(_count == 1)">
@for (var i = 1; i <= _count; i++)
{
<option value="@i">@i</option>
}
</InputSelect>
<label for="text">Text</label>
<InputText @bind-Value="@Model.Text" id="text" class="width30" placeholder="Short rule" required/>
<label for="description">Description</label>
<InputText @bind-Value="@Model.Description" id="description" class="width30" placeholder="Longer rule description"/>
<button type="submit">Submit</button>
</EditForm>
@code {
[Inject] public required InstanceService Instance { get; set; }
[Parameter] public required string Id { get; set; }
[SupplyParameterFromForm] private RuleModel Model { get; set; } = null!;
private int _count;
private Rule? _rule;
private class RuleModel
{
public int Order { get; set; }
public string Text { get; set; } = "";
public string Description { get; set; } = "";
}
protected override void OnParametersSet()
{
Model ??= new RuleModel();
}
protected override async Task OnGet()
{
_rule = await Database.Rules.FirstOrDefaultAsync(p => p.Id == Id)
?? throw GracefulException.RecordNotFound();
_count = await Database.Rules.CountAsync();
Model.Order = _rule.Order;
Model.Text = _rule.Text;
Model.Description = _rule.Description ?? "";
}
private async Task OnSubmit()
{
_rule = await Database.Rules.FirstOrDefaultAsync(p => p.Id == Id)
?? throw GracefulException.RecordNotFound();
await Instance.UpdateRuleAsync(_rule, Model.Order, Model.Text, string.IsNullOrWhiteSpace(Model.Description) ? null : Model.Description);
Nav.NavigateTo("/admin/rules");
}
}