Skip to content

Commit

Permalink
feat: departments tab
Browse files Browse the repository at this point in the history
  • Loading branch information
ruilopesm committed Sep 10, 2024
1 parent 14ece8d commit b31e981
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 26 deletions.
19 changes: 3 additions & 16 deletions lib/atomic/departments.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions lib/atomic_web/live/department_live/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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]
)
Expand All @@ -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]
)
Expand Down
4 changes: 2 additions & 2 deletions lib/atomic_web/live/department_live/show.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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]
)
Expand Down Expand Up @@ -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]
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
<div class="mt-8 px-4 sm:px-6 lg:px-8">
<div id="organization-about">
<h2 class="mb-2 flex-1 select-none truncate text-lg font-semibold text-zinc-900"><%= gettext("Description") %></h2>
<p><%= @organization.description %></p>
Expand Down
Original file line number Diff line number Diff line change
@@ -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"""
<div id="organization-departments" class="grid grid-cols-1 gap-4 sm:grid-cols-2 md:grid-cols-2">
<%= 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)} />
</.link>
<% end %>
</div>
"""
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
2 changes: 1 addition & 1 deletion lib/atomic_web/live/organization_live/show.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 9 additions & 3 deletions lib/atomic_web/live/organization_live/show.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
</.link>

<%!-- 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") %></.button>
<% else %>
<.button icon={:star}><%= gettext("Follow") %></.button>
Expand Down Expand Up @@ -70,7 +70,13 @@
</.link>
</.tabs>
<div :if={@current_tab == "about"}>
<.organization_about organization={@organization} />
<div class="mt-8 px-4 sm:px-6 lg:px-8">
<div :if={@current_tab == "about"}>
<.organization_about organization={@organization} />
</div>
<div :if={@current_tab == "departments"}>
<.organization_departments organization={@organization} />
</div>
</div>
</.page>

0 comments on commit b31e981

Please sign in to comment.