Style upload form
This commit is contained in:
@@ -6,24 +6,23 @@ defmodule MihainatorWeb.UploadLive do
|
|||||||
{:ok,
|
{:ok,
|
||||||
socket
|
socket
|
||||||
|> assign(:uploaded_files, [])
|
|> assign(:uploaded_files, [])
|
||||||
|> allow_upload(:avatar, accept: ~w(.csv .jpeg))}
|
|> assign(:submit_disabled?, true)
|
||||||
|
|> allow_upload(:history, accept: ~w(.csv))}
|
||||||
end
|
end
|
||||||
|
|
||||||
@impl Phoenix.LiveView
|
@impl Phoenix.LiveView
|
||||||
def handle_event("validate", _params, socket) do
|
def handle_event("validate", _params, socket) do
|
||||||
{:noreply, socket}
|
{:noreply,
|
||||||
end
|
socket
|
||||||
|
|> assign(:submit_disabled?, false)}
|
||||||
@impl Phoenix.LiveView
|
|
||||||
def handle_event("cancel-upload", %{"ref" => ref}, socket) do
|
|
||||||
{:noreply, cancel_upload(socket, :avatar, ref)}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
@impl Phoenix.LiveView
|
@impl Phoenix.LiveView
|
||||||
def handle_event("save", _params, socket) do
|
def handle_event("save", _params, socket) do
|
||||||
uploaded_files =
|
uploaded_files =
|
||||||
consume_uploaded_entries(socket, :avatar, fn %{path: path}, _entry ->
|
consume_uploaded_entries(socket, :history, fn %{path: path}, _entry ->
|
||||||
line_count = File.stream!(path)
|
line_count =
|
||||||
|
File.stream!(path)
|
||||||
|> Enum.count()
|
|> Enum.count()
|
||||||
|
|
||||||
IO.puts(line_count)
|
IO.puts(line_count)
|
||||||
|
|||||||
@@ -1,39 +1,52 @@
|
|||||||
<form id="upload-form" phx-submit="save" phx-change="validate">
|
<div class="flex flex-col space-y-8">
|
||||||
<label class="block mb-2 text-sm font-medium text-gray-900" for={@uploads.avatar.ref}>Avatar</label>
|
<h1 class="text-5xl">Threema history checker</h1>
|
||||||
|
|
||||||
<.live_file_input upload={@uploads.avatar} class="block w-full text-sm text-gray-900 border border-gray-300 rounded-lg cursor-pointer bg-gray-50 dark:text-gray-400 focus:outline-none dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400" />
|
<p class="text-lg">Upload a Threema chat history file (.csv) and see who's more interactive!</p>
|
||||||
<button class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded" type="submit">
|
|
||||||
|
<form id="upload-form" phx-submit="save" phx-change="validate" class="flex flex-col space-y-4">
|
||||||
|
<div>
|
||||||
|
<label class="block mb-2 text-sm font-bold text-gray-900" for={@uploads.history.ref}>
|
||||||
|
CSV file
|
||||||
|
</label>
|
||||||
|
|
||||||
|
<.live_file_input
|
||||||
|
upload={@uploads.history}
|
||||||
|
class="block w-full text-sm text-gray-900 border border-gray-300 rounded-lg cursor-pointer bg-gray-50 dark:text-gray-400 focus:outline-none dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<button
|
||||||
|
:if={@submit_disabled?}
|
||||||
|
disabled={@submit_disabled?}
|
||||||
|
class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded w-1/2 place-self-end disabled:opacity-75"
|
||||||
|
type="submit"
|
||||||
|
>
|
||||||
Upload
|
Upload
|
||||||
</button>
|
</button>
|
||||||
</form>
|
|
||||||
|
|
||||||
<%!-- use phx-drop-target with the upload ref to enable file drag and drop --%>
|
<button
|
||||||
<section phx-drop-target={@uploads.avatar.ref}>
|
:if={not @submit_disabled?}
|
||||||
<%= for entry <- @uploads.avatar.entries do %>
|
class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded w-1/2 place-self-end"
|
||||||
|
type="submit"
|
||||||
|
>
|
||||||
|
Upload
|
||||||
|
</button>
|
||||||
|
</form>
|
||||||
|
|
||||||
|
<%= for entry <- @uploads.history.entries do %>
|
||||||
<article class="upload-entry">
|
<article class="upload-entry">
|
||||||
|
|
||||||
<figure>
|
|
||||||
<.live_img_preview entry={entry} />
|
|
||||||
<figcaption><%= entry.client_name %></figcaption>
|
|
||||||
</figure>
|
|
||||||
|
|
||||||
<%!-- entry.progress will update automatically for in-flight entries --%>
|
|
||||||
<progress value={entry.progress} max="100"> <%= entry.progress %>% </progress>
|
|
||||||
|
|
||||||
<%!-- a regular click event whose handler will invoke Phoenix.LiveView.cancel_upload/3 --%>
|
|
||||||
<button type="button" phx-click="cancel-upload" phx-value-ref={entry.ref} aria-label="cancel">×</button>
|
|
||||||
|
|
||||||
<%!-- Phoenix.Component.upload_errors/2 returns a list of error atoms --%>
|
<%!-- Phoenix.Component.upload_errors/2 returns a list of error atoms --%>
|
||||||
<%= for err <- upload_errors(@uploads.avatar, entry) do %>
|
<%= for err <- upload_errors(@uploads.history, entry) do %>
|
||||||
<p class="alert alert-danger"><%= error_to_string(err) %></p>
|
<p class="alert alert-danger"><%= error_to_string(err) %></p>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
||||||
</article>
|
</article>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
||||||
|
<%!-- use phx-drop-target with the upload ref to enable file drag and drop --%>
|
||||||
|
<section phx-drop-target={@uploads.history.ref}>
|
||||||
<%!-- Phoenix.Component.upload_errors/1 returns a list of error atoms --%>
|
<%!-- Phoenix.Component.upload_errors/1 returns a list of error atoms --%>
|
||||||
<%= for err <- upload_errors(@uploads.avatar) do %>
|
<%= for err <- upload_errors(@uploads.history) do %>
|
||||||
<p class="alert alert-danger"><%= error_to_string(err) %></p>
|
<p class="alert alert-danger"><%= error_to_string(err) %></p>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
</section>
|
||||||
</section>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user