Skip to content

Commit

Permalink
Expose preferred language in member admin (#746)
Browse files Browse the repository at this point in the history
Add a method to return the language name, or a warning for an invalid language code.

---------

Co-authored-by: Rob Galanakis <[email protected]>
  • Loading branch information
DeeTheDev and rgalanakis authored Dec 3, 2024
1 parent 60bead2 commit b230e68
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 0 deletions.
4 changes: 4 additions & 0 deletions adminapp/src/pages/MemberDetailPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ export default function MemberDetailPage() {
<DetailGrid
title="Other Information"
properties={[
{
label: "Preferred Language",
value: model.preferences.preferredLanguageName,
},
{ label: "Timezone", value: model.timezone },
{ label: "Created At", value: dayjs(model.createdAt) },
{
Expand Down
1 change: 1 addition & 0 deletions lib/suma/admin_api/members.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class PreferencesSubscriptionEntity < BaseEntity
class PreferencesEntity < BaseEntity
expose :public_url
expose :subscriptions, with: PreferencesSubscriptionEntity
expose :preferred_language_name
end

class DetailedMemberEntity < MemberEntity
Expand Down
6 changes: 6 additions & 0 deletions lib/suma/message/preferences.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ def subscriptions

def public_url = "#{Suma.app_url}/preferences-public?token=#{self.access_token}"

def preferred_language_name
return Suma::I18n::SUPPORTED_LOCALES.fetch(self.preferred_language).language
rescue KeyError
return "Invalid (#{self.preferred_language})"
end

def validate
super
self.validates_includes Suma::I18n.enabled_locale_codes, :preferred_language
Expand Down
2 changes: 2 additions & 0 deletions spec/suma/admin_api/members_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,14 @@ def make_item(i)
cash_ledger = Suma::Fixtures.ledger.member(admin).category(:cash).create
charge1 = Suma::Fixtures.charge(member: admin).create
charge1.add_book_transaction(Suma::Fixtures.book_transaction.from(cash_ledger).create)
admin.preferences!.update(preferred_language: "es")

get "/v1/members/#{admin.id}"

expect(last_response).to have_status(200)
expect(last_response).to have_json_body.that_includes(
sessions: contain_exactly(include(:ip_lookup_link)),
preferences: include(preferred_language_name: "Spanish"),
)
end
end
Expand Down
14 changes: 14 additions & 0 deletions spec/suma/message/preferences_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,18 @@
deliveries = Suma::Fixtures.member.create.message_preferences!.update(preferred_language: "fr").dispatch(msg)
expect(deliveries).to contain_exactly(have_attributes(template: "specs/localized", template_language: "fr"))
end

describe "preferred_language_name" do
it "returns the language name" do
pref = Suma::Fixtures.member.create.message_preferences!
pref.preferred_language = "es"
expect(pref).to have_attributes(preferred_language_name: "Spanish")
end

it "uses a clear value if the message is set to an invalid locale" do
pref = Suma::Fixtures.member.create.message_preferences!
pref.preferred_language = "zz"
expect(pref).to have_attributes(preferred_language_name: "Invalid (zz)")
end
end
end

0 comments on commit b230e68

Please sign in to comment.