diff --git a/.eslintrc.json b/.eslintrc.json index a1e1fa6..6422e97 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -31,6 +31,12 @@ "semi": [ "error", "always" - ] + ], + "prefer-destructuring": ["error", { + "array": false, + "object": false + }, { + "enforceForRenamedProperties": false + }] } } \ No newline at end of file diff --git a/app/controllers/research_projects_controller.rb b/app/controllers/research_projects_controller.rb new file mode 100644 index 0000000..d917ca8 --- /dev/null +++ b/app/controllers/research_projects_controller.rb @@ -0,0 +1,30 @@ +class ResearchProjectsController < ApplicationController + + + DEFAULT_FUNDER_TYPE = "H2020" + + def index + render json: research_projects + end + + def search + @results = research_projects.select { |r| r.description.match(params[:description]) } + logger.debug("Returning #{@results.count} results") + render json: @results + end + + private + + def research_projects + @research_projects ||= begin + Rails.cache.fetch(["research_projects", funder_type], expires_in: 1.day) do + Thread.new { OpenAireRequest.new(funder_type).get!.results }.value + end + end + end + + def funder_type + params.fetch(:type, DEFAULT_FUNDER_TYPE) + end + +end diff --git a/app/javascript/views/plans/edit_details.js b/app/javascript/views/plans/edit_details.js index b62bb57..be132cb 100644 --- a/app/javascript/views/plans/edit_details.js +++ b/app/javascript/views/plans/edit_details.js @@ -1,7 +1,11 @@ import { Tinymce } from '../../utils/tinymce'; import getConstant from '../../constants'; +import 'bootstrap-3-typeahead'; $(() => { + const grantIdField = $('.grant-id-typeahead'); + const grantIdHidden = $('input#plan_grant_number'); + Tinymce.init(); $('#is_test').click((e) => { $('#plan_visibility').val($(e.target).is(':checked') ? 'is_test' : 'privately_visible'); @@ -53,6 +57,48 @@ toggleCheckboxes(selections); }; + const grantNumberInfo = (grantId) => { + return `Grant number: ${grantId}`; + } + + const setUpTypeahead = () => { + if ($('.edit_plan').length) { + $.get('/research_projects.json', (data) => { + window.researchProjects = data; + const descriptionData = $.map(data, datum => datum.description); + grantIdField.typeahead({ source: descriptionData }); + }).then(function() { + setInitialGrantProjectName(); + }); + grantIdField.on('change', () => { + const current = grantIdField.typeahead('getActive'); + if (current) { + // match or partial match found + const currentResearchProject = window.researchProjects.find((datum) => { + const fixString = string => String(string).toLowerCase(); + return fixString(datum.description) === fixString(current); + }); + if (currentResearchProject) { + const grantId = currentResearchProject.grant_id + $('#grant_number_info').html(grantNumberInfo(grantId)); + grantIdHidden.val(grantId); + } + } else { + $('#grant_number_info').html(grantNumberInfo('')); + grantIdHidden.val(''); + } + }); + } + }; + + const setInitialGrantProjectName = () => { + const grantId = grantIdHidden.val(); + const researchProject = researchProjects.find(datum => datum.grant_id === grantId); + if (researchProject) { + grantIdField.val(researchProject.description); + } + }; + $('#other-guidance-orgs').find('input[type="checkbox"]').click((e) => { const checkbox = $(e.target); // Since this is the modal window, copy any selections over to the priority list @@ -70,9 +116,12 @@ } syncGuidance(checkbox.closest('ul[id]')); }); + $('#priority-guidance-orgs').find('input[type="checkbox"]').click((e) => { syncGuidance($(e.target).closest('ul[id]')); }); toggleCheckboxes($('#priority-guidance-orgs input[type="checkbox"]:checked').map((i, el) => $(el).val()).get()); + + setUpTypeahead(); }); diff --git a/app/models/research_project.rb b/app/models/research_project.rb new file mode 100644 index 0000000..66ebda1 --- /dev/null +++ b/app/models/research_project.rb @@ -0,0 +1,13 @@ +# frozen_string_literal + +class ResearchProject < Struct.new(:grant_id, :description) + + def to_json(val = nil) + { grant_id: grant_id, description: description }.to_json + end + + def id + object_id + end + +end diff --git a/app/views/plans/_edit_details.html.erb b/app/views/plans/_edit_details.html.erb index 38143d4..0475ecc 100644 --- a/app/views/plans/_edit_details.html.erb +++ b/app/views/plans/_edit_details.html.erb @@ -30,11 +30,16 @@