Skip to content

Commit

Permalink
Fix departments schema (#305)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarioRodrigues10 authored Aug 11, 2023
1 parent 1edd2a1 commit ea975c3
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
16 changes: 16 additions & 0 deletions lib/atomic/departments.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ defmodule Atomic.Departments do
"""
use Atomic.Context

alias Atomic.Activities.SessionDepartment
alias Atomic.Organizations.Collaborator
alias Atomic.Organizations.Department

Expand Down Expand Up @@ -50,6 +51,21 @@ defmodule Atomic.Departments do
Repo.all(from d in Department, where: d.id in ^ids)
end

@doc """
Returns the list of activity sessions that belong to a department.
## Examples
iex> get_department_sessions(99d7c9e5-4212-4f59-a097-28aaa33c2621)
[%Session{}, ...]
"""
def get_department_sessions(department_id) do
Repo.all(from s in SessionDepartment, where: s.department_id == ^department_id)
|> Repo.preload([:session, session: :activity])
|> Enum.map(& &1.session)
end

@doc """
Gets a single department.
Expand Down
4 changes: 2 additions & 2 deletions lib/atomic/organizations/department.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Atomic.Organizations.Department do
"""
use Atomic.Schema
alias Atomic.Organizations.Organization
alias Atomic.Activities.{Activity, SessionDepartment}
alias Atomic.Activities.{Session, SessionDepartment}

@required_fields ~w(name organization_id)a

Expand All @@ -16,7 +16,7 @@ defmodule Atomic.Organizations.Department do
field :name, :string
field :description, :string

many_to_many :activities, Activity, join_through: SessionDepartment, on_replace: :delete
many_to_many :sessions, Session, join_through: SessionDepartment, on_replace: :delete
belongs_to :organization, Organization, on_replace: :delete_if_exists

timestamps()
Expand Down
4 changes: 3 additions & 1 deletion lib/atomic_web/live/department_live/show.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ defmodule AtomicWeb.DepartmentLive.Show do

@impl true
def handle_params(%{"organization_id" => organization_id, "id" => id}, _, socket) do
department = Departments.get_department!(id, preloads: [:activities])
department = Departments.get_department!(id)
sessions = Departments.get_department_sessions(department.id)
collaborator = Departments.get_collaborator!(socket.assigns.current_user.id, department.id)

entries = [
Expand All @@ -32,6 +33,7 @@ defmodule AtomicWeb.DepartmentLive.Show do
|> assign(:breadcrumb_entries, entries)
|> assign(:page_title, page_title(socket.assigns.live_action, department.name))
|> assign(:department, department)
|> assign(:sessions, sessions)
|> assign(:collaborator, collaborator)
|> assign(
:collaborators,
Expand Down
8 changes: 4 additions & 4 deletions lib/atomic_web/live/department_live/show.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@

<div x-bind:class="! open ? 'hidden' : 'block'" class="flex flex-col border-t border-zinc-200 1.5xl:flex-row">
<ul role="list" class="flex flex-col items-center mt-5 gap-6 lg:flex-row lg:flex-wrap">
<%= for activity <- @department.activities do %>
<%= for session <- @sessions do %>
<li class="w-72 col-span-1 flex flex-col border-2 border-gray-200 rounded-lg bg-white text-center shadow hover:bg-gray-50">
<%= live_patch to: Routes.activity_show_path(@socket, :show, @current_organization, activity), class: "" do %>
<%= live_patch to: Routes.activity_show_path(@socket, :show, @current_organization, session), class: "" do %>
<div class="flex flex-1 flex-col p-4">
<h3 class="mt-6 text-md font-bold font-medium text-gray-900"><%= activity.title %></h3>
<h3 class="mt-6 text-md font-bold font-medium text-gray-900"><%= session.activity.title %></h3>
</div>
<div class="flex flex-col items-center justify-center">
<div class="-mt-px divide-x divide-gray-200">
<%= activity.description %>
<%= session.activity.description %>
</div>
<img src="https://picsum.photos/200/300" alt="random image" class="mt-2 w-full h-32 object-cover rounded-b-lg" />
</div>
Expand Down

0 comments on commit ea975c3

Please sign in to comment.