Split logic in multiple components

This commit is contained in:
2024-10-17 23:02:08 +02:00
parent e3093c99c7
commit ad12768ccf
8 changed files with 122 additions and 83 deletions

View File

@@ -0,0 +1,34 @@
defmodule MihainatorWeb.FileInputComponent do
@moduledoc false
use Phoenix.LiveComponent
@impl true
def mount(socket) do
{:ok,
socket
|> assign(:submit_disabled?, true)
|> allow_upload(:history, accept: ~w(.csv))}
end
@impl true
def handle_event("validate", _params, socket) do
{:noreply,
socket
|> assign(:submit_disabled?, false)}
end
@impl true
def handle_event("save", _params, socket) do
consume_uploaded_entries(socket, :history, fn %{path: path}, _entry ->
Task.async(fn -> Mihainator.Extractor.extract(path) end)
{:ok, nil}
end)
{:noreply, socket}
end
defp error_to_string(:too_large), do: "Too large"
defp error_to_string(:not_accepted), do: "You have selected an unacceptable file type"
end