|
| 1 | +defmodule ValentineWeb.WorkspaceLive.Components.EntityLinkerComponent do |
| 2 | + use ValentineWeb, :live_component |
| 3 | + use PrimerLive |
| 4 | + |
| 5 | + @impl true |
| 6 | + def render(assigns) do |
| 7 | + ~H""" |
| 8 | + <div> |
| 9 | + <.dialog id="linker-modal" is_backdrop is_show is_wide on_cancel={JS.patch(@patch)}> |
| 10 | + <:header_title> |
| 11 | + {gettext("Link %{source_entity_type} to %{target_entity_type}", |
| 12 | + source_entity_type: Atom.to_string(@source_entity_type), |
| 13 | + target_entity_type: Atom.to_string(@target_entity_type) |
| 14 | + )} |
| 15 | + </:header_title> |
| 16 | + <:body> |
| 17 | + <.live_component |
| 18 | + module={ValentineWeb.WorkspaceLive.Components.DropdownSelectComponent} |
| 19 | + id="link-dropdown" |
| 20 | + name={Atom.to_string(@target_entity_type)} |
| 21 | + target={@myself} |
| 22 | + items={ |
| 23 | + (@linkable_entities -- @linked_entities) |
| 24 | + |> Enum.map(fn e -> %{id: e.id, name: entity_content(e)} end) |
| 25 | + } |
| 26 | + /> |
| 27 | + <div class="mt-2"> |
| 28 | + <%= for entity <- @linked_entities || [] do %> |
| 29 | + <.button |
| 30 | + phx-click="remove_entity" |
| 31 | + phx-target={@myself} |
| 32 | + phx-value-id={entity.id} |
| 33 | + class="tag-button mt-2" |
| 34 | + > |
| 35 | + <span>{entity_content(entity)}</span> |
| 36 | + <.octicon name="x-16" /> |
| 37 | + </.button> |
| 38 | + <% end %> |
| 39 | + </div> |
| 40 | + </:body> |
| 41 | + <:footer> |
| 42 | + <.button is_primary phx-click="save" phx-target={@myself}> |
| 43 | + {gettext("Save")} |
| 44 | + </.button> |
| 45 | + <.button phx-click={cancel_dialog("linker-modal")}>{gettext("Cancel")}</.button> |
| 46 | + </:footer> |
| 47 | + </.dialog> |
| 48 | + </div> |
| 49 | + """ |
| 50 | + end |
| 51 | + |
| 52 | + @impl true |
| 53 | + def handle_event("remove_entity", %{"id" => id}, socket) do |
| 54 | + entity = Enum.find(socket.assigns.linked_entities, fn t -> t.id == id end) |
| 55 | + |
| 56 | + {:noreply, |
| 57 | + socket |
| 58 | + |> assign(:linked_entities, socket.assigns.linked_entities -- [entity]) |
| 59 | + |> assign(:linkable_entities, [entity | socket.assigns.linkable_entities])} |
| 60 | + end |
| 61 | + |
| 62 | + @impl true |
| 63 | + def handle_event("save", _params, socket) do |
| 64 | + %{ |
| 65 | + entity: entity, |
| 66 | + linked_entities: linked_entities, |
| 67 | + source_entity_type: source_entity_type, |
| 68 | + target_entity_type: target_entity_type |
| 69 | + } = |
| 70 | + socket.assigns |
| 71 | + |
| 72 | + current = get_in(entity, [Access.key!(target_entity_type)]) |
| 73 | + |
| 74 | + to_add = linked_entities -- current |
| 75 | + to_remove = current -- linked_entities |
| 76 | + |
| 77 | + {adder, remover} = |
| 78 | + case {source_entity_type, target_entity_type} do |
| 79 | + {:assumption, :mitigations} -> |
| 80 | + {&Valentine.Composer.add_mitigation_to_assumption/2, |
| 81 | + &Valentine.Composer.remove_mitigation_from_assumption/2} |
| 82 | + |
| 83 | + {:assumption, :threats} -> |
| 84 | + {&Valentine.Composer.add_threat_to_assumption/2, |
| 85 | + &Valentine.Composer.remove_threat_from_assumption/2} |
| 86 | + |
| 87 | + {:mitigation, :assumptions} -> |
| 88 | + {&Valentine.Composer.add_assumption_to_mitigation/2, |
| 89 | + &Valentine.Composer.remove_assumption_from_mitigation/2} |
| 90 | + |
| 91 | + {:mitigation, :threats} -> |
| 92 | + {&Valentine.Composer.add_threat_to_mitigation/2, |
| 93 | + &Valentine.Composer.remove_threat_from_mitigation/2} |
| 94 | + |
| 95 | + {:threat, :assumptions} -> |
| 96 | + {&Valentine.Composer.add_assumption_to_threat/2, |
| 97 | + &Valentine.Composer.remove_assumption_from_threat/2} |
| 98 | + |
| 99 | + {:threat, :mitigations} -> |
| 100 | + {&Valentine.Composer.add_mitigation_to_threat/2, |
| 101 | + &Valentine.Composer.remove_mitigation_from_threat/2} |
| 102 | + |
| 103 | + {_, _} -> |
| 104 | + {nil, nil} |
| 105 | + end |
| 106 | + |
| 107 | + if adder && remover do |
| 108 | + to_add |
| 109 | + |> Enum.each(fn e -> apply(adder, [entity, e]) end) |
| 110 | + |
| 111 | + to_remove |
| 112 | + |> Enum.each(fn e -> apply(remover, [entity, e]) end) |
| 113 | + |
| 114 | + send(self(), {__MODULE__, {:saved, entity}}) |
| 115 | + end |
| 116 | + |
| 117 | + {:noreply, |
| 118 | + socket |
| 119 | + |> put_flash( |
| 120 | + :info, |
| 121 | + gettext("Linked %{source_entity_type} updated", |
| 122 | + source_entity_type: Atom.to_string(source_entity_type) |
| 123 | + ) |
| 124 | + ) |
| 125 | + |> push_patch(to: socket.assigns.patch)} |
| 126 | + end |
| 127 | + |
| 128 | + @impl true |
| 129 | + def update(%{selected_item: %{id: id}}, socket) do |
| 130 | + entity = Enum.find(socket.assigns.linkable_entities, fn t -> t.id == id end) |
| 131 | + |
| 132 | + {:ok, |
| 133 | + socket |
| 134 | + |> assign(:linked_entities, [entity | socket.assigns.linked_entities]) |
| 135 | + |> assign( |
| 136 | + :linkable_entities, |
| 137 | + socket.assigns.linkable_entities |
| 138 | + |> Enum.reject(fn t -> t.id == id end) |
| 139 | + )} |
| 140 | + end |
| 141 | + |
| 142 | + @impl true |
| 143 | + def update(assigns, socket) do |
| 144 | + {:ok, |
| 145 | + socket |
| 146 | + |> assign(assigns)} |
| 147 | + end |
| 148 | + |
| 149 | + defp entity_content(%Valentine.Composer.Threat{} = threat), |
| 150 | + do: Valentine.Composer.Threat.show_statement(threat) |
| 151 | + |
| 152 | + defp entity_content(entity), do: entity.content |
| 153 | +end |
0 commit comments