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

qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
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
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
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 t...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29703 241
New
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
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
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
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
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

We're in Beta

About us Mission Statement