From 8f2c0bba425fa6d81bd511c724a2ba52d50106ea Mon Sep 17 00:00:00 2001 From: emielvdveen Date: Wed, 5 Jul 2023 16:15:44 +0200 Subject: [PATCH] Made platforms dynamic and refactored content page so it is live updatable --- core/assets/static/images/icons/ready.svg | 11 +++ core/frameworks/signal/_public.ex | 1 + core/lib/core/enums/base.ex | 16 ++++- .../gettext/en/LC_MESSAGES/eyra-project.po | 16 +++++ .../gettext/en/LC_MESSAGES/eyra-support.po | 4 ++ core/priv/gettext/eyra-project.pot | 16 +++++ core/priv/gettext/eyra-support.pot | 4 ++ .../gettext/nl/LC_MESSAGES/eyra-project.po | 16 +++++ .../gettext/nl/LC_MESSAGES/eyra-support.po | 4 ++ core/systems/data_donation/_public.ex | 62 ++++++++++++---- .../data_donation/document_task_form.ex | 12 +--- .../data_donation/document_task_model.ex | 14 ++++ .../data_donation/donate_task_model.ex | 14 ++++ .../data_donation/survey_task_model.ex | 14 ++++ .../data_donation/task_builder_view.ex | 25 ++----- core/systems/data_donation/task_cell.ex | 37 +++++----- core/systems/data_donation/task_form.ex | 3 +- core/systems/data_donation/task_model.ex | 43 +++++++++++ core/systems/data_donation/task_views.ex | 10 +-- core/systems/data_donation/tool_form.ex | 28 -------- core/systems/data_donation/tool_model.ex | 6 +- core/systems/project/_presenter.ex | 25 ++++--- core/systems/project/_public.ex | 9 +++ core/systems/project/_switch.ex | 72 +++++++++++++++++++ .../item_data_donation.ex | 28 +++++++- core/systems/project/item_model.ex | 13 ++-- core/systems/project/tool_ref_model.ex | 2 +- core/systems/support/form.ex | 15 ++++ 28 files changed, 407 insertions(+), 113 deletions(-) create mode 100644 core/assets/static/images/icons/ready.svg create mode 100644 core/systems/support/form.ex diff --git a/core/assets/static/images/icons/ready.svg b/core/assets/static/images/icons/ready.svg new file mode 100644 index 000000000..a5f0811bb --- /dev/null +++ b/core/assets/static/images/icons/ready.svg @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/core/frameworks/signal/_public.ex b/core/frameworks/signal/_public.ex index 21f746fd6..45f694d7f 100644 --- a/core/frameworks/signal/_public.ex +++ b/core/frameworks/signal/_public.ex @@ -5,6 +5,7 @@ defmodule Frameworks.Signal.Public do Core.WebPush.SignalHandlers, Core.APNS.SignalHandlers, Systems.Observatory.Switch, + Systems.Project.Switch, Systems.Assignment.Switch, Systems.Pool.Switch, Systems.Student.Switch, diff --git a/core/lib/core/enums/base.ex b/core/lib/core/enums/base.ex index f070d8c93..052b0db88 100644 --- a/core/lib/core/enums/base.ex +++ b/core/lib/core/enums/base.ex @@ -9,8 +9,22 @@ defmodule Core.Enums.Base do unquote(values) end + def contains(atom) when is_atom(atom) do + contains(Atom.to_string(atom)) + end + + def contains(binary) when is_binary(binary) do + values() + |> Enum.map(&Atom.to_string/1) + |> Enum.member?(binary) + end + def translate(value) do - Gettext.dgettext(CoreWeb.Gettext, "eyra-enums", "#{unquote(name)}.#{value}") + if contains(value) do + Gettext.dgettext(CoreWeb.Gettext, "eyra-enums", "#{unquote(name)}.#{value}") + else + value + end end def labels() do diff --git a/core/priv/gettext/en/LC_MESSAGES/eyra-project.po b/core/priv/gettext/en/LC_MESSAGES/eyra-project.po index 42408ff3a..03bae17b6 100644 --- a/core/priv/gettext/en/LC_MESSAGES/eyra-project.po +++ b/core/priv/gettext/en/LC_MESSAGES/eyra-project.po @@ -194,3 +194,19 @@ msgstr "" #, elixir-autogen, elixir-format, fuzzy msgid "create.item.title" msgstr "Select item type" + +#, elixir-autogen, elixir-format, fuzzy +msgid "item.form.name.label" +msgstr "Name" + +#, elixir-autogen, elixir-format +msgid "item.form.title" +msgstr "" + +#, elixir-autogen, elixir-format, fuzzy +msgid "tabbar.item.support" +msgstr "Support" + +#, elixir-autogen, elixir-format, fuzzy +msgid "tabbar.item.support.forward" +msgstr "Support" diff --git a/core/priv/gettext/en/LC_MESSAGES/eyra-support.po b/core/priv/gettext/en/LC_MESSAGES/eyra-support.po index 9982a211c..7d2740ef4 100644 --- a/core/priv/gettext/en/LC_MESSAGES/eyra-support.po +++ b/core/priv/gettext/en/LC_MESSAGES/eyra-support.po @@ -37,3 +37,7 @@ msgstr "Report is received" #, elixir-autogen, elixir-format msgid "ticket.type" msgstr "Form" + +#, elixir-autogen, elixir-format +msgid "content.form.title" +msgstr "Support" diff --git a/core/priv/gettext/eyra-project.pot b/core/priv/gettext/eyra-project.pot index 09d4237ef..82e0643df 100644 --- a/core/priv/gettext/eyra-project.pot +++ b/core/priv/gettext/eyra-project.pot @@ -194,3 +194,19 @@ msgstr "" #, elixir-autogen, elixir-format msgid "create.item.title" msgstr "" + +#, elixir-autogen, elixir-format +msgid "item.form.name.label" +msgstr "" + +#, elixir-autogen, elixir-format +msgid "item.form.title" +msgstr "" + +#, elixir-autogen, elixir-format +msgid "tabbar.item.support" +msgstr "" + +#, elixir-autogen, elixir-format +msgid "tabbar.item.support.forward" +msgstr "" diff --git a/core/priv/gettext/eyra-support.pot b/core/priv/gettext/eyra-support.pot index cf5326a67..abe6fae88 100644 --- a/core/priv/gettext/eyra-support.pot +++ b/core/priv/gettext/eyra-support.pot @@ -37,3 +37,7 @@ msgstr "" #, elixir-autogen, elixir-format msgid "ticket.type" msgstr "" + +#, elixir-autogen, elixir-format +msgid "content.form.title" +msgstr "" diff --git a/core/priv/gettext/nl/LC_MESSAGES/eyra-project.po b/core/priv/gettext/nl/LC_MESSAGES/eyra-project.po index 3d41e2200..ccf33a479 100644 --- a/core/priv/gettext/nl/LC_MESSAGES/eyra-project.po +++ b/core/priv/gettext/nl/LC_MESSAGES/eyra-project.po @@ -194,3 +194,19 @@ msgstr "" #, elixir-autogen, elixir-format, fuzzy msgid "create.item.title" msgstr "Kies type item" + +#, elixir-autogen, elixir-format, fuzzy +msgid "item.form.name.label" +msgstr "Naam" + +#, elixir-autogen, elixir-format +msgid "item.form.title" +msgstr "" + +#, elixir-autogen, elixir-format, fuzzy +msgid "tabbar.item.support" +msgstr "Support" + +#, elixir-autogen, elixir-format, fuzzy +msgid "tabbar.item.support.forward" +msgstr "Support" diff --git a/core/priv/gettext/nl/LC_MESSAGES/eyra-support.po b/core/priv/gettext/nl/LC_MESSAGES/eyra-support.po index a1def3115..55679c378 100644 --- a/core/priv/gettext/nl/LC_MESSAGES/eyra-support.po +++ b/core/priv/gettext/nl/LC_MESSAGES/eyra-support.po @@ -37,3 +37,7 @@ msgstr "Melding ontvangen" #, elixir-autogen, elixir-format msgid "ticket.type" msgstr "Formulier" + +#, elixir-autogen, elixir-format +msgid "content.form.title" +msgstr "Support" diff --git a/core/systems/data_donation/_public.ex b/core/systems/data_donation/_public.ex index e9ed12e21..754615587 100644 --- a/core/systems/data_donation/_public.ex +++ b/core/systems/data_donation/_public.ex @@ -15,6 +15,7 @@ defmodule Systems.DataDonation.Public do alias Ecto.Multi alias Core.Authorization + alias Frameworks.Signal alias Systems.{ DataDonation @@ -43,13 +44,23 @@ defmodule Systems.DataDonation.Public do |> Repo.get!(id) end - def get_task(tool_id, position, preload \\ []) do - from(task in DataDonation.TaskModel, - where: task.tool_id == ^tool_id, - where: task.position == ^position, - preload: ^preload - ) - |> Repo.one() + def get_task_by_special!(field, special_id, preload \\ []) + + def get_task_by_special!(field, special_id, preload) when is_atom(field) do + get_task_by_special!([field], special_id, preload) + end + + def get_task_by_special!([hd | tl], special_id, preload) when is_integer(special_id) do + query = + from(task in DataDonation.TaskModel, + where: field(task, ^hd) == ^special_id, + preload: ^preload + ) + + Enum.reduce(tl, query, fn key, query -> + query |> or_where([task], field(task, ^key) == ^special_id) + end) + |> Repo.one!() end def get_document_task!(id, preload \\ []) do @@ -68,11 +79,16 @@ defmodule Systems.DataDonation.Public do |> Ecto.Changeset.put_assoc(:auth_node, auth_node) end - def add_task(tool, task_type) when is_binary(task_type) do + def add_task(tool_id, task_type) when is_integer(tool_id) do + get_tool!(tool_id) + |> add_task(task_type) + end + + def add_task(%DataDonation.ToolModel{} = tool, task_type) when is_binary(task_type) do add_task(tool, String.to_existing_atom(task_type)) end - def add_task(%DataDonation.ToolModel{} = tool, task_type) do + def add_task(%DataDonation.ToolModel{id: tool_id} = tool, task_type) do Multi.new() |> Multi.run(:position, fn _, _ -> {:ok, task_count(tool)} @@ -81,6 +97,7 @@ defmodule Systems.DataDonation.Public do |> Multi.insert(:task, fn %{position: position, task_special: task_special} -> create_task(tool, position, task_type, task_special) end) + |> Signal.Public.multi_dispatch(:data_donation_tasks, %{tool_id: tool_id}) |> Repo.transaction() end @@ -119,9 +136,10 @@ defmodule Systems.DataDonation.Public do |> DataDonation.DonateTaskModel.changeset(%{}) end - def update(changeset) do + def update(changeset, key) do Multi.new() - |> Repo.multi_update(:data_donation_tool, changeset) + |> Repo.multi_update(key, changeset) + |> Signal.Public.multi_dispatch(key, %{changeset: changeset}) |> Repo.transaction() end @@ -140,6 +158,7 @@ defmodule Systems.DataDonation.Public do |> Multi.run(:order_and_update, fn _, %{tasks: tasks} -> {:ok, rearrange(tasks)} end) + |> Signal.Public.multi_dispatch(:data_donation_tasks, %{tool_id: tool_id}) |> Repo.transaction() end @@ -161,6 +180,7 @@ defmodule Systems.DataDonation.Public do |> Multi.run(:order_and_update, fn _, %{tasks: tasks} -> {:ok, rearrange(tasks, old, new)} end) + |> Signal.Public.multi_dispatch(:data_donation_tasks, %{tool_id: tool_id}) |> Repo.transaction() end @@ -221,9 +241,27 @@ end defimpl Core.Persister, for: Systems.DataDonation.ToolModel do def save(_tool, changeset) do - case Systems.DataDonation.Public.update(changeset) do + case Systems.DataDonation.Public.update(changeset, :data_donation_tool) do {:ok, %{data_donation_tool: tool}} -> {:ok, tool} _ -> {:error, changeset} end end end + +defimpl Core.Persister, for: Systems.DataDonation.TaskModel do + def save(_task, changeset) do + case Systems.DataDonation.Public.update(changeset, :data_donation_task) do + {:ok, %{data_donation_task: task}} -> {:ok, task} + _ -> {:error, changeset} + end + end +end + +defimpl Core.Persister, for: Systems.DataDonation.DocumentTaskModel do + def save(_task, changeset) do + case Systems.DataDonation.Public.update(changeset, :data_donation_document_task) do + {:ok, %{data_donation_document_task: task}} -> {:ok, task} + _ -> {:error, changeset} + end + end +end diff --git a/core/systems/data_donation/document_task_form.ex b/core/systems/data_donation/document_task_form.ex index 4d1e5b7e7..bd2f140fe 100644 --- a/core/systems/data_donation/document_task_form.ex +++ b/core/systems/data_donation/document_task_form.ex @@ -19,8 +19,7 @@ defmodule Systems.DataDonation.DocumentTaskForm do def update( %{ id: id, - parent: parent, - entity_id: entity_id + entity: entity }, socket ) do @@ -33,22 +32,15 @@ defmodule Systems.DataDonation.DocumentTaskForm do socket |> assign( id: id, - parent: parent, placeholder: placeholder, select_button: select_button, replace_button: replace_button, - entity_id: entity_id + entity: entity ) |> init_file_uploader(:pdf) - |> update_entity() } end - defp update_entity(%{assigns: %{entity_id: entity_id}} = socket) do - entity = DataDonation.Public.get_document_task!(entity_id) - assign(socket, entity: entity) - end - @impl true def handle_event("change", _params, socket) do {:noreply, socket} diff --git a/core/systems/data_donation/document_task_model.ex b/core/systems/data_donation/document_task_model.ex index 87c60dd8b..0ec534014 100644 --- a/core/systems/data_donation/document_task_model.ex +++ b/core/systems/data_donation/document_task_model.ex @@ -9,9 +9,23 @@ defmodule Systems.DataDonation.DocumentTaskModel do end @fields ~w(document_name document_ref)a + @required_fields @fields def changeset(model, params) do model |> cast(params, @fields) end + + def validate(changeset) do + changeset + |> validate_required(@required_fields) + end + + def ready?(task) do + changeset = + changeset(task, %{}) + |> validate() + + changeset.valid?() + end end diff --git a/core/systems/data_donation/donate_task_model.ex b/core/systems/data_donation/donate_task_model.ex index d763899e8..bbc2c2c2e 100644 --- a/core/systems/data_donation/donate_task_model.ex +++ b/core/systems/data_donation/donate_task_model.ex @@ -7,9 +7,23 @@ defmodule Systems.DataDonation.DonateTaskModel do end @fields ~w()a + @required_fields @fields def changeset(model, params) do model |> cast(params, @fields) end + + def validate(changeset) do + changeset + |> validate_required(@required_fields) + end + + def ready?(task) do + changeset = + changeset(task, %{}) + |> validate() + + changeset.valid?() + end end diff --git a/core/systems/data_donation/survey_task_model.ex b/core/systems/data_donation/survey_task_model.ex index 274cf0c7b..f2af33ef1 100644 --- a/core/systems/data_donation/survey_task_model.ex +++ b/core/systems/data_donation/survey_task_model.ex @@ -7,9 +7,23 @@ defmodule Systems.DataDonation.SurveyTaskModel do end @fields ~w()a + @required_fields @fields def changeset(model, params) do model |> cast(params, @fields) end + + def validate(changeset) do + changeset + |> validate_required(@required_fields) + end + + def ready?(task) do + changeset = + changeset(task, %{}) + |> validate() + + changeset.valid?() + end end diff --git a/core/systems/data_donation/task_builder_view.ex b/core/systems/data_donation/task_builder_view.ex index c69f1bf8b..5031a3349 100644 --- a/core/systems/data_donation/task_builder_view.ex +++ b/core/systems/data_donation/task_builder_view.ex @@ -12,19 +12,19 @@ defmodule Systems.DataDonation.TaskBuilderView do @impl true def update(%{action: "delete", task: task}, socket) do DataDonation.Public.delete(task) - {:ok, socket |> update_tasks()} + {:ok, socket} end @impl true def update(%{action: "up", task: %{position: position} = task}, socket) do {:ok, _} = DataDonation.Public.update_position(task, position - 1) - {:ok, socket |> update_tasks()} + {:ok, socket} end @impl true def update(%{action: "down", task: %{position: position} = task}, socket) do {:ok, _} = DataDonation.Public.update_position(task, position + 1) - {:ok, socket |> update_tasks()} + {:ok, socket} end @impl true @@ -38,29 +38,16 @@ defmodule Systems.DataDonation.TaskBuilderView do flow: flow, library: library ) - |> update_tool() - |> update_tasks() } end - defp update_tool(%{assigns: %{tool_id: tool_id}} = socket) do - tool = DataDonation.Public.get_tool!(tool_id) - socket |> assign(tool: tool) - end - - defp update_tasks(%{assigns: %{tool_id: tool_id}} = socket) do - tasks = DataDonation.Public.list_tasks(tool_id, DataDonation.TaskModel.preload_graph(:down)) - socket |> assign(tasks: tasks) - end - @impl true - def handle_event("add", %{"item" => item}, %{assigns: %{tool: tool}} = socket) do - {:ok, _} = DataDonation.Public.add_task(tool, "#{item}_task") + def handle_event("add", %{"item" => item}, %{assigns: %{tool_id: tool_id}} = socket) do + {:ok, _} = DataDonation.Public.add_task(tool_id, "#{item}_task") { :noreply, socket - |> update_tasks() } end @@ -75,7 +62,7 @@ defmodule Systems.DataDonation.TaskBuilderView do <%= @flow.title %> <%= @flow.description %> <.spacing value="M" /> - <.list tasks={@tasks} parent={%{type: __MODULE__, id: @id}} /> + <.list tasks={@flow.tasks} parent={%{type: __MODULE__, id: @id}} />
diff --git a/core/systems/data_donation/task_cell.ex b/core/systems/data_donation/task_cell.ex index e88a58fc5..3cfb76915 100644 --- a/core/systems/data_donation/task_cell.ex +++ b/core/systems/data_donation/task_cell.ex @@ -7,7 +7,7 @@ defmodule Systems.DataDonation.TaskCell do @impl true def update( - %{id: id, entity_id: entity_id, parent: parent, relative_position: relative_position}, + %{id: id, task: task, parent: parent, relative_position: relative_position}, socket ) do { @@ -15,14 +15,14 @@ defmodule Systems.DataDonation.TaskCell do socket |> assign( id: id, - entity_id: entity_id, + task: task, parent: parent, relative_position: relative_position ) - |> update_task() |> update_task_form() |> update_special_form() |> update_title() + |> update_status() |> update_buttons() } end @@ -33,16 +33,11 @@ defmodule Systems.DataDonation.TaskCell do {:noreply, socket} end - defp update_task(%{assigns: %{entity_id: entity_id}} = socket) do - task = DataDonation.Public.get_task!(entity_id, DataDonation.TaskModel.preload_graph(:down)) - socket |> assign(task: task) - end - defp update_task_form(%{assigns: %{id: id, task: task}} = socket) do task_form = %{ id: "#{id}_task_form", module: DataDonation.TaskForm, - entity_id: task.id + entity: task } socket |> assign(task_form: task_form) @@ -53,24 +48,27 @@ defmodule Systems.DataDonation.TaskCell do socket |> assign(special_form: special_form) end - defp special_form(%{request_task: %{id: id}}), + defp special_form(%{request_task: %{id: id} = entity}), do: %{ id: "#{id}_request_form", module: DataDonation.DocumentTaskForm, - entity_id: id, - parent: %{type: __MODULE__, id: id} + entity: entity } - defp special_form(%{download_task: %{id: id}}), + defp special_form(%{download_task: %{id: id} = entity}), do: %{ id: "#{id}_request_form", module: DataDonation.DocumentTaskForm, - entity_id: id, - parent: %{type: __MODULE__, id: id} + entity: entity } defp special_form(_), do: nil + defp update_status(%{assigns: %{task: task}} = socket) do + status = DataDonation.TaskModel.status(task) + assign(socket, status: status) + end + defp update_title(%{assigns: %{task: task}} = socket) do title = get_title(task) assign(socket, title: title) @@ -137,8 +135,13 @@ defmodule Systems.DataDonation.TaskCell do def render(assigns) do ~H"""
-
- <%= @title %> +
+ <%= @title %> + <%= if @status == :ready do %> +
+ ready +
+ <% end %>
<%= if @relative_position != :bottom do %> diff --git a/core/systems/data_donation/task_form.ex b/core/systems/data_donation/task_form.ex index a3ce30a74..1476d622b 100644 --- a/core/systems/data_donation/task_form.ex +++ b/core/systems/data_donation/task_form.ex @@ -9,10 +9,9 @@ defmodule Systems.DataDonation.TaskForm do @impl true def update( - %{id: id, entity_id: entity_id}, + %{id: id, entity: entity}, socket ) do - entity = DataDonation.Public.get_task!(entity_id) changeset = DataDonation.TaskModel.changeset(entity, %{}) { diff --git a/core/systems/data_donation/task_model.ex b/core/systems/data_donation/task_model.ex index 84eb6d9de..b8a739ee7 100644 --- a/core/systems/data_donation/task_model.ex +++ b/core/systems/data_donation/task_model.ex @@ -26,12 +26,55 @@ defmodule Systems.DataDonation.TaskModel do end @fields ~w(platform position title description)a + @required_fields @fields def changeset(model, params) do model |> cast(params, @fields) end + def validate(changeset) do + changeset + |> validate_required(@required_fields) + end + + def ready?(%DataDonation.TaskModel{} = task) do + changeset = + changeset(task, %{}) + |> validate() + + changeset.valid?() + end + + def ready?(list) when is_list(list) do + if Enum.member?(list, false) do + :incomplete + else + :ready + end + end + + def ready?(%DataDonation.SurveyTaskModel{} = special), + do: DataDonation.SurveyTaskModel.ready?(special) + + def ready?(%DataDonation.DocumentTaskModel{} = special), + do: DataDonation.DocumentTaskModel.ready?(special) + + def ready?(%DataDonation.DonateTaskModel{} = special), + do: DataDonation.DonateTaskModel.ready?(special) + + def status(%DataDonation.TaskModel{} = task) do + ready?([ + ready?(task), + ready?(special(task)) + ]) + end + + defp special(%{survey_task: %{id: _id} = special}), do: special + defp special(%{request_task: %{id: _id} = special}), do: special + defp special(%{download_task: %{id: _id} = special}), do: special + defp special(%{donate_task: %{id: _id} = special}), do: special + def preload_graph(:down), do: preload_graph([ diff --git a/core/systems/data_donation/task_views.ex b/core/systems/data_donation/task_views.ex index 57a26b94f..bfdc03870 100644 --- a/core/systems/data_donation/task_views.ex +++ b/core/systems/data_donation/task_views.ex @@ -57,14 +57,16 @@ defmodule Systems.DataDonation.TaskViews do def list(assigns) do ~H"""
- - <%= dgettext("eyra-data-donation", "task.list.hint") %> - + <%= if Enum.count(@tasks) > 0 do %> + + <%= dgettext("eyra-data-donation", "task.list.hint") %> + + <% end %> <%= for task <- @tasks do %> <.live_component id={"task-cell-#{task.id}"} module={DataDonation.TaskCell} - entity_id={task.id} + task={task} parent={@parent} relative_position={relative_position(task.position, Enum.count(@tasks))} /> diff --git a/core/systems/data_donation/tool_form.ex b/core/systems/data_donation/tool_form.ex index cedb47375..392a985dc 100644 --- a/core/systems/data_donation/tool_form.ex +++ b/core/systems/data_donation/tool_form.ex @@ -4,22 +4,11 @@ defmodule Systems.DataDonation.ToolForm do alias Core.Accounts import Frameworks.Pixel.Form - alias Frameworks.Pixel.Selector alias Systems.{ DataDonation } - require Systems.DataDonation.Platforms - - @impl true - def update( - %{active_item_ids: active_item_ids, selector_id: selector_id}, - %{assigns: %{entity: entity}} = socket - ) do - {:ok, socket |> save(entity, %{selector_id => active_item_ids})} - end - @impl true def update( %{id: id, entity_id: entity_id}, @@ -37,15 +26,9 @@ defmodule Systems.DataDonation.ToolForm do entity: entity, changeset: changeset ) - |> update_platform_labels() } end - defp update_platform_labels(%{assigns: %{entity: %{platforms: platforms}}} = socket) do - platform_labels = DataDonation.Platforms.labels(platforms) - socket |> assign(platform_labels: platform_labels) - end - # Handle Events @impl true @@ -77,7 +60,6 @@ defmodule Systems.DataDonation.ToolForm do |> save(changeset) end - attr(:entity_id, :any, required: true) @impl true def render(assigns) do ~H""" @@ -85,16 +67,6 @@ defmodule Systems.DataDonation.ToolForm do <.form id={@id} :let={form} for={@changeset} phx-change="save" phx-target={@myself} > <.number_input form={form} field={:subject_count} label_text={dgettext("eyra-data-donation", "config.nrofsubjects.label")} /> - <.spacing value="M" /> - - <%= dgettext("eyra-data-donation", "platforms.title") %> - <.live_component - module={Selector} - id={:platforms} - items={@platform_labels} - type={:label} - parent={%{type: __MODULE__, id: @id}} - />
""" end diff --git a/core/systems/data_donation/tool_model.ex b/core/systems/data_donation/tool_model.ex index 75141f94e..c814fb33f 100644 --- a/core/systems/data_donation/tool_model.ex +++ b/core/systems/data_donation/tool_model.ex @@ -37,13 +37,15 @@ defmodule Systems.DataDonation.ToolModel do @impl true def operational_validation(changeset), do: changeset - def preload_graph(:full), + def preload_graph(:down), do: preload_graph([ - :auth_node + :auth_node, + :tasks ]) def preload_graph(:auth_node), do: [auth_node: []] + def preload_graph(:tasks), do: [tasks: []] defimpl Frameworks.GreenLight.AuthorizationNode do def id(tool), do: tool.auth_node_id diff --git a/core/systems/project/_presenter.ex b/core/systems/project/_presenter.ex index 40449b7f3..5ce0b1b7f 100644 --- a/core/systems/project/_presenter.ex +++ b/core/systems/project/_presenter.ex @@ -6,25 +6,30 @@ defmodule Systems.Project.Presenter do } @impl true - def view_model(id, Project.NodePage = page, assigns) when is_number(id) do - Project.Public.get_node!(id, Project.NodeModel.preload_graph(:down)) - |> view_model(page, assigns) + def view_model(%Project.NodeModel{} = node, page, assigns) do + builder(page).view_model(node, assigns) end @impl true - def view_model(id, Project.ItemContentPage = page, assigns) when is_number(id) do - Project.Public.get_item!(id, Project.ItemModel.preload_graph(:down)) - |> view_model(page, assigns) + def view_model(%Project.ItemModel{} = item, page, assigns) do + builder(page).view_model(item, assigns) end @impl true - def view_model(%Project.NodeModel{} = node, page, assigns) do - builder(page).view_model(node, assigns) + def view_model(%{id: id}, page, assigns) do + view_model(id, page, assigns) end @impl true - def view_model(%Project.ItemModel{} = item, page, assigns) do - builder(page).view_model(item, assigns) + def view_model(id, Project.NodePage = page, assigns) when is_number(id) do + Project.Public.get_node!(id, Project.NodeModel.preload_graph(:down)) + |> view_model(page, assigns) + end + + @impl true + def view_model(id, Project.ItemContentPage = page, assigns) when is_number(id) do + Project.Public.get_item!(id, Project.ItemModel.preload_graph(:down)) + |> view_model(page, assigns) end defp builder(Project.NodePage), do: Project.NodePageBuilder diff --git a/core/systems/project/_public.ex b/core/systems/project/_public.ex index 4e5b67c61..b4896c679 100644 --- a/core/systems/project/_public.ex +++ b/core/systems/project/_public.ex @@ -34,6 +34,15 @@ defmodule Systems.Project.Public do |> Repo.one!() end + def get_tool_refs_by_tool!(field, tool_id, preload \\ []) + when is_atom(field) and is_integer(tool_id) do + from(tr in Project.ToolRefModel, + where: field(tr, ^field) == ^tool_id, + preload: ^preload + ) + |> Repo.all() + end + @doc """ Returns the list of projects that are owned by the user. """ diff --git a/core/systems/project/_switch.ex b/core/systems/project/_switch.ex index fcdbd0db7..1eb24fe5a 100644 --- a/core/systems/project/_switch.ex +++ b/core/systems/project/_switch.ex @@ -1,2 +1,74 @@ defmodule Systems.Project.Switch do + use Frameworks.Signal.Handler + + alias Frameworks.Signal + + alias Systems.{ + Project, + DataDonation + } + + @impl true + def dispatch(:data_donation_tool, %{changeset: changeset}) do + tool_id = Ecto.Changeset.get_field(changeset, :id) + update_pages(tool_id) + end + + @impl true + def dispatch(:data_donation_task, %{changeset: changeset}) do + task_id = Ecto.Changeset.get_field(changeset, :id) + task = DataDonation.Public.get_task!(task_id) + update_pages(task) + end + + @impl true + def dispatch(:data_donation_document_task, %{changeset: changeset}) do + special_id = Ecto.Changeset.get_field(changeset, :id) + + task = + DataDonation.Public.get_task_by_special!([:request_task_id, :download_task_id], special_id) + + update_pages(task) + end + + @impl true + def dispatch(:data_donation_survey_task, %{changeset: changeset}) do + special_id = Ecto.Changeset.get_field(changeset, :id) + task = DataDonation.Public.get_task_by_special!(:survey_task_id, special_id) + update_pages(task) + end + + @impl true + def dispatch(:data_donation_donate_task, %{changeset: changeset}) do + special_id = Ecto.Changeset.get_field(changeset, :id) + task = DataDonation.Public.get_task_by_special!(:donate_task_id, special_id) + update_pages(task) + end + + @impl true + def dispatch(:data_donation_tasks, %{tool_id: tool_id}) do + update_pages(tool_id) + end + + defp update_pages(%DataDonation.TaskModel{tool_id: tool_id}) do + update_pages(tool_id) + end + + defp update_pages(tool_id) when is_integer(tool_id) do + Project.Public.get_tool_refs_by_tool!(:data_donation_tool_id, tool_id, [:item]) + |> Enum.each(&update_pages/1) + end + + defp update_pages(%Project.ToolRefModel{item: item}) do + update_pages(item) + end + + defp update_pages(%Project.ItemModel{id: item_id}) do + [Project.ItemContentPage] + |> Enum.each(&update_page(&1, item_id)) + end + + defp update_page(page, item_id) do + Signal.Public.dispatch!(%{page: page}, %{id: item_id, model: %{id: item_id}}) + end end diff --git a/core/systems/project/content_page_builder/item_data_donation.ex b/core/systems/project/content_page_builder/item_data_donation.ex index 57687bc66..6510b21ea 100644 --- a/core/systems/project/content_page_builder/item_data_donation.ex +++ b/core/systems/project/content_page_builder/item_data_donation.ex @@ -4,7 +4,8 @@ defmodule Systems.Project.ContentPageBuilder.ItemDataDonation do alias Systems.{ Project, DataDonation, - Privacy + Privacy, + Support } def view_model( @@ -92,7 +93,7 @@ defmodule Systems.Project.ContentPageBuilder.ItemDataDonation do end defp get_tab_keys() do - [:config, :tasks, :privacy, :invite, :monitor] + [:config, :privacy, :tasks, :support, :invite, :monitor] end defp create_tab( @@ -130,6 +131,8 @@ defmodule Systems.Project.ContentPageBuilder.ItemDataDonation do ) do ready? = false + tasks = DataDonation.Public.list_tasks(tool_id, DataDonation.TaskModel.preload_graph(:down)) + %{ id: :tasks_form, ready: ready?, @@ -142,7 +145,8 @@ defmodule Systems.Project.ContentPageBuilder.ItemDataDonation do tool_id: tool_id, flow: %{ title: dgettext("eyra-data-donation", "task.list.title"), - description: dgettext("eyra-data-donation", "task.list.description") + description: dgettext("eyra-data-donation", "task.list.description"), + tasks: tasks }, library: %{ title: dgettext("eyra-data-donation", "task.library.title"), @@ -196,6 +200,24 @@ defmodule Systems.Project.ContentPageBuilder.ItemDataDonation do } end + defp create_tab( + :support, + item, + _show_errors, + _assigns + ) do + %{ + id: :support, + title: dgettext("eyra-project", "tabbar.item.support"), + forward_title: dgettext("eyra-project", "tabbar.item.support.forward"), + type: :fullpage, + live_component: Support.Form, + props: %{ + entity: item + } + } + end + defp create_tab( :invite, %{tool_ref: %{data_donation_tool: %{id: tool_id}}}, diff --git a/core/systems/project/item_model.ex b/core/systems/project/item_model.ex index 508456745..06e759862 100644 --- a/core/systems/project/item_model.ex +++ b/core/systems/project/item_model.ex @@ -42,8 +42,8 @@ defmodule Systems.Project.ItemModel do defimpl Frameworks.Utility.ViewModelBuilder do use CoreWeb, :verified_routes - def view_model(%Project.ItemModel{} = project, page, %{current_user: user}) do - vm(project, page, user) + def view_model(%Project.ItemModel{} = item, page, %{current_user: user}) do + vm(item, page, user) end defp vm( @@ -160,8 +160,13 @@ defmodule Systems.Project.ItemModel do defp get_label(:idle), do: %{type: :idle, text: dgettext("eyra-project", "label.idle")} - defp get_card_tags(%DataDonation.ToolModel{platforms: platforms}) when is_list(platforms), - do: Enum.map(platforms, &DataDonation.Platforms.translate(&1)) + defp get_card_tags(%DataDonation.ToolModel{tasks: tasks}) when is_list(tasks) do + tasks + |> Enum.map(& &1.platform) + |> Enum.filter(&is_binary/1) + |> Enum.uniq() + |> Enum.map(&DataDonation.Platforms.translate/1) + end defp get_card_tags(%Benchmark.ToolModel{}), do: ["Challenge"] defp get_card_tags(_), do: [] diff --git a/core/systems/project/tool_ref_model.ex b/core/systems/project/tool_ref_model.ex index f6e17c97d..b1524d8bd 100644 --- a/core/systems/project/tool_ref_model.ex +++ b/core/systems/project/tool_ref_model.ex @@ -44,7 +44,7 @@ defmodule Systems.Project.ToolRefModel do def preload_graph(:lab_tool), do: [lab_tool: Lab.ToolModel.preload_graph(:full)] def preload_graph(:data_donation_tool), - do: [data_donation_tool: DataDonation.ToolModel.preload_graph(:full)] + do: [data_donation_tool: DataDonation.ToolModel.preload_graph(:down)] def preload_graph(:benchmark_tool), do: [benchmark_tool: Benchmark.ToolModel.preload_graph(:down)] diff --git a/core/systems/support/form.ex b/core/systems/support/form.ex new file mode 100644 index 000000000..da14be4c8 --- /dev/null +++ b/core/systems/support/form.ex @@ -0,0 +1,15 @@ +defmodule Systems.Support.Form do + use CoreWeb.LiveForm + + @impl true + def render(assigns) do + ~H""" +
+ + + <%= dgettext("eyra-support", "content.form.title") %> + +
+ """ + end +end