diff --git a/app/controllers/goal_pledges_controller.rb b/app/controllers/goal_pledges_controller.rb index c88c87c78..66c80fec5 100644 --- a/app/controllers/goal_pledges_controller.rb +++ b/app/controllers/goal_pledges_controller.rb @@ -4,9 +4,11 @@ class GoalPledgesController < ApplicationController rescue_from ActiveRecord::RecordNotFound, with: :record_not_found def new + account = preselected_account @pledge = @goal.goal_pledges.new( currency: @goal.currency, - kind: default_kind_for(@goal), + account: account, + kind: kind_for_account(account), amount: params[:amount].presence ) end @@ -14,7 +16,7 @@ class GoalPledgesController < ApplicationController def create @pledge = @goal.goal_pledges.new(pledge_params) @pledge.account = lookup_account(params.dig(:goal_pledge, :account_id)) - @pledge.kind = default_kind_for(@goal) + @pledge.kind = kind_for_account(@pledge.account) @pledge.currency = @goal.currency if @pledge.save @@ -62,8 +64,20 @@ class GoalPledgesController < ApplicationController @goal.linked_accounts.find_by(id: id) end - def default_kind_for(goal) - goal.any_connected_account? ? "transfer" : "manual_save" + def preselected_account + requested = params[:account_id].presence && @goal.linked_accounts.find_by(id: params[:account_id]) + requested || @goal.linked_accounts.first + end + + # Per-account: manual accounts get a `manual_save` pledge (resolves on the + # user's next valuation), connected accounts get a `transfer` pledge + # (resolves when the synced deposit posts). Account-level avoids the + # mixed-funding goal bug where the goal-level toggle picked one kind for + # all pledges regardless of which account the user actually moved money + # into. + def kind_for_account(account) + return "transfer" if account.nil? + account.manual? ? "manual_save" : "transfer" end def record_not_found diff --git a/app/javascript/controllers/goal_contribution_preview_controller.js b/app/javascript/controllers/goal_pledge_preview_controller.js similarity index 89% rename from app/javascript/controllers/goal_contribution_preview_controller.js rename to app/javascript/controllers/goal_pledge_preview_controller.js index 22299f69a..8d7601aa6 100644 --- a/app/javascript/controllers/goal_contribution_preview_controller.js +++ b/app/javascript/controllers/goal_pledge_preview_controller.js @@ -1,9 +1,8 @@ import { Controller } from "@hotwired/stimulus"; -// Live impact preview for the add-contribution modal. Reads current -// balance + target amount from values and updates a preview sentence -// each keystroke. Template strings come from ERB so the wording stays -// localized. +// Live impact preview for the record-pledge modal. Reads current balance + +// target amount from values and updates a preview sentence each keystroke. +// Template strings come from ERB so the wording stays localized. export default class extends Controller { static targets = ["amountInput", "preview"]; static values = { diff --git a/app/views/goal_pledges/new.html.erb b/app/views/goal_pledges/new.html.erb index 409c37cae..ee0ab326a 100644 --- a/app/views/goal_pledges/new.html.erb +++ b/app/views/goal_pledges/new.html.erb @@ -1,52 +1,49 @@ <%= render DS::Dialog.new do |dialog| %> <% dialog.with_header(title: t(@goal.pledge_action_label_key)) %> <% dialog.with_body do %> - <%= styled_form_with model: @pledge, url: goal_pledges_path(@goal), class: "form" do |form| %> - <% if @pledge.errors.any? %> -
<%= msg %>
- <% end %> -+ <%= styled_form_with model: @pledge, + url: goal_pledges_path(@goal), + class: "space-y-3", + data: { + controller: "goal-pledge-preview", + goal_pledge_preview_current_balance_value: @goal.current_balance.to_f, + goal_pledge_preview_target_amount_value: @goal.target_amount.to_f, + goal_pledge_preview_currency_value: @goal.currency, + goal_pledge_preview_template_zero_value: t(".preview_zero"), + goal_pledge_preview_template_nonzero_value: t(".preview_nonzero"), + goal_pledge_preview_template_reached_value: t(".preview_reached") + } do |f| %> +
<% if @goal.any_connected_account? %> - <%= t("goal_pledges.new.helper_transfer") %> + <%= t(".helper_transfer") %> <% else %> - <%= t("goal_pledges.new.helper_manual") %> + <%= t(".helper_manual") %> <% end %>
-