jacknorman

jacknorman

Parent form state reset when child live component is rendered on button click

Hello,

I have a very simple code setup with a Parent Live View and a child Live Component. They both have a form with some data.

The parent contains 2 fields which is a date and a text field in the form. Outside the form is a button which loads a child live component below the button on a button click.

The child live component has a form with with mainly text fields and a time-input field.

When submitted the data goes to the correct handle events of the parent as well as the child.

I have 2 questions:

  1. when i click on the button to render the child live component the data entered in the parent live view form gets reset and turns blank, how can i avoid this?
  2. Is there any way i can close (not show) the child live component when the form is submitted for the child component?

I am adding the code below.

P.S. I am a beginner and very recently started coding in elixir.


The Live View and its template

defmodule FormsTutorialWeb.PageLive do
  use FormsTutorialWeb, :live_view

  def mount(_params, _session, socket) do
    {:ok, assign(socket, event_type: "")}
  end

  @spec handle_event(<<_::56>>, any, any) :: {:noreply, any}
  def handle_event("clicked", params, socket) do
    IO.puts "handle event of parent"
    IO.inspect(params, label: "params ---> ")
    {:noreply, socket}
  end

  def handle_params(_params, _uri, socket) do
    {:noreply, socket}
  end

  def handle_event("show_interstitial", params, socket) do
    IO.puts "inside handle event show interstitial"
    IO.inspect(params, label: "params ---> ")
    {:noreply, assign(socket, event_type: "Show")}
  end
end


<%= f = form_for :new_day, "#", [phx_submit: :clicked] %>
  <label>Create new day</label>
  <%= date_input f, :date%>

  <label class="form-control-label" for="url">Media URL</label>
  <%= text_input f, :url%>

  <div>
    <%= submit "Done", phx_disable_with: "Setting..." %>
  </div>
</form>

<div phx-click="show_interstitial">
    <a>Show interstitial</a>
</div>



<%= if @event_type == "Show" do %>
  <%= live_component(
            @socket,
            FormsTutorialWeb.PageComponent,
            id: "1",
            number: 1
          )
  %>
<% end %>

The child live component and its template

defmodule FormsTutorialWeb.PageComponent do
  use Phoenix.LiveComponent
  use Phoenix.HTML
  alias FormsTutorial.User
  alias FormsTutorial.Pictures.Picture
  alias FormsTutorial.Pictures

  def mount(socket) do
    changeset = Picture.changeset(%Picture{})
    {:ok, assign(socket, changeset: changeset)}
  end

  # def update(%{id: id, number: number}, socket) do
  #   {:ok,
  #    assign(socket,
  #      id: id,
  #      number: number
  #    )}
  # end

  def handle_event("clicked", params, socket) do
    IO.puts "handle event of child component"
    IO.inspect(params, label: "params ---> ")
    {:noreply, socket}
  end
end

<%= f = form_for :heading, "#", [phx_submit: :clicked, phx_target: @myself] %>
    <p class="pl-2">New Show Interstitial</p>

    <label>Name</label><br>
    <%= text_input f, :name %>

    <label>Age</label><br>
    <%= number_input f, :age %>

    <div>
        <%= submit "DONE"%>
    </div>
</form>

Where Next?

Popular in Questions Top

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
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
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
New
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
Kagamiiiii
Student &amp; New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New

Other popular topics Top

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
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

We're in Beta

About us Mission Statement