39 lines
984 B
Elixir
39 lines
984 B
Elixir
defmodule MihainatorWeb.UploadLive do
|
|
use MihainatorWeb, :live_view
|
|
|
|
@impl Phoenix.LiveView
|
|
def mount(_params, _session, socket) do
|
|
{:ok,
|
|
socket
|
|
|> assign(:uploaded_files, [])
|
|
|> assign(:submit_disabled?, true)
|
|
|> allow_upload(:history, accept: ~w(.csv))}
|
|
end
|
|
|
|
@impl Phoenix.LiveView
|
|
def handle_event("validate", _params, socket) do
|
|
{:noreply,
|
|
socket
|
|
|> assign(:submit_disabled?, false)}
|
|
end
|
|
|
|
@impl Phoenix.LiveView
|
|
def handle_event("save", _params, socket) do
|
|
uploaded_files =
|
|
consume_uploaded_entries(socket, :history, fn %{path: path}, _entry ->
|
|
line_count =
|
|
File.stream!(path)
|
|
|> Enum.count()
|
|
|
|
IO.puts(line_count)
|
|
|
|
{:ok, line_count}
|
|
end)
|
|
|
|
{:noreply, update(socket, :uploaded_files, &(&1 ++ uploaded_files))}
|
|
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
|