mirror of
https://github.com/we-promise/sure.git
synced 2026-04-07 22:34:47 +00:00
Lunchflow fix (#307)
* Fix lunch flow pre-loading and UX * Small UX fixes - Proper closing of modal on cancel - Preload on new account already * Review comments * Fix json error * Delete .claude/settings.local.json Signed-off-by: soky srm <sokysrm@gmail.com> * Lunch Flow brand (again :-) * FIX process only linked accounts * FIX disable accounts with no name * Fix string normalization --------- Signed-off-by: soky srm <sokysrm@gmail.com> Co-authored-by: Juan José Mata <juanjo.mata@gmail.com>
This commit is contained in:
89
app/javascript/controllers/lunchflow_preload_controller.js
Normal file
89
app/javascript/controllers/lunchflow_preload_controller.js
Normal file
@@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user