95 lines
2.9 KiB
Plaintext
95 lines
2.9 KiB
Plaintext
@using Moonlight.Frontend.Helpers
|
|
@using Moonlight.Frontend.Mappers
|
|
@using Moonlight.Frontend.UI.Admin.Components
|
|
@using Moonlight.Shared.Http.Requests.Admin.Roles
|
|
@using Moonlight.Shared.Http.Responses.Admin
|
|
@using ShadcnBlazor.Dialogs
|
|
@using ShadcnBlazor.Extras.Forms
|
|
@using ShadcnBlazor.Extras.Toasts
|
|
@using ShadcnBlazor.Fields
|
|
@using ShadcnBlazor.Inputs
|
|
|
|
@inherits ShadcnBlazor.Extras.Dialogs.DialogBase
|
|
|
|
@inject HttpClient HttpClient
|
|
@inject ToastService ToastService
|
|
|
|
<DialogHeader>
|
|
<DialogTitle>
|
|
Update @Role.Name
|
|
</DialogTitle>
|
|
<DialogDescription>
|
|
Update name, description and the permissions the role should grant to its members
|
|
</DialogDescription>
|
|
</DialogHeader>
|
|
|
|
<EnhancedEditForm Model="Request" OnValidSubmit="OnSubmitAsync">
|
|
<FieldGroup>
|
|
<FormValidationSummary/>
|
|
<DataAnnotationsValidator/>
|
|
|
|
<FieldSet>
|
|
<Field>
|
|
<FieldLabel for="roleName">Name</FieldLabel>
|
|
<TextInputField
|
|
@bind-Value="Request.Name"
|
|
id="roleName"
|
|
placeholder="My fancy role"/>
|
|
</Field>
|
|
<Field>
|
|
<FieldLabel for="keyDescription">Description</FieldLabel>
|
|
<TextareaInputField @bind-Value="Request.Description" id="keyDescription"
|
|
placeholder="Describe what the role should be used for"/>
|
|
</Field>
|
|
<Field>
|
|
<FieldLabel>Permissions</FieldLabel>
|
|
<FieldContent>
|
|
<PermissionSelector Permissions="Permissions"/>
|
|
</FieldContent>
|
|
</Field>
|
|
</FieldSet>
|
|
<Field Orientation="FieldOrientation.Horizontal" ClassName="justify-end">
|
|
<SubmitButton>Save changes</SubmitButton>
|
|
</Field>
|
|
</FieldGroup>
|
|
</EnhancedEditForm>
|
|
|
|
@code
|
|
{
|
|
[Parameter] public Func<Task> OnSubmit { get; set; }
|
|
[Parameter] public RoleDto Role { get; set; }
|
|
|
|
private UpdateRoleDto Request;
|
|
private List<string> Permissions;
|
|
|
|
protected override void OnInitialized()
|
|
{
|
|
Request = RoleMapper.ToUpdate(Role);
|
|
Permissions = Role.Permissions.ToList();
|
|
}
|
|
|
|
private async Task<bool> OnSubmitAsync(EditContext editContext, ValidationMessageStore validationMessageStore)
|
|
{
|
|
Request.Permissions = Permissions.ToArray();
|
|
|
|
var response = await HttpClient.PatchAsJsonAsync(
|
|
$"api/admin/roles/{Role.Id}",
|
|
Request,
|
|
Constants.SerializerOptions
|
|
);
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
{
|
|
await ProblemDetailsHelper.HandleProblemDetailsAsync(response, Request, validationMessageStore);
|
|
return false;
|
|
}
|
|
|
|
await ToastService.SuccessAsync("Role update", $"Role {Request.Name} has been successfully updated");
|
|
|
|
await OnSubmit.Invoke();
|
|
await CloseAsync();
|
|
|
|
return true;
|
|
}
|
|
}
|