rakesh-gupta29

rakesh-gupta29

Toggling the form state makes fields disappear

Form works fine when user is in edit mode.

When the mode is toggled, and the user starts typing, form fields disappear.

defmodule PortalWeb.ClientLive.ProfileLive do
  @moduledoc """
  profile page for clients.
  they can set up their profile along with
  """
  alias Portal.Clients
  import PortalWeb.UI.Button

  use PortalWeb, :live_view_client

  def mount(_params, _session, socket) do
    client = socket.assigns.current_client

    profile_basics_form =
      Clients.update_profile_basics_changeset(client) |> to_form(as: "profile-basics")

    {:ok,
     socket
     |> assign(:trigger_submit, false)
     |> assign(:view_mode, false)
     |> assign(:profile_basics_form, profile_basics_form)}
  end

  def handle_event("toggle_form_mode", _params, socket) do
    mode = socket.assigns.view_mode
    {:noreply, assign(socket, :view_mode, !mode)}
  end

  defp card(assigns) do
    ~H"""
    <article class="grid gap-1">
      <span class="text-base font-normal text-neutral-800"><%= @title %></span>
      <span class="text-lg font-medium text-black">
        <%= if @value == "", do: "-", else: @value %>
      </span>
    </article>
    """
  end

  def handle_event("validate_profile_basics", %{"profile-basics" => params}, socket) do
    client = socket.assigns.current_client

    # Return the updated socket with the updated form and validation action
    {:noreply,
     socket
     |> assign(
       :profile_basics_form,
       client
       |> Clients.update_profile_basics_changeset(params)
       |> to_form(as: "profile-basics")
     )}
  end

  def handle_event("update_profile_basics", %{"profile-basics" => params}, socket) do
    client = socket.assigns.current_client

    case Clients.update_profile_basics(client, params) do
      {:ok, _client} ->
        {:noreply, socket |> put_flash(:info, "profile has been updated")}

      {:error, %Ecto.Changeset{} = changeset} ->
        {:noreply,
         assign(socket,
           profile_basics_form: Clients.update_profile_basics_changeset(changeset) |> to_form()
         )}
    end
  end

  def render(assigns) do
    ~H"""
    <div class=" bg-brand/5 pt-20 pb-10">
      <div class="w-container grid grid-cols-3">
        <div class="col-span-1">
          <div class="sticky top-0 grid gap-2 pt-6">
            <span class="text-2xl font-semibold text-brand">Profile details</span>
            <span>Basic details regarding your profile and company</span>
          </div>
        </div>
        <div class="col-span-2 border-1 border-solid bg-white border-brand/10 rounded-xl overflow-hidden">
          <div class="flex  items-center justify-between gap-3 p-4 bg-brand/10">
            <span class="text-xl font-medium text-brand">Basics</span>
            <button
              type="button"
              phx-click="toggle_form_mode"
              class="hover:bg-brand/10 transition-all duration-150 ease-in-out p-1 h-8 w-8 grid place-content-center rounded-full "
            >
              <%= if @view_mode do %>
                <svg
                  xmlns="http://www.w3.org/2000/svg"
                  fill="none"
                  viewBox="0 0 24 24"
                  stroke-width="1.5"
                  stroke="currentColor"
                  class="size-4"
                >
                  <path
                    stroke-linecap="round"
                    stroke-linejoin="round"
                    d="m16.862 4.487 1.687-1.688a1.875 1.875 0 1 1 2.652 2.652L6.832 19.82a4.5 4.5 0 0 1-1.897 1.13l-2.685.8.8-2.685a4.5 4.5 0 0 1 1.13-1.897L16.863 4.487Zm0 0L19.5 7.125"
                  />
                </svg>
              <% else %>
                <svg
                  xmlns="http://www.w3.org/2000/svg"
                  fill="none"
                  viewBox="0 0 24 24"
                  stroke-width="1.5"
                  stroke="currentColor"
                  class="size-4"
                >
                  <path stroke-linecap="round" stroke-linejoin="round" d="M6 18 18 6M6 6l12 12" />
                </svg>
              <% end %>
            </button>
          </div>
          <%= if @view_mode do %>
            <div class="p-6  rounded-b-md">
              <div class="grid grid-cols-2 gap-4 pb-4">
                <%= card(%{title: "Company's name", value: @profile_basics_form[:name].value}) %>
                <%= card(%{title: "Founded in", value: @profile_basics_form[:founded_year].value}) %>
                <%= card(%{title: "Tagline", value: @profile_basics_form[:tagline].value}) %>
                <%= card(%{title: "Website", value: @profile_basics_form[:website].value}) %>
              </div>
              <%= card(%{title: "Description", value: @profile_basics_form[:description].value}) %>
            </div>
          <% else %>
            <div class="p-6 rounded-b-md">
              <.simple_form
                novalidate
                for={@profile_basics_form}
                id="profile_basics_form"
                phx-submit="update_profile_basics"
                phx-change="validate_profile_basics"
                phx-trigger-action={false}
              >
                <.input field={@profile_basics_form[:name]} type="text" label="Company name" required />
                <div class="grid gap-6 md:grid-cols-2">
                  <.input
                    field={@profile_basics_form[:website]}
                    type="text"
                    label="Website"
                    required
                    placeholder="https://example.com"
                  />
                  <.input
                    field={@profile_basics_form[:founded_year]}
                    type="number"
                    placeholder="19XX"
                    label="Year founded"
                    required
                  />
                </div>
                <.input
                  field={@profile_basics_form[:tagline]}
                  placeholder="enter company's tagline"
                  type="text"
                  label="Company tagline"
                  required
                />
                <div>
                  <.input
                    rows="7"
                    type="textarea"
                    field={@profile_basics_form[:description]}
                    placeholder="descripe about your company"
                    label="Description"
                  />
                </div>

                <:actions>
                  <div class="w-full grid place-content-end">
                    <.button phx-disable-with="Updating...">Update profile</.button>
                  </div>
                </:actions>
              </.simple_form>
            </div>
          <% end %>
        </div>
      </div>
    </div>
    """
  end
end

Where Next?

Popular in Questions Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
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
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
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

Other popular topics Top

SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
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
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
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
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
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
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 42633 214
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New

We're in Beta

About us Mission Statement