-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Provider page with auto complete WIP WIP Changes since support user authentication Move capybara settings Changes to API service to get individual providers based on code Add Publish URL to env.test Conflicting linters Add translated error message for provider code taken Resolving Prettier lint issues Remove message from provider validation Resolve prettier issue Resolve prettier issue Update yarn lock Lint since rebase Yarn update
- Loading branch information
Jamie
committed
Jan 2, 2024
1 parent
26c3245
commit 5df9cdc
Showing
28 changed files
with
674 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
PLACEMENTS_HOST=placements.localhost | ||
CLAIMS_HOST=claims.localhost | ||
PUBLISH_BASE_URL=https://www.publish-teacher-training-courses.service.gov.uk |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
app/controllers/placements/support/provider_suggestions_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
class Placements::Support::ProviderSuggestionsController < Placements::Support::ApplicationController | ||
def index | ||
accredited_providers = AccreditedProviderApi.call | ||
filtered_providers = filter_providers(accredited_providers, query_params) | ||
providers = | ||
filtered_providers.map { |provider| formatted_provider(provider) } | ||
render json: providers | ||
end | ||
|
||
private | ||
|
||
def query_params | ||
params.require(:query) | ||
end | ||
|
||
def filter_providers(providers, query) | ||
return providers if query.blank? | ||
|
||
downcase_query = query.downcase | ||
providers.select do |provider| | ||
[ | ||
provider.dig("attributes", "name"), | ||
provider.dig("attributes", "postcode"), | ||
provider.dig("attributes", "urn"), | ||
provider.dig("attributes", "ukprn"), | ||
].any? { |attribute| attribute&.downcase&.include?(downcase_query) } | ||
end | ||
end | ||
|
||
def formatted_provider(provider) | ||
{ | ||
id: provider.fetch("id"), | ||
name: provider.dig("attributes", "name"), | ||
code: provider.dig("attributes", "code"), | ||
} | ||
end | ||
end |
28 changes: 28 additions & 0 deletions
28
app/controllers/placements/support/providers_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
class Placements::Support::ProvidersController < Placements::Support::ApplicationController | ||
def new | ||
@provider = Provider.new | ||
end | ||
|
||
def create | ||
@provider = Provider.new(provider_params) | ||
if @provider.save | ||
redirect_to placements_support_organisations_path | ||
else | ||
render :new | ||
end | ||
end | ||
|
||
def check | ||
@provider = Provider.new(provider_code: params[:accredited_provider_id]) | ||
@provider_details = AccreditedProviderApi.call(@provider.provider_code) | ||
return if @provider.valid? && @provider_details.present? | ||
|
||
render :new | ||
end | ||
|
||
private | ||
|
||
def provider_params | ||
params.require(:provider).permit(:provider_code) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import initAutocomplete from "./autocomplete"; | ||
|
||
const providerTemplate = (result) => result && result.name; | ||
const providerSuggestionTemplate = (result) => | ||
result && `${result.name} (${result.code})`; | ||
const onConfirm = (input) => (option) => | ||
(input.value = option ? option.code : ""); | ||
|
||
function init() { | ||
const options = { | ||
path: `/support/provider_suggestions`, | ||
template: { | ||
inputValue: providerTemplate, | ||
suggestion: providerSuggestionTemplate, | ||
}, | ||
minLength: 2, | ||
inputName: "accredited_provider_id", | ||
onConfirm, | ||
}; | ||
|
||
initAutocomplete( | ||
"accredited-provider-autocomplete", | ||
"accredited-provider-search-form-query-field", | ||
options, | ||
); | ||
} | ||
|
||
export default init; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
import { initAll } from "govuk-frontend"; | ||
import autocompleteSetup from "./autocomplete"; | ||
import accreditedProviderAutocompleteSetup from "./accredited_provider"; | ||
|
||
initAll(); | ||
autocompleteSetup(); | ||
accreditedProviderAutocompleteSetup(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import accessibleAutocomplete from "accessible-autocomplete"; | ||
import debounce from "lodash.debounce"; | ||
import { request } from "./utils/request_helper"; | ||
|
||
const initAutocomplete = (elementId, inputId, options = {}) => { | ||
try { | ||
const element = document.getElementById(elementId); | ||
const input = document.getElementById(inputId); | ||
|
||
if (element && input) { | ||
const { path, template, minLength, onConfirm, inputName } = options; | ||
const { inputValue, suggestion } = template; | ||
|
||
accessibleAutocomplete({ | ||
element, | ||
id: input.id, | ||
showNoOptionsFound: true, | ||
name: input.name, | ||
defaultValue: input.value, | ||
minLength, | ||
source: debounce(request(path), 900), | ||
templates: { | ||
inputValue, | ||
suggestion, | ||
}, | ||
onConfirm: onConfirm(input), | ||
confirmOnBlur: false, | ||
autoselect: true, | ||
}); | ||
|
||
// Hijack the original input to submit the selected value. | ||
input.id = `old-${input.id}`; | ||
input.name = inputName; | ||
input.type = "hidden"; | ||
} | ||
} catch (err) { | ||
console.error(`Failed to initialise autocomplete for ${elementId}:`, err); | ||
} | ||
}; | ||
|
||
export default initAutocomplete; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
const getPath = (endpoint, query) => { | ||
return `${endpoint}?query=${query}`; | ||
}; | ||
|
||
const request = (endpoint) => { | ||
let xhr = null; // Hoist this call so that we can abort previous requests. | ||
|
||
return (query, callback) => { | ||
if (xhr && xhr.readyState !== XMLHttpRequest.DONE) { | ||
xhr.abort(); | ||
} | ||
const path = getPath(endpoint, query); | ||
|
||
xhr = new XMLHttpRequest(); | ||
xhr.addEventListener("load", (evt) => { | ||
let results = []; | ||
try { | ||
results = JSON.parse(xhr.responseText); | ||
} catch (err) { | ||
console.error( | ||
`Failed to parse results from endpoint ${path}, error is:`, | ||
err, | ||
); | ||
} | ||
callback(results); | ||
}); | ||
xhr.open("GET", path); | ||
xhr.send(); | ||
}; | ||
}; | ||
|
||
export { getPath, request }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
class AccreditedProviderApi | ||
include ServicePattern | ||
|
||
def initialize(code = nil) | ||
@code = code | ||
end | ||
|
||
attr_reader :code | ||
|
||
def call | ||
code.present? ? provider_list(code) : provider_details | ||
end | ||
|
||
private | ||
|
||
def provider_list(code) | ||
Rails | ||
.cache | ||
.fetch("accredited_provider_details_#{code}", expires_in: 24.hours) do | ||
response = HTTParty.get(provider_details_url(code)) | ||
response = JSON.parse(response.to_s) | ||
response["data"] | ||
end | ||
end | ||
|
||
def provider_details | ||
Rails | ||
.cache | ||
.fetch("all_accredited_providers", expires_in: 24.hours) do | ||
response = HTTParty.get(all_providers_url) | ||
response = JSON.parse(response.to_s) | ||
response["data"] | ||
end | ||
end | ||
|
||
def all_providers_url | ||
"#{ENV["PUBLISH_BASE_URL"]}/api/public/v1/recruitment_cycles/#{next_year}/providers?filter[is_accredited_body]=true" | ||
end | ||
|
||
def provider_details_url(code) | ||
"#{ENV["PUBLISH_BASE_URL"]}/api/public/v1/recruitment_cycles/#{next_year}/providers/#{code}" | ||
end | ||
|
||
def next_year | ||
Time.current.next_year.year | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.