From e250b46ed23b3a057e5cea00a48d6097e68600e0 Mon Sep 17 00:00:00 2001 From: Ridhwana Date: Fri, 10 Dec 2021 17:07:40 +0200 Subject: [PATCH] Using a Form Object that will persist for the Creator Settings Form (#15684) * WIP: add a creatore settings form * WIP: updat the controller to use the Creator Settings FOREM * feat: use the creator settings form for the new action * feat: add some default values for the new action * a note about form data * update the initiaize function to set some default values * feat: update the form to use the model data * feat: permit adn use the attributes within creator_settings_form * update the flash error * refactor: require and permit parameters * chore: use booleans, set defaults and validate the form * spec: update all the creator_settings tests * chore: remove comment * refactor: use self * feat: aggregate failures' * chore: remove the logo uploader in the controller * refactor: update error handling * feat: update the wasy the controller handles success and error * chore: remove the resource errors * feat: show flash message on new line * fix: use a redirect so that we can get back to /new * refactor: pass these values through as they seem to be caching whne setting them as default * chore: change default values * spec: update tests * Fix CreatorSettingsForm specs * fix: use a boolean for public * spec: add another test for the success var * fix: radio button labels to correspond + cyress specs * spec: update based on new changes * spec: update the params and the expected output * spec: update the comments and status * feat: no need for the initialize as we use Active Record Attributes * feat: update the tac and coc to be persisted when ticked * fix: amend spec * blank space * Message Co-authored-by: Michael Kohl --- .../initializers/initializePodcastPlayback.js | 6 +- .../admin/creator_settings_controller.rb | 46 ++++++------- app/forms/creator_settings_form.rb | 53 +++++++++++++++ .../onboarding/components/Navigation.jsx | 11 +--- app/models/settings/community.rb | 2 +- app/models/settings/user_experience.rb | 2 +- .../admin/creator_settings/_form.html.erb | 60 ++++++++--------- app/views/admin/creator_settings/new.html.erb | 26 ++------ app/views/admin/shared/_logo_upload.html.erb | 6 +- .../creatorSettings.spec.js | 1 + spec/forms/creator_settings_form_spec.rb | 64 +++++++++++++++++++ spec/requests/admin/creator_settings_spec.rb | 29 +++++---- 12 files changed, 201 insertions(+), 105 deletions(-) create mode 100644 app/forms/creator_settings_form.rb create mode 100644 spec/forms/creator_settings_form_spec.rb diff --git a/app/assets/javascripts/initializers/initializePodcastPlayback.js b/app/assets/javascripts/initializers/initializePodcastPlayback.js index 41b3e3eea..0e71aa256 100644 --- a/app/assets/javascripts/initializers/initializePodcastPlayback.js +++ b/app/assets/javascripts/initializers/initializePodcastPlayback.js @@ -475,7 +475,9 @@ function initializePodcastPlayback() { function handlePodcastMessages(event) { const message = JSON.parse(event.detail); - if (message.namespace !== 'podcast') { return } + if (message.namespace !== 'podcast') { + return; + } var currentState = currentAudioState(); switch (message.action) { @@ -506,7 +508,7 @@ function initializePodcastPlayback() { if (deviceType !== 'web') { Runtime.podcastMessage = (msg) => { window.ForemMobile.injectNativeMessage('podcast', msg); - } + }; } } diff --git a/app/controllers/admin/creator_settings_controller.rb b/app/controllers/admin/creator_settings_controller.rb index 9631d1432..004089b90 100644 --- a/app/controllers/admin/creator_settings_controller.rb +++ b/app/controllers/admin/creator_settings_controller.rb @@ -4,6 +4,14 @@ module Admin invite_only_mode logo primary_brand_color_hex public].freeze def new + @creator_settings_form = CreatorSettingsForm.new( + community_name: ::Settings::Community.community_name, + public: ::Settings::UserExperience.public, + invite_only_mode: ::Settings::Authentication.invite_only_mode, + primary_brand_color_hex: ::Settings::UserExperience.primary_brand_color_hex, + checked_code_of_conduct: current_user.checked_code_of_conduct, + checked_terms_and_conditions: current_user.checked_terms_and_conditions, + ) @max_file_size = LogoUploader::MAX_FILE_SIZE @logo_allowed_types = (LogoUploader::CONTENT_TYPE_ALLOWLIST + LogoUploader::EXTENSION_ALLOWLIST.map { |extension| ".#{extension}" }).join(",") @@ -11,27 +19,21 @@ module Admin def create extra_authorization - ActiveRecord::Base.transaction do - ::Settings::Community.community_name = settings_params[:community_name] - ::Settings::UserExperience.primary_brand_color_hex = settings_params[:primary_brand_color_hex] - ::Settings::Authentication.invite_only_mode = settings_params[:invite_only] - ::Settings::UserExperience.public = settings_params[:public] - if settings_params[:logo] - logo_uploader = upload_logo(settings_params[:logo]) - ::Settings::General.original_logo = logo_uploader.url - ::Settings::General.resized_logo = logo_uploader.resized_logo.url - end - end + @creator_settings_form = CreatorSettingsForm.new(settings_params) current_user.update!( - saw_onboarding: true, - checked_code_of_conduct: settings_params[:checked_code_of_conduct], - checked_terms_and_conditions: settings_params[:checked_terms_and_conditions], + checked_code_of_conduct: @creator_settings_form.checked_code_of_conduct, + checked_terms_and_conditions: @creator_settings_form.checked_terms_and_conditions, ) - redirect_to root_path - rescue StandardError => e - flash.now[:error] = e.message - render new_admin_creator_setting_path + @creator_settings_form.save + + if @creator_settings_form.success + current_user.update!(saw_onboarding: true) + redirect_to root_path + else + flash[:error] = @creator_settings_form.errors.full_messages + redirect_to new_admin_creator_setting_path + end end private @@ -41,13 +43,7 @@ module Admin end def settings_params - params.permit(ALLOWED_PARAMS) - end - - def upload_logo(image) - LogoUploader.new.tap do |uploader| - uploader.store!(image) - end + params.require(:creator_settings_form).permit(ALLOWED_PARAMS) end end end diff --git a/app/forms/creator_settings_form.rb b/app/forms/creator_settings_form.rb new file mode 100644 index 000000000..43cc173f2 --- /dev/null +++ b/app/forms/creator_settings_form.rb @@ -0,0 +1,53 @@ +class CreatorSettingsForm + include ActiveModel::Model + include ActiveModel::Attributes + + attribute :checked_code_of_conduct, :boolean, default: false + attribute :checked_terms_and_conditions, :boolean, default: false + attribute :community_name, :string + attribute :invite_only_mode, :boolean + attribute :logo + attribute :primary_brand_color_hex, :string + attribute :public, :boolean + + validates :community_name, + :primary_brand_color_hex, presence: true + + validates :checked_code_of_conduct, inclusion: { in: [true, false] } + validates :checked_terms_and_conditions, inclusion: { in: [true, false] } + validates :invite_only_mode, inclusion: { in: [true, false] } + validates :public, inclusion: { in: [true, false] } + + attr_accessor :success + + def save + if valid? + begin + ::Settings::Community.community_name = community_name + ::Settings::UserExperience.primary_brand_color_hex = primary_brand_color_hex + ::Settings::Authentication.invite_only_mode = invite_only_mode + ::Settings::UserExperience.public = public + + if logo + logo_uploader = upload_logo(logo) + ::Settings::General.original_logo = logo_uploader.url + ::Settings::General.resized_logo = logo_uploader.resized_logo.url + end + @success = true + rescue StandardError => e + errors.add(:base, e.message) + @success = false + end + else + @success = false + end + end + + private + + def upload_logo(image) + LogoUploader.new.tap do |uploader| + uploader.store!(image) + end + end +end diff --git a/app/javascript/onboarding/components/Navigation.jsx b/app/javascript/onboarding/components/Navigation.jsx index d68a9d7ec..ec3c6f7e4 100644 --- a/app/javascript/onboarding/components/Navigation.jsx +++ b/app/javascript/onboarding/components/Navigation.jsx @@ -52,15 +52,8 @@ export class Navigation extends Component { } render() { - const { - next, - prev, - hideNext, - hidePrev, - disabled, - canSkip, - className, - } = this.props; + const { next, prev, hideNext, hidePrev, disabled, canSkip, className } = + this.props; return (