Files
mihainator/lib/mihainator_web/live/file_input_component.ex

35 lines
791 B
Elixir

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