Format files

This commit is contained in:
Alex
2023-02-26 23:15:55 +01:00
parent 2e8c6c847e
commit 796520432c
8 changed files with 104 additions and 70 deletions

View File

@@ -59,8 +59,7 @@ defmodule Claper.Forms do
"""
def get_form!(id, preload \\ []),
do:
Repo.get!(Form, id) |> Repo.preload(preload)
do: Repo.get!(Form, id) |> Repo.preload(preload)
@doc """
Gets a single form for a given position.
@@ -177,8 +176,7 @@ defmodule Claper.Forms do
def disable_all(presentation_file_id, position) do
from(f in Form,
where:
f.presentation_file_id == ^presentation_file_id and f.position == ^position
where: f.presentation_file_id == ^presentation_file_id and f.position == ^position
)
|> Repo.update_all(set: [enabled: false])
end
@@ -240,7 +238,8 @@ defmodule Claper.Forms do
"""
def list_form_submits(presentation_file_id) do
from(fs in FormSubmit,
join: f in Form, on: f.id == fs.form_id,
join: f in Form,
on: f.id == fs.form_id,
where: f.presentation_file_id == ^presentation_file_id
)
|> Repo.all()
@@ -257,6 +256,7 @@ defmodule Claper.Forms do
"""
def get_form_submit(user_id, form_id) when is_number(user_id),
do: Repo.get_by(FormSubmit, form_id: form_id, user_id: user_id)
def get_form_submit(attendee_identifier, form_id),
do: Repo.get_by(FormSubmit, form_id: form_id, attendee_identifier: attendee_identifier)
@@ -274,7 +274,8 @@ defmodule Claper.Forms do
** (Ecto.NoResultsError)
"""
def get_form_submit_by_id!(id, preload \\ []), do: Repo.get_by!(FormSubmit, id: id) |> Repo.preload(preload)
def get_form_submit_by_id!(id, preload \\ []),
do: Repo.get_by!(FormSubmit, id: id) |> Repo.preload(preload)
@doc """
Creates or update a FormSubmit.
@@ -288,27 +289,35 @@ defmodule Claper.Forms do
{:error, %Ecto.Changeset{}}
"""
def create_or_update_form_submit(event_uuid, %{"user_id" => user_id, "form_id" => form_id} = attrs) when is_number(user_id) do
def create_or_update_form_submit(
event_uuid,
%{"user_id" => user_id, "form_id" => form_id} = attrs
)
when is_number(user_id) do
get_form_submit(user_id, form_id) |> create_or_update_form_submit(event_uuid, attrs)
end
def create_or_update_form_submit(event_uuid, %{"attendee_identifier" => attendee_identifier, "form_id" => form_id} = attrs) do
get_form_submit(attendee_identifier, form_id) |> create_or_update_form_submit(event_uuid, attrs)
def create_or_update_form_submit(
event_uuid,
%{"attendee_identifier" => attendee_identifier, "form_id" => form_id} = attrs
) do
get_form_submit(attendee_identifier, form_id)
|> create_or_update_form_submit(event_uuid, attrs)
end
def create_or_update_form_submit(fs, event_uuid, attrs) do
case fs do
nil -> %FormSubmit{}
nil -> %FormSubmit{}
form_submit -> form_submit
end
|> FormSubmit.changeset(attrs)
|> Repo.insert_or_update()
|> case do
{:ok, r} -> case fs do
nil -> broadcast({:ok, r, event_uuid}, :form_submit_created)
_form_submit -> broadcast({:ok, r, event_uuid}, :form_submit_updated)
end
{:ok, r} ->
case fs do
nil -> broadcast({:ok, r, event_uuid}, :form_submit_created)
_form_submit -> broadcast({:ok, r, event_uuid}, :form_submit_updated)
end
end
end

View File

@@ -279,8 +279,7 @@ defmodule Claper.Polls do
def disable_all(presentation_file_id, position) do
from(p in Poll,
where:
p.presentation_file_id == ^presentation_file_id and p.position == ^position
where: p.presentation_file_id == ^presentation_file_id and p.position == ^position
)
|> Repo.update_all(set: [enabled: false])
end

View File

@@ -66,38 +66,47 @@ defmodule ClaperWeb.EventLive.FormComponent do
end
@impl true
def handle_event("submit", %{"form_submit" => params}, %{assigns: %{current_user: current_user}} = socket) when is_map(current_user) do
def handle_event(
"submit",
%{"form_submit" => params},
%{assigns: %{current_user: current_user}} = socket
)
when is_map(current_user) do
case Claper.Forms.create_or_update_form_submit(
socket.assigns.event.uuid,
%{"response" => params }
|> Map.put("user_id", socket.assigns.current_user.id)
|> Map.put("form_id", socket.assigns.form.id)
) do
{:ok, form_submit} ->
{:noreply,
socket
|> assign(:current_form_submit, form_submit)}
socket.assigns.event.uuid,
%{"response" => params}
|> Map.put("user_id", socket.assigns.current_user.id)
|> Map.put("form_id", socket.assigns.form.id)
) do
{:ok, form_submit} ->
{:noreply,
socket
|> assign(:current_form_submit, form_submit)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, :changeset, changeset)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, :changeset, changeset)}
end
end
@impl true
def handle_event("submit", %{"form_submit" => params}, %{assigns: %{attendee_identifier: attendee_identifier}} = socket) do
def handle_event(
"submit",
%{"form_submit" => params},
%{assigns: %{attendee_identifier: attendee_identifier}} = socket
) do
case Claper.Forms.create_or_update_form_submit(
socket.assigns.event.uuid,
%{"response" => params }
|> Map.put("attendee_identifier", attendee_identifier)
|> Map.put("form_id", socket.assigns.form.id)
) do
{:ok, form_submit} ->
{:noreply,
socket
|> assign(:current_form_submit, form_submit)}
socket.assigns.event.uuid,
%{"response" => params}
|> Map.put("attendee_identifier", attendee_identifier)
|> Map.put("form_id", socket.assigns.form.id)
) do
{:ok, form_submit} ->
{:noreply,
socket
|> assign(:current_form_submit, form_submit)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, :changeset, changeset)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, :changeset, changeset)}
end
end

View File

@@ -97,7 +97,9 @@ defmodule ClaperWeb.EventLive.Manage do
@impl true
def handle_info({:form_submit_created, fs}, socket) do
{:noreply,
socket |> update(:form_submits, fn form_submits -> [fs | form_submits] end) |> push_event("scroll", %{})}
socket
|> update(:form_submits, fn form_submits -> [fs | form_submits] end)
|> push_event("scroll", %{})}
end
@impl true
@@ -160,6 +162,7 @@ defmodule ClaperWeb.EventLive.Manage do
def handle_event("poll-set-default", %{"id" => id}, socket) do
Forms.disable_all(socket.assigns.event.presentation_file.id, socket.assigns.state.position)
Polls.set_default(
id,
socket.assigns.event.presentation_file.id,
@@ -183,11 +186,12 @@ defmodule ClaperWeb.EventLive.Manage do
{:noreply,
socket
|> assign(:polls, list_polls(socket, socket.assigns.event.presentation_file.id))
|> assign(:forms, list_forms(socket, socket.assigns.event.presentation_file.id))
}
|> assign(:forms, list_forms(socket, socket.assigns.event.presentation_file.id))}
end
def handle_event("form-set-default", %{"id" => id}, socket) do
Polls.disable_all(socket.assigns.event.presentation_file.id, socket.assigns.state.position)
Forms.set_default(
id,
socket.assigns.event.presentation_file.id,
@@ -300,17 +304,32 @@ defmodule ClaperWeb.EventLive.Manage do
form = Claper.Forms.get_form_submit_by_id!(id)
{:ok, _} = Claper.Forms.delete_form_submit(event_id, form)
{:noreply, assign(socket, :form_submits, list_form_submits(socket, socket.assigns.event.presentation_file.id))}
{:noreply,
assign(
socket,
:form_submits,
list_form_submits(socket, socket.assigns.event.presentation_file.id)
)}
end
@impl true
def handle_event("list-tab", %{"tab" => tab}, socket) do
socket = assign(socket, :list_tab, String.to_atom(tab))
socket = case tab do
"posts" -> assign(socket, :posts, list_posts(socket, socket.assigns.event.uuid))
"forms" -> assign(socket, :form_submits, list_form_submits(socket, socket.assigns.event.presentation_file.id))
end
{:noreply,socket}
socket =
case tab do
"posts" ->
assign(socket, :posts, list_posts(socket, socket.assigns.event.uuid))
"forms" ->
assign(
socket,
:form_submits,
list_form_submits(socket, socket.assigns.event.presentation_file.id)
)
end
{:noreply, socket}
end
@impl true
@@ -374,7 +393,10 @@ defmodule ClaperWeb.EventLive.Manage do
socket
|> assign(:create, "form")
|> assign(:form, %Forms.Form{
fields: [%Forms.Field{name: gettext("Name"), type: "text"}, %Forms.Field{name: gettext("Email"), type: "email"}]
fields: [
%Forms.Field{name: gettext("Name"), type: "text"},
%Forms.Field{name: gettext("Email"), type: "email"}
]
})
end
@@ -438,5 +460,4 @@ defmodule ClaperWeb.EventLive.Manage do
defp list_form_submits(_socket, presentation_file_id) do
Claper.Forms.list_form_submits(presentation_file_id)
end
end

View File

@@ -281,7 +281,7 @@ defmodule ClaperWeb.EventLive.Show do
def handle_info({:form_deleted, _form}, socket) do
{:noreply,
socket
|> update(:current_form, fn _current_form-> nil end)}
|> update(:current_form, fn _current_form -> nil end)}
end
@impl true
@@ -596,12 +596,15 @@ defmodule ClaperWeb.EventLive.Show do
end
defp get_current_form_submit(%{assigns: %{current_user: current_user}} = socket, form_id)
when is_map(current_user) do
when is_map(current_user) do
fs = Forms.get_form_submit(current_user.id, form_id)
socket |> assign(:current_form_submit, fs)
end
defp get_current_form_submit(%{assigns: %{attendee_identifier: attendee_identifier}} = socket, form_id) do
defp get_current_form_submit(
%{assigns: %{attendee_identifier: attendee_identifier}} = socket,
form_id
) do
fs = Forms.get_form_submit(attendee_identifier, form_id)
socket |> assign(:current_form_submit, fs)
end

View File

@@ -35,8 +35,7 @@ defmodule ClaperWeb.StatLive.Index do
:engagement_rate,
calculate_engagement_rate(grouped_total_votes, distinct_poster_count, event)
)
|> assign(:posts, list_posts(socket, event.uuid))
}
|> assign(:posts, list_posts(socket, event.uuid))}
end
@impl true

View File

@@ -1 +0,0 @@

View File

@@ -27,9 +27,7 @@ defmodule Claper.FormsTest do
test "get_form!/1 returns the form with given id" do
presentation_file = presentation_file_fixture()
form =
form_fixture(%{presentation_file_id: presentation_file.id})\
form = form_fixture(%{presentation_file_id: presentation_file.id})
assert Forms.get_form!(form.id) == form
end
@@ -88,7 +86,6 @@ defmodule Claper.FormsTest do
form = form_fixture(%{presentation_file_id: presentation_file.id})
assert %Ecto.Changeset{} = Forms.change_form(form)
end
end
describe "form_submits" do
@@ -103,17 +100,15 @@ defmodule Claper.FormsTest do
presentation_file = presentation_file_fixture(%{}, [:event])
f = form_fixture(%{presentation_file_id: presentation_file.id})
assert {:ok, %Claper.Forms.FormSubmit{}} =
Forms.create_or_update_form_submit(
presentation_file.event_id,
%{
"user_id" => presentation_file.event.user_id,
"form_id" => f.id,
"response" => %{:"Test" => "some option 1", :"Test 2" => "some option 2"}
}
presentation_file.event_id,
%{
"user_id" => presentation_file.event.user_id,
"form_id" => f.id,
"response" => %{:Test => "some option 1", :"Test 2" => "some option 2"}
}
)
end
end
end