<%= error_to_string(err) %>
+ <% end %> + +diff --git a/lib/mihainator_web/live/upload_live.ex b/lib/mihainator_web/live/upload_live.ex new file mode 100644 index 0000000..ef66271 --- /dev/null +++ b/lib/mihainator_web/live/upload_live.ex @@ -0,0 +1,39 @@ +defmodule MihainatorWeb.UploadLive do + use MihainatorWeb, :live_view + + @impl Phoenix.LiveView + def mount(_params, _session, socket) do + {:ok, + socket + |> assign(:uploaded_files, []) + |> allow_upload(:avatar, accept: ~w(.csv .jpeg))} + end + + @impl Phoenix.LiveView + def handle_event("validate", _params, socket) do + {:noreply, socket} + end + + @impl Phoenix.LiveView + def handle_event("cancel-upload", %{"ref" => ref}, socket) do + {:noreply, cancel_upload(socket, :avatar, ref)} + end + + @impl Phoenix.LiveView + def handle_event("save", _params, socket) do + uploaded_files = + consume_uploaded_entries(socket, :avatar, 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 diff --git a/lib/mihainator_web/live/upload_live.html.heex b/lib/mihainator_web/live/upload_live.html.heex new file mode 100644 index 0000000..86a3dcd --- /dev/null +++ b/lib/mihainator_web/live/upload_live.html.heex @@ -0,0 +1,39 @@ +
+ +<%!-- use phx-drop-target with the upload ref to enable file drag and drop --%> +<%= error_to_string(err) %>
+ <% end %> + +<%= error_to_string(err) %>
+ <% end %> + +