From 5c33b4a621c02d6c34c50f69a87467ec872dc6f7 Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Wed, 8 Apr 2020 00:18:12 +1000 Subject: [PATCH] Profile action fixes --- .../discourse/components/wizard-mapper.js.es6 | 2 +- .../javascripts/discourse/lib/wizard.js.es6 | 2 + .../components/wizard-custom-action.hbs | 4 +- lib/custom_wizard/actions.rb | 54 ++++++++++++++----- 4 files changed, 46 insertions(+), 16 deletions(-) diff --git a/assets/javascripts/discourse/components/wizard-mapper.js.es6 b/assets/javascripts/discourse/components/wizard-mapper.js.es6 index b4e1f7a9..e08fa649 100644 --- a/assets/javascripts/discourse/components/wizard-mapper.js.es6 +++ b/assets/javascripts/discourse/components/wizard-mapper.js.es6 @@ -31,7 +31,7 @@ export default Component.extend({ }); selectionTypes.forEach(type => { - if (options[`${type}Selection`]) { + if (options[`${type}Selection`] !== undefined) { result[`${type}Selection`] = options[`${type}Selection`] } else { result[`${type}Selection`] = type === 'text' ? true : false; diff --git a/assets/javascripts/discourse/lib/wizard.js.es6 b/assets/javascripts/discourse/lib/wizard.js.es6 index 9ba65bab..d488c56b 100644 --- a/assets/javascripts/discourse/lib/wizard.js.es6 +++ b/assets/javascripts/discourse/lib/wizard.js.es6 @@ -36,6 +36,8 @@ const profileFields = [ 'avatar', 'date_of_birth', 'title', + 'profile_background', + 'card_background', 'locale', 'location', 'website', diff --git a/assets/javascripts/discourse/templates/components/wizard-custom-action.hbs b/assets/javascripts/discourse/templates/components/wizard-custom-action.hbs index e815a203..6c8c424b 100644 --- a/assets/javascripts/discourse/templates/components/wizard-custom-action.hbs +++ b/assets/javascripts/discourse/templates/components/wizard-custom-action.hbs @@ -138,8 +138,9 @@ {{wizard-mapper inputs=action.profile_updates options=(hash - single=true + singular=true inputTypes='association' + textSelection='value' userFieldSelection='key' wizardFieldSelection='value' keyDefaultSelection='userField' @@ -248,6 +249,7 @@ {{wizard-mapper inputs=action.custom_fields options=(hash + singular=true inputTypes='association' wizardFieldSelection='value' userFieldSelection='value' diff --git a/lib/custom_wizard/actions.rb b/lib/custom_wizard/actions.rb index e7f67e5a..1bdec074 100644 --- a/lib/custom_wizard/actions.rb +++ b/lib/custom_wizard/actions.rb @@ -63,15 +63,13 @@ class CustomWizard::Action def update_profile return unless (profile_updates = action['profile_updates']).length - - allowed_fields = CustomWizard::Mapper.user_fields + ['avatar'] params = {} profile_updates.first[:pairs].each do |pair| - if allowed_fields.include?(pair['key']) - key = pair['key'] - key = "#{key}_upload_url" if ['profile_background', 'card_background'].include?(key) - params[key.to_sym] = mapper.map_field(pair['value'], pair['value_type']) + if allowed_profile_fields.include?(pair['key']) + key = cast_profile_key(pair['key']).to_sym + value = mapper.map_field(pair['value'], pair['value_type']) + params[key] = cast_profile_value(value, pair['key']) end end @@ -85,14 +83,6 @@ class CustomWizard::Action end end end - - def update_avatar(upload) - user.create_user_avatar unless user.user_avatar - user.user_avatar.custom_upload_id = upload['id'] - user.uploaded_avatar_id = upload['id'] - user.save! - user.user_avatar.save! - end def send_to_api api_body = nil @@ -261,4 +251,40 @@ class CustomWizard::Action add_custom_fields(params) end + + def profile_url_fields + ['profile_background', 'card_background'] + end + + def cast_profile_key(key) + if profile_url_fields.include?(key) + "#{key}_upload_url" + else + key + end + end + + def cast_profile_value(value, key) + if profile_url_fields.include?(key) + value['url'] + elsif key === 'avatar' + value['id'] + else + value + end + end + + def allowed_profile_fields + CustomWizard::Mapper.user_fields + + profile_url_fields + + ['avatar'] + end + + def update_avatar(upload_id) + user.create_user_avatar unless user.user_avatar + user.user_avatar.custom_upload_id = upload_id + user.uploaded_avatar_id = upload_id + user.save! + user.user_avatar.save! + end end \ No newline at end of file