Skip to content

Commit

Permalink
Remove autocomplete from Merge User UI (#781)
Browse files Browse the repository at this point in the history
  • Loading branch information
gunndabad authored Jan 16, 2024
1 parent 5d661a8 commit 2af9b59
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,54 +5,18 @@
ViewBag.Title = Html.DisplayNameFor(m => m.UserIdToMerge);
}

@section Styles
{
<link rel="stylesheet" href="~/Styles/Components/accessible-autocomplete.min.css" asp-append-version="true" />
}

@section Scripts
{
<script src="~/Scripts/Components/accessible-autocomplete.min.js"></script>
<script asp-add-nonce="true">
window.onload = function () {
const inputName = '@nameof(Model.UserIdToMerge)';
const select = document.querySelector(`select[name=${inputName}`);
const value = select.value;
const container = select.parentElement;
accessibleAutocomplete({
element: container,
id: inputName,
source: @Html.Raw(Json.Serialize(Model.UserIds)),
defaultValue: value
})
container.removeChild(select);
const input = container.querySelector('input');
input.name = inputName;
}
</script>
}

@section BeforeContent {
<govuk-back-link asp-page="/Admin/User" asp-route-userId="@Model.UserId" />
}

<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds-from-desktop">
<form asp-page="MergeUser" asp-route-userId="@Model.UserId">
<govuk-select asp-for="UserIdToMerge">
<govuk-select-label is-page-heading="true" class="govuk-label--l">
<govuk-input asp-for="UserIdToMerge" input-class="govuk-input--width-20">
<govuk-input-label is-page-heading="true" class="govuk-label--l">
<h1 class="govuk-heading-l">@ViewBag.Title</h1>

<p>Which user do you want to merge with <b>@Model.UserId</b>.</p>
</govuk-select-label>
<govuk-select-item value=""></govuk-select-item>
@foreach (var userId in Model.UserIds ?? Enumerable.Empty<Guid>())
{
<govuk-select-item value="@userId">@userId</govuk-select-item>
}
</govuk-select>
</govuk-input-label>
</govuk-input>

<govuk-button type="submit">Continue</govuk-button>
</form>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ public Merge(TeacherIdentityServerDbContext dbContext)
_dbContext = dbContext;
}

public ICollection<Guid>? UserIds { get; set; }

[BindProperty]
[Display(Name = "Select which user to merge")]
[Required(ErrorMessage = "Select which user you want to merge")]
[Display(Name = "Enter the user ID to merge")]
[Required(ErrorMessage = "Enter the user ID to merge")]
public Guid? UserIdToMerge { get; set; }

[FromRoute]
Expand All @@ -34,14 +32,17 @@ public void OnGet()

public IActionResult OnPost()
{
if (!ModelState.IsValid)
if (!UserIdToMerge.HasValue)
{
return this.PageWithErrors();
ModelState.AddModelError(nameof(UserIdToMerge), "Enter the user ID of the user to merge.");
}
else if (UserIdToMerge.Value == UserId)
{
ModelState.AddModelError(nameof(UserIdToMerge), "User cannot be merged with itself.");
}

if (UserIds == null || !UserIds.Contains((Guid)UserIdToMerge!))
if (!ModelState.IsValid)
{
ModelState.AddModelError(nameof(UserIdToMerge), "You must select a user ID from the given list");
return this.PageWithErrors();
}

Expand All @@ -58,11 +59,6 @@ public override async Task OnPageHandlerExecutionAsync(PageHandlerExecutingConte
return;
}

UserIds = _dbContext.Users
.Where(u => u.UserId != UserId && u.UserType == UserType.Default)
.Select(u => u.UserId)
.ToArray();

await next();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,29 +97,29 @@ public async Task Post_EmptyUserIdToMerge_ReturnsError()
var response = await HttpClient.SendAsync(request);

// Assert
await AssertEx.HtmlResponseHasError(response, "UserIdToMerge", "Select which user you want to merge");
await AssertEx.HtmlResponseHasError(response, "UserIdToMerge", "Enter the user ID to merge");
}

[Fact]
public async Task Post_InvalidUserIdToMerge_ReturnsError()
{
// Arrange
var user = await TestData.CreateUser(userType: UserType.Default, hasTrn: false);
var invalidUserToMerge = await TestData.CreateUser(userType: UserType.Staff, hasTrn: false);
var invalidUserToMergeId = user.UserId;

var request = new HttpRequestMessage(HttpMethod.Post, $"/admin/users/{user.UserId}/merge")
{
Content = new FormUrlEncodedContentBuilder()
{
{ "UserIdToMerge", invalidUserToMerge.UserId }
{ "UserIdToMerge", invalidUserToMergeId }
}
};

// Act
var response = await HttpClient.SendAsync(request);

// Assert
await AssertEx.HtmlResponseHasError(response, "UserIdToMerge", "You must select a user ID from the given list");
await AssertEx.HtmlResponseHasError(response, "UserIdToMerge", "User cannot be merged with itself.");
}

[Fact]
Expand Down

0 comments on commit 2af9b59

Please sign in to comment.