Skip to content

Commit

Permalink
Added support for creating project items
Browse files Browse the repository at this point in the history
  • Loading branch information
mellelieuwes committed Jun 28, 2023
1 parent 1879be0 commit 8a79af0
Show file tree
Hide file tree
Showing 16 changed files with 382 additions and 31 deletions.
2 changes: 1 addition & 1 deletion core/bundles/next/lib/layouts/workspace/menu_builder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ defmodule Next.Layouts.Workspace.MenuBuilder do
def include_map(user),
do: %{
console: Authorization.can_access?(user, Next.Console.Page),
projects: Systems.Admin.Public.admin?(user)
projects: Systems.Admin.Public.admin?(user) or user.researcher
}
end
38 changes: 37 additions & 1 deletion core/lib/core/factories.ex
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,15 @@ defmodule Core.Factories do
end

def build(:project_node) do
build(:project_node, %{name: Faker.Lorem.word(), project_path: [1, 2]})
build(:project_node, %{name: Faker.Lorem.word(), project_path: []})
end

def build(:project_item) do
build(:project_item, %{name: Faker.Lorem.word(), project_path: []})
end

def build(:tool_ref) do
build(:tool_ref, %{})
end

def build(:auth_node, %{} = attributes) do
Expand Down Expand Up @@ -379,6 +387,34 @@ defmodule Core.Factories do
|> struct!(attributes)
end

def build(:project_item, %{} = attributes) do
{node, attributes} = Map.pop(attributes, :node, build(:project_node))
{tool_ref, attributes} = Map.pop(attributes, :tool_ref, build(:tool_ref))

%Project.ItemModel{
node: node,
tool_ref: tool_ref
}
|> struct!(attributes)
end

def build(:tool_ref, %{} = attributes) do
{item, attributes} = Map.pop(attributes, :item, build(:project_item))
{survey_tool, attributes} = Map.pop(attributes, :survey_tool, nil)
{lab_tool, attributes} = Map.pop(attributes, :lab_tool, nil)
{data_donation_tool, attributes} = Map.pop(attributes, :data_donation_tool, nil)
{benchmark_tool, attributes} = Map.pop(attributes, :benchmark_tool, nil)

%Project.ToolRefModel{
item: item,
survey_tool: survey_tool,
lab_tool: lab_tool,
data_donation_tool: data_donation_tool,
benchmark_tool: benchmark_tool
}
|> struct!(attributes)
end

def build(:assignment, %{} = attributes) do
{auth_node, attributes} = Map.pop(attributes, :auth_node, build(:auth_node))
{budget, attributes} = Map.pop(attributes, :budget, build(:budget))
Expand Down
2 changes: 1 addition & 1 deletion core/priv/gettext/en/LC_MESSAGES/eyra-enums.po
Original file line number Diff line number Diff line change
Expand Up @@ -340,4 +340,4 @@ msgstr "Benchmark"

#, elixir-autogen, elixir-format, fuzzy
msgid "project_tools.data_donation"
msgstr "Data Donation Study"
msgstr "Data Donation"
4 changes: 4 additions & 0 deletions core/priv/gettext/en/LC_MESSAGES/eyra-project.po
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,7 @@ msgstr "Retract"
#, elixir-autogen, elixir-format
msgid "create.item.button.short"
msgstr ""

#, elixir-autogen, elixir-format, fuzzy
msgid "create.item.title"
msgstr "Select item type"
4 changes: 4 additions & 0 deletions core/priv/gettext/eyra-project.pot
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,7 @@ msgstr ""
#, elixir-autogen, elixir-format
msgid "create.item.button.short"
msgstr ""

#, elixir-autogen, elixir-format
msgid "create.item.title"
msgstr ""
2 changes: 1 addition & 1 deletion core/priv/gettext/nl/LC_MESSAGES/eyra-enums.po
Original file line number Diff line number Diff line change
Expand Up @@ -340,4 +340,4 @@ msgstr "Benchmark"

