Skip to content

Commit

Permalink
Merge pull request #489 from eyra/fix-share
Browse files Browse the repository at this point in the history
Fixed share view on projects overview page
  • Loading branch information
mellelieuwes authored Nov 28, 2023
2 parents d9ee328 + ec024e3 commit 3519e87
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 103 deletions.
9 changes: 9 additions & 0 deletions core/frameworks/fabric.ex
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ defmodule Fabric do
%Fabric.LiveComponent.Model{ref: child_ref, params: params}
end

# Install

def install_children(%Phoenix.LiveView.Socket{assigns: %{fabric: fabric}} = socket, children)
when is_list(children) do
Phoenix.Component.assign(socket, fabric: install_children(fabric, children))
Expand All @@ -100,6 +102,8 @@ defmodule Fabric do
%Fabric.Model{fabric | children: children}
end

# CRUD

def get_child(%Phoenix.LiveView.Socket{assigns: %{fabric: fabric}}, child_id) do
get_child(fabric, child_id)
end
Expand Down Expand Up @@ -163,6 +167,11 @@ defmodule Fabric do
Phoenix.Component.assign(assigns, fabric: remove_child(fabric, child_id))
end

def show_popup(context, child_id) when is_atom(child_id) do
child = get_child(context, child_id)
show_popup(context, child)
end

def show_popup(
%Phoenix.LiveView.Socket{assigns: assigns} = socket,
%Fabric.LiveComponent.Model{} = child
Expand Down
25 changes: 7 additions & 18 deletions core/frameworks/pixel/components/share_view.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
defmodule Frameworks.Pixel.ShareView do
use CoreWeb, :live_component
use CoreWeb, :live_component_fabric
use Fabric.LiveComponent

alias CoreWeb.UI.UserListItem

Expand Down Expand Up @@ -55,7 +56,7 @@ defmodule Frameworks.Pixel.ShareView do
socket
|> assign(shared_users: [user] ++ shared_users)
|> filter_users()
|> update_parent(%{add: user, content_id: content_id})
|> send_event(:parent, "add_user", %{user: user, content_id: content_id})
else
socket
end
Expand All @@ -74,7 +75,7 @@ defmodule Frameworks.Pixel.ShareView do
socket
|> assign(shared_users: shared_users |> List.delete(user))
|> filter_users()
|> update_parent(%{remove: user, content_id: content_id})
|> send_event(:parent, "remove_user", %{user: user, content_id: content_id})
else
socket
end
Expand All @@ -83,27 +84,14 @@ defmodule Frameworks.Pixel.ShareView do

@impl true
def handle_event("close", _unsigned_params, socket) do
{:noreply, update_parent(socket, :close)}
{:noreply, socket |> send_event(:parent, "finish")}
end

defp update_parent(socket, message) do
send(self(), %{module: __MODULE__, action: message})
socket
end

# data(filtered_users, :list)
# data(close_button, :map)

attr(:content_id, :integer, required: true)
attr(:content_name, :string, required: true)
attr(:group_name, :string, required: true)
attr(:users, :list, required: true)
attr(:shared_users, :list)

@impl true
def render(assigns) do
~H"""
<div class="">
<Frameworks.Pixel.Panel.flat>
<div class="flex flex-row">
<div class="flex-grow">
<div class="text-title5 font-title5 sm:text-title3 sm:font-title3">
Expand Down Expand Up @@ -153,6 +141,7 @@ defmodule Frameworks.Pixel.ShareView do
<% end %>
</div>
</div>
</Frameworks.Pixel.Panel.flat>
</div>
"""
end
Expand Down
4 changes: 2 additions & 2 deletions core/priv/gettext/en/LC_MESSAGES/eyra-project.po
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ msgstr "Projects"

#, elixir-autogen, elixir-format
msgid "share.dialog.content"
msgstr ""
msgstr "project"

#, elixir-autogen, elixir-format
msgid "share.dialog.group"
msgstr ""
msgstr "researchers"

#, elixir-autogen, elixir-format
msgid "config.title"
Expand Down
4 changes: 2 additions & 2 deletions core/priv/gettext/nl/LC_MESSAGES/eyra-project.po
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ msgstr "Projecten"

#, elixir-autogen, elixir-format
msgid "share.dialog.content"
msgstr ""
msgstr "project"

#, elixir-autogen, elixir-format
msgid "share.dialog.group"
msgstr ""
msgstr "onderzoekers"

#, elixir-autogen, elixir-format
msgid "config.title"
Expand Down
9 changes: 4 additions & 5 deletions core/systems/project/form.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
defmodule Systems.Project.Form do
use CoreWeb.LiveForm
use CoreWeb.LiveForm, :fabric
use Fabric.LiveComponent

import CoreWeb.UI.Dialog

Expand All @@ -10,7 +11,7 @@ defmodule Systems.Project.Form do
# Handle initial update
@impl true
def update(
%{id: id, project: project, target: target},
%{id: id, project: project},
socket
) do
changeset = Project.Model.changeset(project, %{})
Expand All @@ -21,7 +22,6 @@ defmodule Systems.Project.Form do
|> assign(
id: id,
project: project,
target: target,
changeset: changeset,
show_errors: false
)
Expand Down Expand Up @@ -96,8 +96,7 @@ defmodule Systems.Project.Form do
end

defp finish(socket) do
send(self(), %{module: __MODULE__, action: :close})
socket
socket |> send_event(:parent, "finish")
end

@impl true
Expand Down
160 changes: 84 additions & 76 deletions core/systems/project/overview_page.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
defmodule Systems.Project.OverviewPage do
use CoreWeb, :live_view
use CoreWeb, :live_view_fabric
use Fabric.LiveView, CoreWeb.Layouts
use CoreWeb.Layouts.Workspace.Component, :projects
use CoreWeb.UI.PlainDialog
use Systems.Observatory.Public
Expand All @@ -17,6 +18,7 @@ defmodule Systems.Project.OverviewPage do
Project
}

