2024-11-15 13:49:37 -05:00
|
|
|
import { Controller } from "@hotwired/stimulus";
|
|
|
|
|
|
|
|
// Connects to data-controller="plaid"
|
|
|
|
export default class extends Controller {
|
|
|
|
static values = {
|
|
|
|
linkToken: String,
|
2025-01-31 17:04:26 -05:00
|
|
|
region: { type: String, default: "us" },
|
2025-02-12 12:59:35 -06:00
|
|
|
isUpdate: { type: Boolean, default: false },
|
2025-05-25 08:12:54 -04:00
|
|
|
itemId: String,
|
2024-11-15 13:49:37 -05:00
|
|
|
};
|
|
|
|
|
2025-05-25 08:12:54 -04:00
|
|
|
connect() {
|
|
|
|
this.open();
|
|
|
|
}
|
|
|
|
|
2024-11-15 13:49:37 -05:00
|
|
|
open() {
|
|
|
|
const handler = Plaid.create({
|
|
|
|
token: this.linkTokenValue,
|
|
|
|
onSuccess: this.handleSuccess,
|
|
|
|
onLoad: this.handleLoad,
|
|
|
|
onExit: this.handleExit,
|
2025-05-25 08:12:54 -04:00
|
|
|
onEvent: this.handleEvent,
|
2024-11-15 13:49:37 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
handler.open();
|
|
|
|
}
|
|
|
|
|
2025-01-31 17:04:26 -05:00
|
|
|
handleSuccess = (public_token, metadata) => {
|
2025-02-12 12:59:35 -06:00
|
|
|
if (this.isUpdateValue) {
|
|
|
|
// Trigger a sync to verify the connection and update status
|
|
|
|
fetch(`/plaid_items/${this.itemIdValue}/sync`, {
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
2025-05-25 08:12:54 -04:00
|
|
|
Accept: "application/json",
|
2025-02-12 12:59:35 -06:00
|
|
|
"Content-Type": "application/json",
|
|
|
|
"X-CSRF-Token": document.querySelector('[name="csrf-token"]').content,
|
2025-05-25 08:12:54 -04:00
|
|
|
},
|
2025-02-12 12:59:35 -06:00
|
|
|
}).then(() => {
|
|
|
|
// Refresh the page to show the updated status
|
|
|
|
window.location.href = "/accounts";
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2024-11-15 17:33:18 -05:00
|
|
|
|
2025-02-12 12:59:35 -06:00
|
|
|
// For new connections, create a new Plaid item
|
2024-11-15 13:49:37 -05:00
|
|
|
fetch("/plaid_items", {
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
"X-CSRF-Token": document.querySelector('[name="csrf-token"]').content,
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
|
|
|
plaid_item: {
|
|
|
|
public_token: public_token,
|
|
|
|
metadata: metadata,
|
2025-01-31 17:04:26 -05:00
|
|
|
region: this.regionValue,
|
2024-11-15 13:49:37 -05:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
}).then((response) => {
|
|
|
|
if (response.redirected) {
|
|
|
|
window.location.href = response.url;
|
|
|
|
}
|
|
|
|
});
|
2025-01-31 17:04:26 -05:00
|
|
|
};
|
2024-11-15 13:49:37 -05:00
|
|
|
|
2025-01-31 17:04:26 -05:00
|
|
|
handleExit = (err, metadata) => {
|
2025-02-12 12:59:35 -06:00
|
|
|
// If there was an error during update mode, refresh the page to show latest status
|
|
|
|
if (err && metadata.status === "requires_credentials") {
|
|
|
|
window.location.href = "/accounts";
|
|
|
|
}
|
2025-01-31 17:04:26 -05:00
|
|
|
};
|
2024-11-15 13:49:37 -05:00
|
|
|
|
2025-01-31 17:04:26 -05:00
|
|
|
handleEvent = (eventName, metadata) => {
|
2024-11-15 13:49:37 -05:00
|
|
|
// no-op
|
2025-01-31 17:04:26 -05:00
|
|
|
};
|
2024-11-15 13:49:37 -05:00
|
|
|
|
2025-01-31 17:04:26 -05:00
|
|
|
handleLoad = () => {
|
2024-11-15 13:49:37 -05:00
|
|
|
// no-op
|
2025-01-31 17:04:26 -05:00
|
|
|
};
|
2024-11-15 13:49:37 -05:00
|
|
|
}
|