Fix file upload

This commit is contained in:
Alex Lion
2026-08-04 14:24:15 +07:00
parent 15901e7cd9
commit ab85ac7346
3 changed files with 53 additions and 3 deletions

View File

@@ -47,7 +47,10 @@ defmodule ClaperWeb.EventLive.EventFormComponent do
@impl true
def handle_event("save", %{"event" => event_params}, socket) do
save_event(socket, socket.assigns.action, event_params)
case uploaded_entries(socket, :presentation_file) do
{_, []} -> save_event(socket, socket.assigns.action, event_params)
_ -> {:noreply, socket}
end
end
@impl true

View File

@@ -1,7 +1,7 @@
<div id="wrapper" class={if @container == :drawer, do: "flex h-full flex-col", else: ""}>
<% upload_entry = Enum.at(@uploads.presentation_file.entries, 0) %>
<% upload_progress = if upload_entry, do: upload_entry.progress, else: 0 %>
<% can_submit = upload_progress in [0, 100] && @changeset.valid? %>
<% can_submit = (is_nil(upload_entry) || upload_progress == 100) && @changeset.valid? %>
<% presentation_file = Map.get(@event, :presentation_file) %>
<% presentation_attached =
not is_nil(presentation_file) && Map.get(presentation_file, :length, 0) > 0 &&
@@ -192,13 +192,14 @@
>
{gettext("Choose file")}
</label>
<.live_file_input upload={@uploads.presentation_file} class="sr-only" />
<p class="mt-3 text-xs text-base-content/50">
{gettext("PDF, PPT, or PPTX up to %{size} MB", size: @max_file_size)}
</p>
</div>
<% end %>
<.live_file_input upload={@uploads.presentation_file} class="sr-only" />
<%= if upload_entry do %>
<%= for err <- upload_errors(@uploads.presentation_file, upload_entry) do %>
<div class="alert alert-error mt-3 py-2 text-sm" role="alert">

View File

@@ -114,6 +114,52 @@ defmodule ClaperWeb.EventLiveTest do
assert has_element?(index_live, ~s(button[form="event-form"][disabled]))
end
test "keeps the upload active and disables save while a presentation is starting", %{
conn: conn
} do
{:ok, new_live, _html} = live(conn, ~p"/events/new")
new_live
|> form("#event-form", event: %{name: "New event"})
|> render_change()
upload =
file_input(new_live, "#file-form", :presentation_file, [
%{name: "slides.pdf", content: "%PDF-1.4", type: "application/pdf"}
])
assert render_upload(upload, "slides.pdf", 1) =~ "Uploading... 1%"
assert has_element?(new_live, ~s(#file-form input[type="file"]))
assert has_element?(new_live, ~s(button[form="event-form"][disabled]))
assert render_upload(upload, "slides.pdf", 99) =~ "New presentation ready"
assert has_element?(new_live, ~s|button[form="event-form"]:not([disabled])|)
end
test "does not create an event while a presentation upload is pending", %{conn: conn} do
{:ok, new_live, _html} = live(conn, ~p"/events/new")
event_count = Claper.Repo.aggregate(Claper.Events.Event, :count)
new_live
|> form("#event-form", event: %{name: "New event"})
|> render_change()
upload =
file_input(new_live, "#file-form", :presentation_file, [
%{name: "slides.pdf", content: "%PDF-1.4", type: "application/pdf"}
])
assert {:ok, _metadata} = preflight_upload(upload)
html =
new_live
|> form("#event-form", event: %{name: "New event"})
|> render_submit()
assert html =~ "Uploading... 0%"
assert Claper.Repo.aggregate(Claper.Events.Event, :count) == event_count
end
test "deletes event in listing", %{conn: conn, presentation_file: presentation_file} do
{:ok, index_live, _html} = live(conn, ~p"/events/#{presentation_file.event.uuid}/edit")