-
Notifications
You must be signed in to change notification settings - Fork 587
feat(goals): show what each account still has room to earmark #3166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jjmata
merged 5 commits into
we-promise:main
from
buzzromain:feat/goal-earmark-headroom
Aug 26, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
cfa27c8
feat(goals): show what each account still has room to earmark
buzzromain 98fb2fa
fix(goals): read the typed amount strictly, and format it in the app'…
buzzromain f9f8135
fix(goals): let the assistant create a second goal on a claimed account
buzzromain 4b8ff01
Merge branch 'main' into feat/goal-earmark-headroom
jjmata 3910f18
test(goals): move the spend tests back out of the private section
buzzromain File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| import { Controller } from "@hotwired/stimulus" | ||
|
|
||
| // Tells the user what each funding account still has room for, as they type. | ||
| // | ||
| // Nothing here is an error, and the wording matters more than the maths: a | ||
| // goal in progress legitimately claims more than its account holds. An | ||
| // account of 6,000 backing two goals of 5,000 is a correct setup, not an | ||
| // over-allocation, so a warning phrased as one would fire permanently. The | ||
| // pro-rata message states the consequence instead of scolding. | ||
| // | ||
| // Deliberately separate from goal-form, which is already at 10 targets | ||
| // against the 7 the project guidelines suggest. Three here, and no shared | ||
| // state between them. | ||
| export default class extends Controller { | ||
| static targets = ["allocationInput", "warning", "checkbox"] | ||
| static values = { | ||
| currency: String, | ||
| locale: String, | ||
| wholeBalance: String, | ||
| prorata: String, | ||
| headroom: String, | ||
| } | ||
|
|
||
| // A complete number, optionally with one decimal separator and digits after | ||
| // it. `Number.parseFloat` alone accepts prefixes — "500abc" becomes 500 — | ||
| // and a bare comma-to-dot swap turns the thousands-separated "1,500" into | ||
| // 1.5, so a typo or a habit from another locale silently changed the amount | ||
| // the preview was based on. | ||
| static ALLOCATION_PATTERN = /^\d+(?:[.,]\d+)?$/ | ||
|
|
||
| connect() { | ||
| this.refresh() | ||
| } | ||
|
|
||
| refresh() { | ||
| this.allocationInputTargets.forEach((input) => this.#refreshRow(input)) | ||
| } | ||
|
|
||
| #refreshRow(input) { | ||
| const row = input.closest("[data-balance]") | ||
| if (!row) return | ||
|
|
||
| const warning = row.querySelector('[data-goal-earmark-target="warning"]') | ||
| const checkbox = row.querySelector('input[type="checkbox"]') | ||
| if (!warning) return | ||
|
|
||
| // An unchecked account funds nothing, so it has nothing to say. | ||
| if (checkbox && !checkbox.checked) return this.#hide(warning) | ||
|
|
||
| const balance = Number.parseFloat(row.dataset.balance || "0") | ||
| const others = Number.parseFloat(row.dataset.earmarkedByOthers || "0") | ||
| const raw = input.value.trim() | ||
|
|
||
| if (raw === "") { | ||
| return this.#show(warning, this.wholeBalanceValue) | ||
| } | ||
|
|
||
| if (!this.constructor.ALLOCATION_PATTERN.test(raw)) return this.#hide(warning) | ||
|
|
||
| const entered = Number.parseFloat(raw.replace(",", ".")) | ||
| if (Number.isNaN(entered)) return this.#hide(warning) | ||
|
|
||
| const total = others + entered | ||
|
|
||
| if (total > balance) { | ||
| this.#show( | ||
| warning, | ||
| this.prorataValue | ||
| .replace("{total}", this.#money(total)) | ||
| .replace("{balance}", this.#money(balance)), | ||
| ) | ||
| } else { | ||
| this.#show(warning, this.headroomValue.replace("{left}", this.#money(balance - total))) | ||
| } | ||
| } | ||
|
|
||
| #show(element, text) { | ||
| element.textContent = text | ||
| element.classList.remove("hidden") | ||
| } | ||
|
|
||
| #hide(element) { | ||
| element.classList.add("hidden") | ||
| } | ||
|
|
||
| #money(value) { | ||
| try { | ||
| return new Intl.NumberFormat(this.localeValue || undefined, { | ||
| style: "currency", | ||
| currency: this.currencyValue || "USD", | ||
| maximumFractionDigits: 0, | ||
| }).format(value) | ||
| } catch { | ||
| return `${this.currencyValue || "$"}${Math.round(value).toLocaleString()}` | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| <%= render DS::Dialog.new do |dialog| %> | ||
| <% dialog.with_header(title: t(".heading")) %> | ||
| <% dialog.with_body do %> | ||
| <%= render "form", goal: @goal, linkable_accounts: @linkable_accounts, currently_linked_account_ids: @currently_linked_account_ids %> | ||
| <%= render "form", goal: @goal, linkable_accounts: @linkable_accounts, currently_linked_account_ids: @currently_linked_account_ids, pooled_allocations: @pooled_allocations %> | ||
| <% end %> | ||
| <% end %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.