benonymus
Hey there,
I have a liveview that is being live rendered inside a usual eex new template, and in this liveview I have this for stripe:
<%= if @show_stripe_credit_form do %>
<div class="field" phx-hook="Test">
<%= label f, "Credit Card", class: "label" %>
<div id="card-element" style="border: 1px solid #DFE0E0" class="b-r-4 p-2">
<!-- Elements will create input elements here -->
</div>
<!-- We'll put the error messages in this element -->
<p id="card-errors" role="alert" class="has-text-danger m-t-1">
</p>
</div>
<% end %>
I also have a separate file with the js which works on another page of the app without liveview element, but it doesn’t seem to hook into this one. Here is the js:
<script src="https://js.stripe.com/v3/"></script>
<script>
document.addEventListener('DOMContentLoaded', () => {
if (!document.getElementById('card-element')) {
return
}
var stripe = Stripe('<%= System.get_env("STRIPE_PUBLISHABLE_KEY") %>');
var elements = stripe.elements({
fonts: [
{
cssSrc: 'https://use.typekit.net/gjk6uuk.css',
},
],
});
var style = {
base: {
color: "#32325d",
fontFamily: 'sofia-pro, avenir next, avenir, BlinkMacSystemFont, -apple-system, "Segoe UI", "Roboto", sans-serif',
},
invalid: {
color: "#FF495C",
iconColor: "#fa755a"
}
};
var card = elements.create("card", { style: style });
card.mount("#card-element");
card.addEventListener('change', function(event) {
var displayError = document.getElementById('card-errors');
if (event.error) {
displayError.textContent = event.error.message;
} else {
displayError.textContent = '';
}
});
let form = document.getElementById('ticket-order-form');
console.log(form)
let submitButton = document.getElementById('submit-button');
form.addEventListener('submit', (event) => {
event.preventDefault()
submitButton.disable = true;
stripe.createToken(card).then(function(result) {
if (result.error) {
// show error
errorElement = document.getElementById('card-errors')
errorElement.textContent = result.error.message
submitButton.disable = false;
} else {
// add stripeToken to form and submit
stripeTokenHandler(result.token)
}
});
});
function stripeTokenHandler(token) {
console.log(token)
hiddenInput = document.createElement('input')
hiddenInput.setAttribute('type', 'hidden')
hiddenInput.setAttribute('name', 'stripeToken')
hiddenInput.setAttribute('value', token.id)
form.appendChild(hiddenInput)
form.submit()
}
});
</script>
My question is where do I need to add this in order to be applied in the liveview every time the liveview changes, because I hide the field based on changes on the page?
Thanks!
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 7- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
sfusato
When you “live navigate” that piece of JS won’t be run again. For liveview pages, you could listen on this event:
phx:page-loading-stop.benonymus
Thanks, by any chance could you give a bit more practical direction, or example?
sfusato
Add another event listener like the one you have for
DOMContentLoaded, but forphx:page-loading-stopand on thewindowobject.See if this gets you closer to your objective. Then, since you say you need to react every time the liveview changes, you could properly hook it up via a liveview hook.
benonymus
Doesn’t seem to make any difference unfortunately, in general it seems like the js has no effect, it doesn’t change the div visually.
sfusato
How are you adding the JS to the page?
In the code snippet above you have two
<script>tags. Are you rendering that within the template itself?winescout
I had a similar issue getting some Bootstrap JS to register if they were injected into the page. With that page-loading-stop hook there, you still need to get all your markup loaded on the page before it fires. In the case that started this thread, that “<%= if … %>” is probably causing that code to render after some handle_event, and after the page-loading-stop callback.
In some cases, I’m able to use “display: none” and “display: block” instead so the code is loaded before the callback, but that doesn’t work in all cases unfortunately.
Dabsy
I encountered this because I had the same issue yesterday. I’m rendering multiple payment sources via LiveView and needed my scripts to execute every update else widgets would not be rendered on change or the scripts for Stripe would not run. None of the phx emitted event listeners are a valid solution to this.
My solution was to put a
phx-hook="ChangePaymentOptionon the container div of each of the payment options with the hook utilising the mounted function. I didn’t think this was viable at first until I realised I could push new Hooks directly onto the liveSocket: