-
Notifications
You must be signed in to change notification settings - Fork 97
Lunchflow fix #307
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
Lunchflow fix #307
Changes from 6 commits
58adf90
8ca7a12
0aab417
d30bed5
d1cdb0c
7693daf
9555a4d
4bb5066
daeebfa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| import { Controller } from "@hotwired/stimulus"; | ||
|
|
||
| // Connects to data-controller="lunchflow-preload" | ||
| export default class extends Controller { | ||
| static targets = ["link", "spinner"]; | ||
| static values = { | ||
| accountableType: String, | ||
| returnTo: String, | ||
| }; | ||
|
|
||
| connect() { | ||
| this.preloadAccounts(); | ||
| } | ||
|
|
||
| async preloadAccounts() { | ||
| try { | ||
| // Show loading state if we have a link target (on method selector page) | ||
| if (this.hasLinkTarget) { | ||
| this.showLoading(); | ||
| } | ||
|
|
||
| // Fetch accounts in background to populate cache | ||
| const url = new URL( | ||
| "/lunchflow_items/preload_accounts", | ||
| window.location.origin | ||
| ); | ||
| if (this.hasAccountableTypeValue) { | ||
| url.searchParams.append("accountable_type", this.accountableTypeValue); | ||
| } | ||
| if (this.hasReturnToValue) { | ||
| url.searchParams.append("return_to", this.returnToValue); | ||
| } | ||
|
|
||
| const csrfToken = document.querySelector('[name="csrf-token"]'); | ||
| const headers = { | ||
| Accept: "application/json", | ||
| }; | ||
| if (csrfToken) { | ||
| headers["X-CSRF-Token"] = csrfToken.content; | ||
| } | ||
|
|
||
| const response = await fetch(url, { headers }); | ||
|
|
||
| if (!response.ok) { | ||
| throw new Error(`HTTP error! status: ${response.status}`); | ||
| } | ||
|
|
||
| const data = await response.json(); | ||
|
|
||
| if (data.success && data.has_accounts) { | ||
| // Accounts loaded successfully, enable the link | ||
| if (this.hasLinkTarget) { | ||
| this.hideLoading(); | ||
| } | ||
| } else if (!data.has_accounts) { | ||
| // No accounts available, hide the link entirely | ||
| if (this.hasLinkTarget) { | ||
| this.linkTarget.style.display = "none"; | ||
| } | ||
| } else { | ||
| // Error occurred | ||
| if (this.hasLinkTarget) { | ||
| this.hideLoading(); | ||
| } | ||
| console.error("Failed to preload Lunchflow accounts:", data.error); | ||
| } | ||
| } catch (error) { | ||
| // On error, still enable the link so user can try | ||
| if (this.hasLinkTarget) { | ||
| this.hideLoading(); | ||
| } | ||
| console.error("Error preloading Lunchflow accounts:", error); | ||
| } | ||
| } | ||
|
|
||
| showLoading() { | ||
| this.linkTarget.classList.add("pointer-events-none", "opacity-50"); | ||
| if (this.hasSpinnerTarget) { | ||
| this.spinnerTarget.classList.remove("hidden"); | ||
| } | ||
| } | ||
|
|
||
| hideLoading() { | ||
| this.linkTarget.classList.remove("pointer-events-none", "opacity-50"); | ||
| if (this.hasSpinnerTarget) { | ||
| this.spinnerTarget.classList.add("hidden"); | ||
| } | ||
| } | ||
sokie marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,10 +21,10 @@ def get_accounts | |
|
|
||
| handle_response(response) | ||
| rescue SocketError, Net::OpenTimeout, Net::ReadTimeout => e | ||
| Rails.logger.error "Lunchflow API: GET /accounts failed: #{e.class}: #{e.message}" | ||
| Rails.logger.error "Lunch Flow API: GET /accounts failed: #{e.class}: #{e.message}" | ||
| raise LunchflowError.new("Exception during GET request: #{e.message}", :request_failed) | ||
| rescue => e | ||
| Rails.logger.error "Lunchflow API: Unexpected error during GET /accounts: #{e.class}: #{e.message}" | ||
| Rails.logger.error "Lunch Flow API: Unexpected error during GET /accounts: #{e.class}: #{e.message}" | ||
| raise LunchflowError.new("Exception during GET request: #{e.message}", :request_failed) | ||
| end | ||
|
|
||
|
|
@@ -52,10 +52,10 @@ def get_account_transactions(account_id, start_date: nil, end_date: nil) | |
|
|
||
| handle_response(response) | ||
| rescue SocketError, Net::OpenTimeout, Net::ReadTimeout => e | ||
| Rails.logger.error "Lunchflow API: GET #{path} failed: #{e.class}: #{e.message}" | ||
| Rails.logger.error "Lunch Flow API: GET #{path} failed: #{e.class}: #{e.message}" | ||
| raise LunchflowError.new("Exception during GET request: #{e.message}", :request_failed) | ||
| rescue => e | ||
| Rails.logger.error "Lunchflow API: Unexpected error during GET #{path}: #{e.class}: #{e.message}" | ||
| Rails.logger.error "Lunch Flow API: Unexpected error during GET #{path}: #{e.class}: #{e.message}" | ||
| raise LunchflowError.new("Exception during GET request: #{e.message}", :request_failed) | ||
| end | ||
|
|
||
|
|
@@ -71,10 +71,10 @@ def get_account_balance(account_id) | |
|
|
||
| handle_response(response) | ||
| rescue SocketError, Net::OpenTimeout, Net::ReadTimeout => e | ||
| Rails.logger.error "Lunchflow API: GET #{path} failed: #{e.class}: #{e.message}" | ||
| Rails.logger.error "Lunch Flow API: GET #{path} failed: #{e.class}: #{e.message}" | ||
| raise LunchflowError.new("Exception during GET request: #{e.message}", :request_failed) | ||
| rescue => e | ||
| Rails.logger.error "Lunchflow API: Unexpected error during GET #{path}: #{e.class}: #{e.message}" | ||
| Rails.logger.error "Lunch Flow API: Unexpected error during GET #{path}: #{e.class}: #{e.message}" | ||
| raise LunchflowError.new("Exception during GET request: #{e.message}", :request_failed) | ||
| end | ||
|
|
||
|
|
@@ -93,7 +93,7 @@ def handle_response(response) | |
| when 200 | ||
| JSON.parse(response.body, symbolize_names: true) | ||
| when 400 | ||
| Rails.logger.error "Lunchflow API: Bad request - #{response.body}" | ||
| Rails.logger.error "Lunch Flow API: Bad request - #{response.body}" | ||
| raise LunchflowError.new("Bad request to Lunchflow API: #{response.body}", :bad_request) | ||
|
Comment on lines
+96
to
97
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Inconsistent naming: one instance of "Lunchflow API" remains. Line 96 was updated to "Lunch Flow API", but line 97 still contains "Lunchflow API" in the error message. This creates inconsistency with the rest of the branding updates in this PR. Apply this diff to complete the branding update: - raise LunchflowError.new("Bad request to Lunchflow API: #{response.body}", :bad_request)
+ raise LunchflowError.new("Bad request to Lunch Flow API: #{response.body}", :bad_request)🤖 Prompt for AI Agents |
||
| when 401 | ||
| raise LunchflowError.new("Invalid API key", :unauthorized) | ||
|
|
@@ -104,7 +104,7 @@ def handle_response(response) | |
| when 429 | ||
| raise LunchflowError.new("Rate limit exceeded. Please try again later.", :rate_limited) | ||
| else | ||
| Rails.logger.error "Lunchflow API: Unexpected response - Code: #{response.code}, Body: #{response.body}" | ||
| Rails.logger.error "Lunch Flow API: Unexpected response - Code: #{response.code}, Body: #{response.body}" | ||
| raise LunchflowError.new("Failed to fetch data: #{response.code} #{response.message} - #{response.body}", :fetch_failed) | ||
| end | ||
| end | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.