defmodule MmautoWeb.PartLive.FormComponent do
use MmautoWeb, :live_component
alias Mmauto.Accounts
@impl true
def render(assigns) do
~H"""
<div>
<.header>
<%= @title %>
<:subtitle>Use this form to manage part records in your database.</:subtitle>
</.header>
<.simple_form
for={@form}
id="part-form"
phx-target={@myself}
phx-change="validate"
phx-submit="save"
multipart
>
<.input field={@form[:name]} type="text" label="Name" />
<.input field={@form[:dec]} type="text" label="Made in" />
<!-- <.input field={@form[:image]} type="file" label="image" /> THIS NOT WORKING -->
<.input field={@form[:price]} type="number" label="Price" />
<.input field={@form[:brand]} type="text" label="Brand" />
<.input field={@form[:model]} type="text" label="Model" />
<.input field={@form[:pnum]} type="number" label="Part Number" />
<.input field={@form[:instock]} type="checkbox" label="Instock" />
<.input field={@form[:stock1]} type="number" label="Stock N1" />
<.input field={@form[:stock2]} class="mx-2" type="number" label="Stock N2" />
<.input field={@form[:stock3]} type="number" label="Stock N3" />
<.input field={@form[:id]} type="text" label="Id" />
<:actions>
<.button phx-disable-with="Saving...">Save Part</.button>
</:actions>
</.simple_form>
<!-- same here -->
<form id="upload-form" phx-submit="savee" phx-change="vali">
<div class="container">
<.live_file_input upload={@uploads.image} />
</div>
<button type="submit">Upload</button>
</form>
</div>
"""
end
@impl true
def mount(_params, _session, socket) do
{:ok, socket
|> assign(:upload_file, nil)
|> allow_upload(:image, accept: ~w(.jpg .jpeg .png), max_entries: 1)
}
end
@impl true
def update(%{part: part} = assigns, socket) do
changeset = Accounts.change_part(part)
{:ok,
socket
|> assign(assigns)
|> assign_form(changeset)
|> allow_upload(:image, accept: ~w(.jpg .jpeg .png), max_entries: 1)}
end
@impl true
def handle_event("validate", params, socket) do
part_params = params["part"]
IO.inspect socket.assigns
changeset =
socket.assigns.part
|> Accounts.change_part(part_params)
|> Map.put(:action, :validate)
{:noreply, assign_form(socket, changeset)}
end
@impl true
def handle_event("vali", %{"image" => image}, socket) do
IO.inspect image
changeset =
socket
|> assign(:id, nil)
{:noreply, assign_form(socket, changeset)}
end
@impl true
def handle_event("vali", %{"image" => image}, socket) do
IO.inspect image
IO.inspect socket.assigns
changeset =
socket
|> assign(:id, nil)
{:noreply, assign_form(socket, changeset)}
end
def handle_event("save", %{"part" => part_params}, socket) do
IO.inspect part_params
save_part(socket, socket.assigns.action, part_params)
end
def handle_event("savee", param, socket) do
IO.inspect param
IO.inspect socket.assigns
{:noreply, socket}
end
defp save_part(socket, :edit, part_params) do
case Accounts.update_part(socket.assigns.part, part_params) do
{:ok, part} ->
notify_parent({:saved, part})
{:noreply,
socket
|> put_flash(:info, "Part updated successfully")
|> push_patch(to: socket.assigns.patch)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign_form(socket, changeset)}
end
end
defp save_part(socket, :new, part_params) do
IO.puts "New"
IO.inspect part_params
IO.puts "hello"
IO.inspect socket.assigns.uploads.image
case Accounts.create_part(part_params) do
{:ok, part} ->
notify_parent({:saved, part})
{:noreply,
socket
|> put_flash(:info, "Part created successfully")
|> push_patch(to: socket.assigns.patch)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign_form(socket, changeset)}
end
end
defp assign_form(socket, %Ecto.Changeset{} = changeset) do
assign(socket, :form, to_form(changeset))
end
defp notify_parent(msg), do: send(self(), {__MODULE__, msg})
end