lonamiaec

lonamiaec

LiveComponent inside a modal closing modal when receiving any event

Hi all!

I have a LiveComponent living inside a modal. This component lists some items and let me add a new one with a form.
I render the LiveComponent in this way

<%= live_component @socket, TestProjectWeb.ProceedingLive.TaskComponent, id: "tasks-#{@proceeding.id}", proceeding_id: @proceeding.id %>

Here is my component template (is not complete, but I have problems with both the button and the form)

<button class="btn btn-warning btn-fab btn-round" data-toggle="modal" data-target="#exampleModal" >
    <i class="material-icons">list</i>
</button>

<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Tareas</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <div class="d-flex flex-row">
            <%= if not @show_add_task do %>
              <div class="p-2">
                  <button class="btn btn-info btn-round" phx-click="show_add_task" phx-target="<%= @myself %>">
                  Añadir <i class="material-icons">add</i>
                  </button>
              </div>
            <% end %>
        </div>
        <div class="row">
          <div class="col-md-12 col-lg-12">
              <%= f = form_for @changeset, "#", id: "task-form", phx_submit: "add_task", phx_target: @myself %>
                <div class="form-row">
                  <div class="col-md-9 col-lg-9">
                    <div class="form-group">
                      <label class="control-label" for="task_description">Descripción</label>
                      <%= text_input f, :description, class: "form-control"%>
                    </div>
                  </div>
                  <div class="col-md-3 col-lg-3">
                    <div class="form-group">
                      <label class="control-label" for="task_deadline">Fecha límite</label>
                      <%= date_input f, :deadline, class: "form-control", rows: 1 %>
                    </div>
                  </div>
                </div>
                <div class="form-row">
                  <div class="form-group">
                    <%= submit "Guardar", class: "btn btn-primary btn-round", phx_disable_with: "Guardando..." %>
                  </div>
                </div>
              </form>
          </div>
        </div>

The problem is if I click either the submit button or the add button my modal disappears. I’ve added some logs on the way, and events arrive at the expected handle_event functions, but I’m not sure if I’m messing the state at some point so my template can not be rendered.
Here is my LiveComponent code

defmodule TestProjectWeb.ProceedingLive.TaskComponent do
  use TestProjectWeb, :live_component

  alias TestProject.Accounts
  alias TestProject.Proceedings
  alias TestProject.Proceedings.Task

  @impl true
  def update(assigns, socket) do
    socket = socket |> assign(assigns) |> assign(:tasks, Proceedings.get_tasks_by_proceeding(assigns.proceeding_id))
    {:ok, socket}
  end

  @impl true
  def mount(socket) do
    changeset = Proceedings.change_task(%Task{})
    socket = assign(
      socket,
      show_add_task: false,
      tasks: [],
      changeset: changeset
    )
    {:ok, socket}
  end

  @impl true
  def handle_event("show_add_task", value, socket) do
    :timer.sleep(4000)
    socket = assign(socket, show_add_task: true)
    {:noreply, socket}
  end

  @impl true
  def handle_event("add_task", %{"task" => task} = _params, socket) do
    user = Accounts.get_user_by_session_token(socket.assigns.user_token)
    attrs = task
      |> Map.put_new("user_id", user.id)
      |> Map.put_new("proceeding_id", socket.assigns.proceeding_id)

    case Proceedings.create_task(attrs) do
      {:ok, _client} ->
        socket = assign(
          socket,
          show_add_task: false,
          tasks: [],
          changeset: Proceedings.change_task(%Task{}))
        socket = assign(socket, show_add_task: false)
        {:noreply, socket}

      {:error, %Ecto.Changeset{} = changeset} ->
        socket = assign(
          socket,
          show_note_form: false,
          note_changeset: changeset)
        {:noreply, socket}
    end
  end
end

This is the template rendered


And this is what I can see after sending the event to the LiveComponent’s process, the modal has just been closed/disappeared.

I added a :time.sleep before {:noreply at both events and the modal is open until :noreply arrives. I`m not sure If I’ve misunderstood something related to state management on LiveComponents, my state is corrupted or anything else

Thanks in advance!

First Post!

andreaseriksson

andreaseriksson

I think its easiest to use js and wait until after the animation is done, send the event to the server.

Have an example here (with alpine):

Where Next?

Popular in Questions Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
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
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
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
yawaramin
In the Dialyzer docs ( dialyzer — OTP 29.0.2 (dialyzer 6.0.1) ), there is a way to turn off a specific warning for a function: -dialyzer...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New

Other popular topics Top

Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
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
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
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
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 127536 1222
New

We're in Beta

About us Mission Statement