@impl true
def mount(_params, _session, %{assigns: %{current_user: user}} = socket) do
{
:ok,
Expand All @@ -40,6 +42,51 @@ defmodule Systems.Project.OverviewPage do
socket |> update_menus()
end

@impl true
def compose(:project_form, %{active_project: project_id, vm: %{projects: projects}}) do
project = Enum.find(projects, &(&1.id == String.to_integer(project_id)))

%{
module: Project.Form,
params: %{
project: project
}
}
end

@impl true
def compose(:share_view, %{active_project: project_id, current_user: user}) do
researchers =
Core.Accounts.list_researchers([:profile])
# filter current user
|> Enum.filter(&(&1.id != user.id))

owners =
project_id
|> String.to_integer()
|> Project.Public.get!()
|> Project.Public.list_owners([:profile])
# filter current user
|> Enum.filter(&(&1.id != user.id))

%{
module: ShareView,
params: %{
content_id: project_id,
content_name: dgettext("eyra-project", "share.dialog.content"),
group_name: dgettext("eyra-project", "share.dialog.group"),
users: researchers,
shared_users: owners
}
}
end

@impl true
def handle_event("show_popup", %{ref: %{id: id, module: module}, params: params}, socket) do
popup = %{module: module, props: Map.put(params, :id, id)}
{:noreply, socket |> assign(popup: popup)}
end

@impl true
def handle_event(
"card_clicked",
Expand All @@ -55,17 +102,15 @@ defmodule Systems.Project.OverviewPage do
def handle_event(
"edit",
%{"item" => project_id},
%{assigns: %{vm: %{projects: projects}}} = socket
socket
) do
project = Enum.find(projects, &(&1.id == String.to_integer(project_id)))

popup = %{
module: Project.Form,
project: project,
target: self()
{
:noreply,
socket
|> assign(active_project: project_id)
|> compose_child(:project_form)
|> show_popup(:project_form)
}

{:noreply, assign(socket, popup: popup)}
end

@impl true
Expand Down Expand Up @@ -105,32 +150,13 @@ defmodule Systems.Project.OverviewPage do
end

@impl true
def handle_event("share", %{"item" => project_id}, %{assigns: %{current_user: user}} = socket) do
researchers =
Core.Accounts.list_researchers([:profile])
# filter current user
|> Enum.filter(&(&1.id != user.id))

owners =
project_id
|> String.to_integer()
|> Project.Public.get!()
|> Project.Public.list_owners([:profile])
# filter current user
|> Enum.filter(&(&1.id != user.id))

popup = %{
module: ShareView,
content_id: project_id,
content_name: dgettext("eyra-project", "share.dialog.content"),
group_name: dgettext("eyra-project", "share.dialog.group"),
users: researchers,
shared_users: owners
}

def handle_event("share", %{"item" => project_id}, socket) do
{
:noreply,
socket |> assign(popup: popup)
socket
|> assign(active_project: project_id)
|> compose_child(:share_view)
|> show_popup(:share_view)
}
end

Expand All @@ -147,6 +173,29 @@ defmodule Systems.Project.OverviewPage do
}
end

@impl true
def handle_event("add_user", %{user: user, content_id: project_id}, socket) do
project_id
|> Project.Public.get!()
|> Project.Public.add_owner!(user)

{:noreply, socket}
end

@impl true
def handle_event("remove_user", %{user: user, content_id: project_id}, socket) do
project_id
|> Project.Public.get!()
|> Project.Public.remove_owner!(user)

{:noreply, socket}
end

@impl true
def handle_event("finish", _, socket) do
{:noreply, socket |> assign(popup: nil)}
end

@impl true
def handle_info(%{auto_save: _status}, socket) do
{
Expand All @@ -172,47 +221,6 @@ defmodule Systems.Project.OverviewPage do
}
end

@impl true
def handle_info(%{module: _, action: :close}, socket) do
{
:noreply,
socket
|> assign(popup: nil)
}
end

@impl true
def handle_info(
%{module: Systems.Project.CreatePopup, action: %{redirect_to: node_id}},
socket
) do
{:noreply, push_redirect(socket, to: ~p"/project/node/#{node_id}")}
end

@impl true
def handle_info(
%{module: Frameworks.Pixel.ShareView, action: %{add: user, content_id: project_id}},
socket
) do
project_id
|> Project.Public.get!()
|> Project.Public.add_owner!(user)

{:noreply, socket}
end

@impl true
def handle_info(
%{module: Frameworks.Pixel.ShareView, action: %{remove: user, content_id: project_id}},
socket
) do
project_id
|> Project.Public.get!()
|> Project.Public.remove_owner!(user)

{:noreply, socket}
end

@impl true
def render(assigns) do
~H"""
Expand All @@ -221,7 +229,7 @@ defmodule Systems.Project.OverviewPage do
<%= if @popup do %>
<.popup>
<div class="mx-10 w-full max-w-popup sm:max-w-popup-sm md:max-w-popup-md lg:max-w-popup-lg">
<.live_component id={:project_overview_popup} module={@popup.module} {@popup} />
<.live_component id={:project_overview_popup} module={@popup.module} {@popup.props} />
</div>
</.popup>
<% end %>
Expand Down

0 comments on commit 3519e87

Please sign in to comment.