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,
|
|
|
|
};
|
|
|
|
|
|
|
|
open() {
|
|
|
|
const handler = Plaid.create({
|
|
|
|
token: this.linkTokenValue,
|
|
|
|
onSuccess: this.handleSuccess,
|
|
|
|
onLoad: this.handleLoad,
|
|
|
|
onExit: this.handleExit,
|
|
|
|
onEvent: this.handleEvent,
|
|
|
|
});
|
|
|
|
|
|
|
|
handler.open();
|
|
|
|
}
|
|
|
|
|
|
|
|
handleSuccess(public_token, metadata) {
|
2024-11-15 17:33:18 -05:00
|
|
|
window.location.href = "/accounts";
|
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
}).then((response) => {
|
|
|
|
if (response.redirected) {
|
|
|
|
window.location.href = response.url;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
handleExit(err, metadata) {
|
|
|
|
// no-op
|
|
|
|
}
|
|
|
|
|
|
|
|
handleEvent(eventName, metadata) {
|
|
|
|
// no-op
|
|
|
|
}
|
|
|
|
|
|
|
|
handleLoad() {
|
|
|
|
// no-op
|
|
|
|
}
|
|
|
|
}
|