diff --git a/app/assets/javascripts/preferences.js b/app/assets/javascripts/preferences.js new file mode 100644 index 0000000000..977a586e13 --- /dev/null +++ b/app/assets/javascripts/preferences.js @@ -0,0 +1,32 @@ +$(document).ready(function () { + $("#available_languages_menu").prop("hidden", false); + $("#user_languages, #available_languages").on("input", updateButtons); + updateButtons(); + + $("#add_preferred_language").click(function () { + var preferredLanguagesValue = $("#user_languages").val().trim(); + var selectedLanguage = $("#available_languages").val(); + $("#user_languages").val(selectedLanguage + " " + preferredLanguagesValue); + updateButtons(); + $("#remove_preferred_language").trigger("focus"); + }); + + $("#remove_preferred_language").click(function () { + var preferredLanguages = $("#user_languages").val().trim().split(/\s+/); + var selectedLanguage = $("#available_languages").val(); + var updatedPreferredLanguages = preferredLanguages.filter(function (language) { + return language !== selectedLanguage; + }); + $("#user_languages").val(updatedPreferredLanguages.join(" ")); + updateButtons(); + $("#add_preferred_language").trigger("focus"); + }); + + function updateButtons() { + var preferredLanguages = $("#user_languages").val().trim().split(/\s+/); + var selectedLanguage = $("#available_languages").val(); + var languageAlreadyAdded = preferredLanguages.includes(selectedLanguage); + $("#add_preferred_language").prop("hidden", languageAlreadyAdded); + $("#remove_preferred_language").prop("hidden", !languageAlreadyAdded); + } +}); diff --git a/app/views/preferences/edit.html.erb b/app/views/preferences/edit.html.erb index 07d89fbb5b..7051e7acaa 100644 --- a/app/views/preferences/edit.html.erb +++ b/app/views/preferences/edit.html.erb @@ -1,3 +1,7 @@ +<% content_for :head do %> + <%= javascript_include_tag "preferences" %> +<% end %> + <% content_for :heading do %>

<%= t ".title" %>

<% end %> @@ -7,6 +11,19 @@ <%= f.text_field :languages %> + + <%= f.primary t(".save") %> <%= link_to t(".cancel"), preferences_path, :class => "btn btn-link" %> <% end %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 8114c8b10b..2e0f567f44 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1773,6 +1773,9 @@ en: edit_preferences: Edit Preferences edit: title: Edit Preferences + available_languages: Available Languages + add: Add + remove: Remove save: Update Preferences cancel: Cancel update: diff --git a/test/system/preferences_test.rb b/test/system/preferences_test.rb index 59a7209f5b..465287c1d3 100644 --- a/test/system/preferences_test.rb +++ b/test/system/preferences_test.rb @@ -1,6 +1,12 @@ require "application_system_test_case" class PreferencesTest < ApplicationSystemTestCase + def setup + create(:language, :code => "en") + create(:language, :code => "fr", :english_name => "French", :native_name => "Français") + create(:language, :code => "de", :english_name => "German", :native_name => "Deutsch") + end + def test_flash_message_shows_in_original_language sign_in_as(create(:user)) @@ -19,4 +25,30 @@ def test_flash_message_shows_in_new_language assert_content "Préférences mises à jour" end + + def test_language_dropdown + user = create(:user, :languages => %w[en fr]) + sign_in_as(user) + + visit edit_preferences_path + assert_field "Preferred Languages", :with => "en fr" + + select "French", :from => "Available Languages" + assert_no_button "Add" + assert_button "Remove" + + click_on "Remove" + assert_field "Preferred Languages", :with => "en" + assert_button "Add" + assert_no_button "Remove" + + click_on "Add" + assert_field "Preferred Languages", :with => "fr en" + assert_no_button "Add" + assert_button "Remove" + + fill_in "Preferred Languages", :with => "de en" + assert_button "Add" + assert_no_button "Remove" + end end