From b31e981b73e7390501f25e2df57bfa2a8fbf7e28 Mon Sep 17 00:00:00 2001 From: Rui Lopes Date: Tue, 10 Sep 2024 12:49:06 +0100 Subject: [PATCH] feat: departments tab --- lib/atomic/departments.ex | 19 ++------- lib/atomic_web/live/department_live/index.ex | 4 +- lib/atomic_web/live/department_live/show.ex | 4 +- .../components/organization_about.ex | 4 +- .../components/organization_departments.ex | 42 +++++++++++++++++++ lib/atomic_web/live/organization_live/show.ex | 2 +- .../live/organization_live/show.html.heex | 12 ++++-- 7 files changed, 61 insertions(+), 26 deletions(-) create mode 100644 lib/atomic_web/live/organization_live/components/organization_departments.ex diff --git a/lib/atomic/departments.ex b/lib/atomic/departments.ex index 1abf710d0..068776b75 100644 --- a/lib/atomic/departments.ex +++ b/lib/atomic/departments.ex @@ -194,19 +194,6 @@ defmodule Atomic.Departments do Repo.all(Collaborator) end - @doc """ - Returns the list of collaborators belonging to an organization. - - ## Examples - - iex> list_collaborators_by_organization_id("99d7c9e5-4212-4f59-a097-28aaa33c2621") - [%Collaborator{}, ...] - - """ - def list_collaborators_by_organization_id(id) do - Repo.all(from p in Collaborator, where: p.organization_id == ^id) - end - @doc """ Gets a single collaborator. @@ -363,14 +350,14 @@ defmodule Atomic.Departments do ## Examples - iex> list_collaborators_by_department_id("99d7c9e5-4212-4f59-a097-28aaa33c2621") + iex> list_department_collaborators(123) [%Collaborator{}, ...] """ - def list_collaborators_by_department_id(id, opts \\ []) do + def list_department_collaborators(id, opts \\ []) do Collaborator - |> apply_filters(opts) |> where([c], c.department_id == ^id) + |> apply_filters(opts) |> Repo.all() end diff --git a/lib/atomic_web/live/department_live/index.ex b/lib/atomic_web/live/department_live/index.ex index a5396b57a..dfb26d9c4 100644 --- a/lib/atomic_web/live/department_live/index.ex +++ b/lib/atomic_web/live/department_live/index.ex @@ -45,7 +45,7 @@ defmodule AtomicWeb.DepartmentLive.Index do |> Enum.map(fn department -> collaborators = department.id - |> Departments.list_collaborators_by_department_id( + |> Departments.list_department_collaborators( preloads: [:user], where: [accepted: true] ) @@ -60,7 +60,7 @@ defmodule AtomicWeb.DepartmentLive.Index do |> Enum.map(fn department -> collaborators = department.id - |> Departments.list_collaborators_by_department_id( + |> Departments.list_department_collaborators( preloads: [:user], where: [accepted: true] ) diff --git a/lib/atomic_web/live/department_live/show.ex b/lib/atomic_web/live/department_live/show.ex index 85f88883c..ee5d70bf9 100644 --- a/lib/atomic_web/live/department_live/show.ex +++ b/lib/atomic_web/live/department_live/show.ex @@ -39,7 +39,7 @@ defmodule AtomicWeb.DepartmentLive.Show do |> assign(list_collaborators(department.id, params, has_permissions)) |> assign( :all_collaborators, - Departments.list_collaborators_by_department_id(department.id, + Departments.list_department_collaborators(department.id, preloads: [:user], where: [accepted: true] ) @@ -73,7 +73,7 @@ defmodule AtomicWeb.DepartmentLive.Show do |> assign(list_collaborators(department.id, params, has_permissions)) |> assign( :all_collaborators, - Departments.list_collaborators_by_department_id(department.id, + Departments.list_department_collaborators(department.id, preloads: [:user], where: [accepted: true] ) diff --git a/lib/atomic_web/live/organization_live/components/organization_about.ex b/lib/atomic_web/live/organization_live/components/organization_about.ex index bea7438c4..bd186fb85 100644 --- a/lib/atomic_web/live/organization_live/components/organization_about.ex +++ b/lib/atomic_web/live/organization_live/components/organization_about.ex @@ -5,11 +5,11 @@ defmodule AtomicWeb.OrganizationLive.Components.OrganizationAbout do alias Atomic.Organizations.Organization alias Atomic.Socials - attr :organization, Organization, required: true, doc: "The organization which about to display" + attr :organization, Organization, required: true, doc: "the organization which about to display" def organization_about(assigns) do ~H""" -
+

<%= gettext("Description") %>

<%= @organization.description %>

diff --git a/lib/atomic_web/live/organization_live/components/organization_departments.ex b/lib/atomic_web/live/organization_live/components/organization_departments.ex new file mode 100644 index 000000000..9c03680df --- /dev/null +++ b/lib/atomic_web/live/organization_live/components/organization_departments.ex @@ -0,0 +1,42 @@ +defmodule AtomicWeb.OrganizationLive.Components.OrganizationDepartments do + @moduledoc """ + Internal organization-related component for displaying its departments. + """ + use AtomicWeb, :component + + alias Atomic.Departments + alias Atomic.Organizations.{Department, Organization} + + # FIXME: This should be a shared component + import AtomicWeb.DepartmentLive.Components.DepartmentCard + + attr :organization, Organization, + required: true, + doc: "the organization which departments to display" + + def organization_departments(assigns) do + ~H""" +
+ <%= for department <- list_departments(@organization) do %> + <.link navigate={Routes.department_show_path(AtomicWeb.Endpoint, :show, @organization, department)}> + <.department_card department={department} collaborators={list_department_collaborators(department)} /> + + <% end %> +
+ """ + end + + defp list_departments(%Organization{} = organization) do + Departments.list_departments_by_organization_id(organization.id, + preloads: [:organization], + where: [archived: false] + ) + end + + defp list_department_collaborators(%Department{} = department) do + Departments.list_department_collaborators(department.id, + preloads: [:user], + where: [accepted: true] + ) + end +end diff --git a/lib/atomic_web/live/organization_live/show.ex b/lib/atomic_web/live/organization_live/show.ex index ada697179..8897753b4 100644 --- a/lib/atomic_web/live/organization_live/show.ex +++ b/lib/atomic_web/live/organization_live/show.ex @@ -3,8 +3,8 @@ defmodule AtomicWeb.OrganizationLive.Show do alias Atomic.{Accounts, Organizations} - import AtomicWeb.OrganizationLive.Components.OrganizationAbout import AtomicWeb.Components.{Gradient, Tabs} + import AtomicWeb.OrganizationLive.Components.{OrganizationAbout, OrganizationDepartments} @impl true def mount(_params, _session, socket) do diff --git a/lib/atomic_web/live/organization_live/show.html.heex b/lib/atomic_web/live/organization_live/show.html.heex index 6aef37887..b151c7828 100644 --- a/lib/atomic_web/live/organization_live/show.html.heex +++ b/lib/atomic_web/live/organization_live/show.html.heex @@ -24,7 +24,7 @@ <%!-- TODO: Maybe show button when there's no current user, but with a must login warning? --%> - <%= if !Organizations.user_following?(@current_user.id, @organization.id) do %> + <%= if Organizations.user_following?(@current_user.id, @organization.id) do %> <.button icon={:star} icon_variant={:solid} color={:white}><%= gettext("Following") %> <% else %> <.button icon={:star}><%= gettext("Follow") %> @@ -70,7 +70,13 @@ -
- <.organization_about organization={@organization} /> +
+
+ <.organization_about organization={@organization} /> +
+ +
+ <.organization_departments organization={@organization} /> +