Skip to content

Commit

Permalink
Added temp delete item
Browse files Browse the repository at this point in the history
  • Loading branch information
mellelieuwes committed Jun 26, 2023
1 parent f44f7f5 commit fc0f0e9
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 29 deletions.
31 changes: 30 additions & 1 deletion core/lib/core/factories.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ defmodule Core.Factories do
Budget,
Bookkeeping,
Content,
Org
Org,
Project
}

def valid_user_password, do: Faker.Util.format("%5d%5a%5A#")
Expand Down Expand Up @@ -245,6 +246,14 @@ defmodule Core.Factories do
build(:assignment, %{})
end

def build(:project) do
build(:project, %{name: Faker.Lorem.word()})
end

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

def build(:auth_node, %{} = attributes) do
%Authorization.Node{}
|> struct!(attributes)
Expand Down Expand Up @@ -350,6 +359,26 @@ defmodule Core.Factories do
|> struct!(attributes)
end

def build(:project, %{} = attributes) do
{auth_node, attributes} = Map.pop(attributes, :auth_node, build(:auth_node))
{node, attributes} = Map.pop(attributes, :budget, build(:project_node))

%Project.Model{
auth_node: auth_node,
root: node
}
|> struct!(attributes)
end

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

%Project.NodeModel{
auth_node: auth_node
}
|> 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
48 changes: 46 additions & 2 deletions core/systems/project/_assembly.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ defmodule Systems.Project.Assembly do
|> Repo.delete_all()
end

def delete(%Project.ItemModel{id: id}) do
from(item in Project.ItemModel,
where: item.id == ^id
)
|> Repo.delete_all()
end

def create(name, user, :empty) do
Multi.new()
|> prepare_project(name, user)
Expand All @@ -41,6 +48,29 @@ defmodule Systems.Project.Assembly do
|> Repo.transaction()
end

def create_item(%Project.NodeModel{id: node_id} = node, tool_special) do
project =
from(p in Project.Model, where: p.root_id == ^node_id, preload: [:auth_node])
|> Repo.one!()

Multi.new()
|> Multi.insert(:auth_node, fn _ ->
Authorization.make_node(project.auth_node)
end)
|> prepare_tool(tool_special)
|> Multi.insert(:tool_ref, fn %{tool: tool} ->
Project.Public.create_tool_ref(tool_special, tool)
end)
|> Multi.insert(:item, fn %{tool_ref: tool_ref} ->
Project.Public.create_item(
%{name: "Item", project_path: [project.id, node_id]},
node,
tool_ref
)
end)
|> Repo.transaction()
end

defp prepare_project(multi, name, user) do
multi
|> Project.Public.create(%{name: name})
Expand All @@ -64,7 +94,7 @@ defmodule Systems.Project.Assembly do
end)
end

defp prepare_tool_ref(multi, index, :data_donation) do
defp prepare_tool_ref(multi, index, :data_donation) when is_integer(index) do
multi
|> Multi.insert({:tool_auth_node, index}, fn %{root: %{auth_node: auth_node}} ->
Authorization.make_node(auth_node)
Expand All @@ -77,7 +107,7 @@ defmodule Systems.Project.Assembly do
end)
end

defp prepare_tool_ref(multi, index, :benchmark) do
defp prepare_tool_ref(multi, index, :benchmark) when is_integer(index) do
multi
|> Multi.insert({:tool_auth_node, index}, fn %{root: %{auth_node: auth_node}} ->
Authorization.make_node(auth_node)
Expand All @@ -89,4 +119,18 @@ defmodule Systems.Project.Assembly do
Project.Public.create_tool_ref(:benchmark_tool, tool)
end)
end

defp prepare_tool(multi, :data_donation_tool) 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
multi
|> Multi.insert(:tool, fn %{auth_node: auth_node} ->
Benchmark.Public.create(%{title: "", director: :project}, auth_node)
end)
end
end
5 changes: 5 additions & 0 deletions core/systems/project/_public.ex
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ defmodule Systems.Project.Public do
|> Project.Assembly.delete()
end

def delete_item(id) when is_number(id) do
get_item!(id, Project.ItemModel.preload_graph(:down))
|> Project.Assembly.delete()
end