#, elixir-autogen, elixir-format, fuzzy
msgid "project_tools.data_donation"
msgstr "Data Donatie Studie"
msgstr "Data Donatie"
4 changes: 4 additions & 0 deletions core/priv/gettext/nl/LC_MESSAGES/eyra-project.po
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,7 @@ msgstr "Terugtrekken"
#, elixir-autogen, elixir-format
msgid "create.item.button.short"
msgstr ""

#, elixir-autogen, elixir-format, fuzzy
msgid "create.item.title"
msgstr "Kies type item"
18 changes: 9 additions & 9 deletions core/priv/repo/seeds.exs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ images = [
"raw_url=https%3A%2F%2Fimages.unsplash.com%2Fphoto-1515378791036-0648a3ef77b2%3Fixid%3DMnwyMTY0MzZ8MHwxfHNlYXJjaHw4fHx3b3JrfGVufDB8fHx8MTYyMTc3NjgwOQ%26ixlib%3Drb-1.2.1&username=christinhumephoto&name=Christin+Hume&blur_hash=LMF%3B%3Dw0LAJR%25~A9uT0nNRjxaW%3DIo"
]

data_donation_promotions =
_data_donation_promotions =
Enum.map(images, fn image ->
%{
title: Faker.Lorem.sentence(),
Expand Down Expand Up @@ -52,15 +52,15 @@ data_donation_promotions =
}
end)

Enum.each(data_donation_promotions, fn promotion ->
data = %{
script: File.read!(Path.join([:code.priv_dir(:core), "repo", "script.py"])),
subject_count: 400
# promotion: promotion
}
# Enum.each(data_donation_promotions, fn promotion ->
# data = %{
# script: File.read!(Path.join([:code.priv_dir(:core), "repo", "script.py"])),
# subject_count: 400
# # promotion: promotion
# }

Core.Factories.insert!(:data_donation_tool, data)
end)
# Core.Factories.insert!(:data_donation_tool, data)
# end)

# campaigns =
# Enum.map(data_donation_tools, fn data_donation_tool ->
Expand Down
12 changes: 7 additions & 5 deletions core/systems/project/_assembly.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ defmodule Systems.Project.Assembly do
|> Repo.transaction()
end

def create_item(%Project.NodeModel{id: node_id} = node, tool_special) do
def create_item(name, %Project.NodeModel{id: node_id} = node, tool_special)
when is_binary(name) do
project =
from(p in Project.Model, where: p.root_id == ^node_id, preload: [:auth_node])
|> Repo.one!()
Expand All @@ -59,11 +60,12 @@ defmodule Systems.Project.Assembly do
end)
|> prepare_tool(tool_special)
|> Multi.insert(:tool_ref, fn %{tool: tool} ->
Project.Public.create_tool_ref(tool_special, tool)
key = String.to_existing_atom("#{tool_special}_tool")
Project.Public.create_tool_ref(key, tool)
end)
|> Multi.insert(:item, fn %{tool_ref: tool_ref} ->
Project.Public.create_item(
%{name: "Item", project_path: [project.id, node_id]},
%{name: name, project_path: [project.id, node_id]},
node,
tool_ref
)
Expand Down Expand Up @@ -120,14 +122,14 @@ defmodule Systems.Project.Assembly do
end)
end

defp prepare_tool(multi, :data_donation_tool) do
defp prepare_tool(multi, :data_donation) do
multi
|> Multi.insert(:tool, fn %{auth_node: auth_node} ->
DataDonation.Public.create(%{subject_count: 0, director: :project}, auth_node)
end)
end

