Skip to content

Commit

Permalink
add autocomplete to link form a mat project
Browse files Browse the repository at this point in the history
  • Loading branch information
elielijah321 committed Apr 3, 2024
1 parent 1f0c4f8 commit 505829c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,12 @@
Which MAT/SAT project do you want to link this conversion to?
</label>
</h1>
<p class="govuk-body">Enter the trust name, application reference or FAM reference of the existing form a MAT/SAT project</p>
<govuk-text-input name="application-reference" id="application-reference" asp-for="ApplicationReference" />
<partial name="_AutoComplete" model="@Model.AutoCompleteSearchModel" />
</div>

<button class="govuk-button pt-3" data-module="govuk-button" data-cy="select-common-submitbutton">
Continue
</button>
</form>

</div>
</div>
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using Dfe.PrepareConversions.Data;
using Dfe.PrepareConversions.Data.Models;
using Dfe.PrepareConversions.Data.Services;
using Dfe.PrepareConversions.Models;
using Dfe.PrepareConversions.Models.ProjectList;
using Dfe.PrepareConversions.Services;
using DocumentFormat.OpenXml.Office2010.Excel;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.IdentityModel.Tokens;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using EstablishmentDto = Dfe.Academies.Contracts.V4.Establishments.EstablishmentDto;
Expand All @@ -26,54 +26,87 @@ public LinkFormAMatProject(IAcademyConversionProjectRepository repository, IGetE
_getEstablishment = getEstablishment;
_errorService = errorService;
}

private const string SEARCH_LABEL = "Enter the trust name, application reference or FAM reference of the existing form a MAT/SAT project";
private const string SEARCH_ENDPOINT = "/start-new-project/link-project?handler=Search&searchQuery=";

[BindProperty]
public string SearchQuery { get; set; } = "";

[BindProperty]
public string IsFormAMat { get; set; }
[BindProperty]
public string IsProjectInPrepare { get; set; }
[BindProperty]
public string HasSchoolApplied { get; set; }
[BindProperty(Name = "application-reference")]
public string ApplicationReference { get; set; }


public string Urn { get; set; }

public async Task<IActionResult> OnGet(string urn, string isFormAMat, string hasSchoolApplied, string applicationReference, string isProjectInPrepare)
public AutoCompleteSearchModel AutoCompleteSearchModel { get; set; }

public async Task<IActionResult> OnGet(string urn, string isFormAMat, string hasSchoolApplied, string isProjectInPrepare)
{
ProjectListFilters.ClearFiltersFrom(TempData);
HasSchoolApplied = hasSchoolApplied;
IsFormAMat = isFormAMat;
IsProjectInPrepare = isProjectInPrepare;
ApplicationReference = applicationReference ?? null;

EstablishmentDto establishment = await _getEstablishment.GetEstablishmentByUrn(urn);
Urn = establishment.Urn;

AutoCompleteSearchModel = new AutoCompleteSearchModel(SEARCH_LABEL, SearchQuery, SEARCH_ENDPOINT);

return Page();
}

public async Task<IActionResult> OnPost(string ukprn, string urn, string redirect)
public async Task<IActionResult> OnGetSearch(string searchQuery)
{
if (ApplicationReference.IsNullOrEmpty() || ApplicationReference.Length <= 2)
{
_errorService.AddError("Application Reference", "Please enter a application reference with more than three characters");
return Page();
}
string[] searchSplit = SplitOnBrackets(searchQuery);

IEnumerable<FormAMatProject> projects = (await _repository.SearchFormAMatProjects(searchQuery)).Body;

return new JsonResult(projects.Select(s => new { suggestion = HighlightSearchMatch($"{s.ProposedTrustName} ({DeduceReferenceNumber(s)})", searchSplit[0].Trim(), s), value = $"{DeduceReferenceNumber(s)}" }));
}

var results = await _repository.SearchFormAMatProjects(ApplicationReference);
public async Task<IActionResult> OnPost(string ukprn, string urn, string redirect)
{
AutoCompleteSearchModel = new AutoCompleteSearchModel(SEARCH_LABEL, SearchQuery, SEARCH_ENDPOINT);

if (!results.Success || results.Body.Count() == 0)
if (SearchQuery.IsNullOrEmpty() || SearchQuery.Length <= 2)
{
_errorService.AddError("Application Reference", "Could not find a project with those details");
_errorService.AddError("Application Reference", "Please enter a application reference with more than three characters");
return Page();
}

var applicationReference = results.Body.First().ApplicationReference;
var applicationReference = SearchQuery;

var nextPage = Links.NewProject.Summary.Page;

redirect = string.IsNullOrEmpty(redirect) ? nextPage : redirect;

return RedirectToPage(redirect, new { ukprn, urn, HasSchoolApplied, IsFormAMat, IsProjectInPrepare, applicationReference });
}

private static string HighlightSearchMatch(string input, string toReplace, FormAMatProject project)
{
if (project == null || string.IsNullOrWhiteSpace(project.ReferenceNumber) || string.IsNullOrWhiteSpace(project.ProposedTrustName)) return string.Empty;

int index = input.IndexOf(toReplace, StringComparison.InvariantCultureIgnoreCase);
string correctCaseSearchString = input.Substring(index, toReplace.Length);

return input.Replace(toReplace, $"<strong>{correctCaseSearchString}</strong>", StringComparison.InvariantCultureIgnoreCase);
}

private static string[] SplitOnBrackets(string input)
{
// return array containing one empty string if input string is null or empty
if (string.IsNullOrWhiteSpace(input)) return new string[1] { string.Empty };

return input.Split(new[] { '(', ')' }, StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries);
}

private static string DeduceReferenceNumber(FormAMatProject project)
{
return !string.IsNullOrEmpty(project.ReferenceNumber) ? project.ReferenceNumber : project.ApplicationReference;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@
{ "urn", Model.Establishment.Urn },
{ "hasSchoolApplied", Model.HasSchoolApplied },
{ "ukprn", Model.Trust?.Ukprn },
{ "isFormAMat", Model.IsFormAMat}
};

var backLinkRouteParams = new Dictionary<string, string>
{
{ "urn", Model.Establishment.Urn },
{ "ukprn", Model.Trust?.Ukprn },
{ "hasSchoolApplied", Model.HasSchoolApplied },
{ "redirect", Links.NewProject.Summary.Page }
{ "redirect", Links.NewProject.Summary.Page },
{ "isFormAMat", Model.IsFormAMat}
};

var backLink = SummaryModel.DetermineBackLink(Model.HasSchoolApplied?.ToLower().Equals("yes") ?? false, Model.HasPreferredTrust?.ToLower().Equals("yes") ?? false, Model.ProposedTrustName is not null);
Expand Down

0 comments on commit 505829c

Please sign in to comment.