def create(
%Multi{} = multi,
%{name: _name} = attrs
Expand Down
16 changes: 8 additions & 8 deletions core/systems/project/item_model.ex
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ defmodule Systems.Project.ItemModel do
tags = get_card_tags(tool)
path = ~p"/project/item/#{id}/content"

delete = %{
action: %{type: :send, event: "delete", item: id},
face: %{type: :icon, icon: :delete}
}

%{
type: :secondary,
id: id,
Expand All @@ -71,8 +76,8 @@ defmodule Systems.Project.ItemModel do
title: name,
tags: tags,
info: ["#{subject_count} participants | 0 donations"],
right_actions: [],
left_actions: []
left_actions: [],
right_actions: [delete]
}
end

Expand All @@ -97,11 +102,6 @@ defmodule Systems.Project.ItemModel do
path = ~p"/project/item/#{id}/content"
label = get_label(status)

duplicate = %{
action: %{type: :send, event: "duplicate", item: id},
face: %{type: :label, label: "Duplicate", wrap: true}
}

delete = %{
action: %{type: :send, event: "delete", item: id},
face: %{type: :icon, icon: :delete}
Expand All @@ -127,7 +127,7 @@ defmodule Systems.Project.ItemModel do
title: name,
tags: tags,
info: [info_line_1],
left_actions: [duplicate],
left_actions: [],
right_actions: [delete]
}
end
Expand Down
16 changes: 14 additions & 2 deletions core/systems/project/node_page.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ defmodule Systems.Project.NodePage do
socket |> update_menus()
end

@impl true
def handle_event("delete", %{"item" => item_id}, socket) do
Project.Public.delete_item(String.to_integer(item_id))

{
:noreply,
socket
|> update_view_model()
|> update_menus()
}
end

@impl true
def handle_event(
"card_clicked",
Expand Down Expand Up @@ -62,10 +74,10 @@ defmodule Systems.Project.NodePage do
<div class="h-full pt-2px lg:pt-1">
<Button.Action.send event="create_item">
<div class="sm:hidden">
<Button.Face.plain_icon label={dgettext("eyra-project", "add.new.node.button.short")} icon={:forward} />
<Button.Face.plain_icon label={dgettext("eyra-project", "create.item.button.short")} icon={:forward} />
</div>
<div class="hidden sm:block">
<Button.Face.plain_icon label={dgettext("eyra-project", "add.new.node.button")} icon={:forward} />
<Button.Face.plain_icon label={dgettext("eyra-project", "create.item.button")} icon={:forward} />
</div>
</Button.Action.send>
</div>
Expand Down
3 changes: 2 additions & 1 deletion core/systems/project/node_page_builder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ defmodule Systems.Project.NodePageBuilder do
id: id,
title: node.name,
node_cards: node_cards,
item_cards: item_cards
item_cards: item_cards,
node: node
}
end

Expand Down
15 changes: 0 additions & 15 deletions core/systems/project/overview_page.ex
Original file line number Diff line number Diff line change
Expand Up @@ -138,21 +138,6 @@ defmodule Systems.Project.OverviewPage do
}
end

@impl true
def handle_event("duplicate", %{"item" => project_id}, socket) do
preload = Project.Model.preload_graph(:full)
_project = Project.Public.get!(String.to_integer(project_id), preload)

# Project.Assembly.copy(project)

{
:noreply,
socket
|> update_cards()
|> update_menus()
}
end

@impl true
def handle_event("create_project", _params, %{assigns: %{current_user: user}} = socket) do
popup = %{
Expand Down
3 changes: 3 additions & 0 deletions core/systems/project/tools.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
defmodule Systems.Project.Tools do
use Core.Enums.Base, {:project_tools, [:data_donation, :benchmark]}
end
11 changes: 11 additions & 0 deletions core/test/systems/project/assembly_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
defmodule Systems.Project.AssemblyTest do
use Core.DataCase

alias Systems.{
Project
}

test "create_item/2" do
%{root: root} = Factories.insert!(:project, %{name: "AAP"})
end
end

0 comments on commit fc0f0e9

Please sign in to comment.