defp prepare_tool(multi, :benchmark_tool) do
defp prepare_tool(multi, :benchmark) do
multi
|> Multi.insert(:tool, fn %{auth_node: auth_node} ->
Benchmark.Public.create(%{title: "", director: :project}, auth_node)
Expand Down
114 changes: 114 additions & 0 deletions core/systems/project/create_item_popup.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
defmodule Systems.Project.CreateItemPopup do
use CoreWeb, :live_component

alias Frameworks.Pixel.Selector

alias Systems.{
Project
}

# Handle Tool Type Selector Update
@impl true
def update(
%{active_item_id: active_item_id, selector_id: :tool_selector},
%{assigns: %{tool_labels: tool_labels}} = socket
) do
%{id: selected_tool} = Enum.find(tool_labels, &(&1.id == active_item_id))

{
:ok,
socket
|> assign(selected_tool: selected_tool)
}
end

# Initial Update
@impl true
def update(%{id: id, node: node, target: target}, socket) do
title = dgettext("eyra-project", "create.item.title")

{
:ok,
socket
|> assign(id: id, node: node, target: target, title: title)
|> init_tools()
|> init_buttons()
}
end

defp init_tools(socket) do
selected_tool = :empty
tool_labels = Project.Tools.labels(selected_tool)
socket |> assign(tool_labels: tool_labels, selected_tool: selected_tool)
end

defp init_buttons(%{assigns: %{myself: myself}} = socket) do
socket
|> assign(
buttons: [
%{
action: %{type: :send, event: "proceed", target: myself},
face: %{
type: :primary,
label: dgettext("eyra-project", "create.proceed.button")
}
},
%{
action: %{type: :send, event: "cancel", target: myself},
face: %{type: :label, label: dgettext("eyra-ui", "cancel.button")}
}
]
)
end

@impl true
def handle_event(
"proceed",
_,
%{assigns: %{selected_tool: selected_tool}} = socket
) do
create_item(socket, selected_tool)

{:noreply, socket |> close()}
end

@impl true
def handle_event("cancel", _, socket) do
{:noreply, socket |> close()}
end

defp close(%{assigns: %{target: target}} = socket) do
update_target(target, %{module: __MODULE__, action: :close})
socket
end

defp create_item(%{assigns: %{node: node}}, tool) do
name = Project.Tools.translate(tool)
Project.Assembly.create_item(name, node, tool)
end

@impl true
def render(assigns) do
~H"""
<div>
<Text.title3><%= @title %></Text.title3>
<.spacing value="S" />
<.live_component
module={Selector}
id={:tool_selector}
items={@tool_labels}
type={:radio}
optional?={false}
parent={%{type: __MODULE__, id: @id}}
/>
<.spacing value="M" />
<div class="flex flex-row gap-4">
<%= for button <- @buttons do %>
<Button.dynamic {button} />
<% end %>
</div>
</div>
"""
end
end
2 changes: 1 addition & 1 deletion core/systems/project/create_project_popup.ex
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ defmodule Systems.Project.CreatePopup do
~H"""
<div>
<Text.title3><%= @title %></Text.title3>
<.spacing value="XS" />
<.spacing value="S" />
<.live_component
module={Selector}
id={:template_selector}
Expand Down
77 changes: 77 additions & 0 deletions core/systems/project/item_form.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
defmodule Systems.Project.ItemForm do
use CoreWeb.LiveForm

alias Systems.{
Project
}

# Handle initial update
@impl true
def update(
%{id: id, entity: item, target: target},
socket
) do
changeset = Project.ItemModel.changeset(item, %{})

close_button = %{
action: %{type: :send, event: "close"},
face: %{type: :icon, icon: :close}
}

{
:ok,
socket
|> assign(
id: id,
entity: item,
target: target,
close_button: close_button,
changeset: changeset
)
}
end

# Handle Events
@impl true
def handle_event("close", _params, socket) do
send(self(), %{module: __MODULE__, action: :close})
{:noreply, socket}
end

@impl true
def handle_event("save", %{"item_model" => attrs}, %{assigns: %{entity: entity}} = socket) do
{
:noreply,
socket
|> save(entity, attrs)
}
end

# Saving

def save(socket, entity, attrs) do
changeset = Project.ItemModel.changeset(entity, attrs)

socket
|> save(changeset)
end

@impl true
def render(assigns) do
~H"""
<div>
<div class="flex flex-row">
<div>
<Text.title3><%= dgettext("eyra-project", "item.form.title") %></Text.title3>
</div>
<div class="flex-grow" />
<Button.dynamic {@close_button} />
</div>
<.form id={@id} :let={form} for={@changeset} phx-change="save" phx-target={@myself} >
<.text_input form={form} field={:name} label_text={dgettext("eyra-project", "item.form.name.label")} />
</.form>
</div>
"""
end
end
Loading

0 comments on commit 8a79af0

Please sign in to comment.