Split logic in multiple components
This commit is contained in:
34
lib/mihainator_web/live/file_input_component.ex
Normal file
34
lib/mihainator_web/live/file_input_component.ex
Normal 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
|
||||
Reference in New Issue
Block a user