eddyraz
How to use hooks with data generated from the server
good Afternoon, I have a live_view component with a input hidden field “attached” to a phx-hook
1–in the template:
<form id="login_form" phx-submit="check_login_credentials">
<input type="hidden" id="login_token" phx-hook="input_login_token"/>
.
.
.
</form>
2–in the server:
there’s a function handle_event/3 that processes the username and password and send it to an external Auth API in Django using djoser REST API. and receives a token.
def handle_event("check_login_credentials", params, socket) do
login_tkn="823703847509432870598743205987" #test dummy data
if login_tkn do
{:noreply, push_event(socket, "saveLoginToken", %{login_token : login_tkn}
.
.
.
end
end
3—in the hooks file app.js theres a hook declared who manipulates the data sent from the server
let hooks = {}
hooks.saveLogintoken ={
mounted() {
localStorage.clear()
this.handleEvent("saveLoginToken", ({login_tkn}) => localStorage.setItem("login_token", login_tkn)
},
};
When I put a console.log(something) in after the mounted() section it prints in the console, but any code put in this.handleEvent() section is not executed, If i take the other aproach and use
this.el.addEventListener("input", e => {....} it works but not handleEvent
could anybody explain why is not working, thanks in advanced.
Marked As Solved
mcrumm
The problem is in send_token_to_browser:
Note that push_event returns a modified socket struct- the event is not pushed to the client until you return from the LiveView callback. You are not receiving the event on the client because you are not returning the socket struct containing the event.
Also Liked
imkleats
There’s one other typo that could be problematic if it’s not just erroneous transcription.
The event payload in the Elixir side has a key of login_token, but the object destructuring on the JS side’s handleEvent call is using the key login_tkn which would be undefined based on the payload coming across the socket.
Terbium-135
If you are using push_event:
Phoenix Liveview adds the string phx: in front of the event name.
So "saveLoginToken" becomes "phx:saveLoginToken"
Have a look at
with the topic: Handling server-pushed events
let liveSocket = new LiveSocket(...)
window.addEventListener(`phx:highlight`, (e) => {
let el = document.getElementById(e.detail.id)
if(el) {
// logic for highlighting
}
})
Not sure if this is the reason for your problem
eddyraz
Thanks so much,
I put it this way, and it worked:
@impl true
def handle_event("check_login_credentials", params, socket) do
login_token = process_token_request(params["user_name"], params["password"], socket)
send_token_to_browser(socket, params, login_token)
end
@impl true
def send_token_to_browser(socket, params, login_token) do
if login_token do
socket = socket |> assign(:is_authenticated, true)
{:noreply, push_event(socket, "save_login_token", %{payload: login_token})}
else
{:noreply, assign(socket, params: params)}
end
end
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








