From 4f1bdcda22a6ff3ba13011c929c08ffb2d7865cc Mon Sep 17 00:00:00 2001 From: Arit Amana <32520970+msarit@users.noreply.github.com> Date: Fri, 5 Nov 2021 09:11:42 -0400 Subject: [PATCH] [Small Wins] Show character limits for profile inputs (#15255) * Start implementation * Extend raw implementation; will refactor later * still building * basic implementation * create JS pack * Rename JS pack; use existing CSS property * complete implementation for profile fields * Add guard againt irrelevant profile fields * extract view logic to helper and add specs --- app/helpers/profile_helper.rb | 9 +++ app/javascript/packs/userProfileSettings.js | 32 +++++++++ app/views/users/_profile.html.erb | 73 ++++++++++++++++++--- spec/helpers/profile_helper_spec.rb | 10 +++ 4 files changed, 116 insertions(+), 8 deletions(-) create mode 100644 app/javascript/packs/userProfileSettings.js diff --git a/app/helpers/profile_helper.rb b/app/helpers/profile_helper.rb index 34627e4e5..333971607 100644 --- a/app/helpers/profile_helper.rb +++ b/app/helpers/profile_helper.rb @@ -15,4 +15,13 @@ module ProfileHelper { github: urls["GitHub"], twitter: urls["Twitter"], facebook: urls["Facebook"] }.compact end + + def character_count_denominator(field_type) + case field_type + when "text_field" + "100" + when "text_area" + "200" + end + end end diff --git a/app/javascript/packs/userProfileSettings.js b/app/javascript/packs/userProfileSettings.js new file mode 100644 index 000000000..8013fd49e --- /dev/null +++ b/app/javascript/packs/userProfileSettings.js @@ -0,0 +1,32 @@ +const userFieldIds = ['user[name]', 'user[email]', 'user[username]']; +const profileFieldIds = Array.from( + document.querySelectorAll('[id^="profile["]'), +).map((node) => node.id); +const allFieldIds = [...userFieldIds, ...profileFieldIds]; + +export function fieldCharacterLimits() { + window.addEventListener('load', () => { + allFieldIds.forEach((field_id) => { + const field = document.getElementById(field_id); + const fieldValueLength = field.value.length; + const fieldCharacterSpan = document.getElementById( + field.dataset.characterSpanId, + ); + fieldCharacterSpan.innerHTML = fieldValueLength; + }); + + document + .getElementById('user-profile-form') + .addEventListener('keyup', (event) => { + if (!event.target.dataset.characterSpanId) { + return; + } + + document.getElementById( + event.target.dataset.characterSpanId, + ).innerHTML = event.target.value.length; + }); + }); +} + +fieldCharacterLimits(); diff --git a/app/views/users/_profile.html.erb b/app/views/users/_profile.html.erb index 9ea4bf46e..14976e153 100644 --- a/app/views/users/_profile.html.erb +++ b/app/views/users/_profile.html.erb @@ -1,20 +1,40 @@ -<%= javascript_packs_with_chunks_tag "colorPreview", "validateFileInputs", "stickySaveFooter", defer: true %> +<%= javascript_packs_with_chunks_tag "colorPreview", "stickySaveFooter", "userProfileSettings", "validateFileInputs", defer: true %> <%= render "users/additional_authentication" %> -<%= form_with(url: profile_path(@user.profile_id), method: "put", html: { class: "sticky-footer-form" }) do |f| %> +<%= form_with(url: profile_path(@user.profile_id), method: "put", html: { class: "sticky-footer-form", id: "user-profile-form" }) do |f| %>
<%= field.description %>
diff --git a/spec/helpers/profile_helper_spec.rb b/spec/helpers/profile_helper_spec.rb index 6d45b9d63..74c28d493 100644 --- a/spec/helpers/profile_helper_spec.rb +++ b/spec/helpers/profile_helper_spec.rb @@ -68,4 +68,14 @@ describe ProfileHelper do end end end + + describe "character_count_denominator" do + it "returns '100' when the field_type is 'text_field'" do + expect(helper.character_count_denominator("text_field")).to eq("100") + end + + it "returns '200' when the field_type is 'text_area'" do + expect(helper.character_count_denominator("text_area")).to eq("200") + end + end end