chrisdel101

chrisdel101

How to close modal in event handler liveview

A few hours of work as not made it happen :frowning:. I need approach 1 (or similar) to work.

Note: I’m using the default LiveHelpers modal code.1

Approach 1

The modal should close on “save okay” only, so is handled after save in the handler. For now handle_event just tries to close. hide_modal runs but does not close the modal.

  def handle_event("save", %{"user" => user_params}, socket) do
    IO.inspect(user_params) # this runs
    save_user(socket, socket.assigns.action, user_params)
    MyApp.LiveHelpers.hide_modal() #default location of modal
    {:noreply, socket}
  end

# form where the save button is
  <.form
    let={f}
    for={@changeset}
    id="user-form"
    phx-target={@myself}
    phx-change="validate"
    phx-submit="save">
  
    <div>
      <%= submit "Save", phx_disable_with: "Saving..."%>
    </div>
  </.form>

# modal close function comes w/ Phoenix, and works elsewhere
  def hide_modal(js \\ %JS{}) do
    IO.puts("fired")
    js
    |> JS.hide(to: "#modal", transition: "fade-out")
    |> JS.hide(to: "#modal-content", transition: "fade-out-scale")
  end

IO.puts("fired") runs but the modal does not close.

Approach 2

Adding to the button does work but it closes on any click, even w/ a blank form, or w/ errors. This would work for a cancel button but not for save.

 <%= submit "Save", phx_disable_with: "Saving...", phx_click: MyApp.LiveHelpers.hide_modal()%>
  

I guess it’s something to do w/ the binding phx- events directly to the button render, or lack thereof. Since it’s conditional to “save okay” this blunt click binding will not suffice.

Most Liked

andreaseriksson

andreaseriksson

If you navigate away, or patch the url so there is another live_action, this should happen automatically.

<.modal
  :if={@live_action in [:new, :edit]}
  id="task-modal"
  show
  on_cancel={JS.navigate(~p"/tasks")}
>
  <:title><%= @page_title %></:title>
  <.live_component
    module={MyAppWeb.TasksLive.FormComponent}
    id={@task.id || :new}
    title={@page_title}
    action={@live_action}
    task={@task}
    navigate={~p"/tasks"}
  />
</.modal>

But If you dont change url, or live_action,you can update it so instead of:

:if={@live_action in [:new, :edit]}

you can use set a show_modal in assigns and let that control it.

:if={@show_modal}

Otherwise, I usually go for a variant described here:

Where Next?

Popular in Questions Top

Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 42920 311
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30877 112
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43622 214
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New

We're in Beta

About us Mission Statement