Skip to content

Commit

Permalink
Add organization show page and other stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
MarioRodrigues10 committed Aug 5, 2023
1 parent 40d6902 commit c6db5e1
Show file tree
Hide file tree
Showing 12 changed files with 274 additions and 72 deletions.
4 changes: 2 additions & 2 deletions lib/atomic/activities.ex
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ defmodule Atomic.Activities do
## Examples
iex> is_participating?(activity, user)
iex> is_participating?(activity_id, user_id)
true
iex> is_participating?(activity, user)
iex> is_participating?(activity_id, user_id)
false
"""
def is_participating?(activity_id, user_id) do
Expand Down
35 changes: 35 additions & 0 deletions lib/atomic/organizations.ex
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,27 @@ defmodule Atomic.Organizations do
|> Repo.preload(preloads)
end

@doc """
Gets a single membership by user id and organization id.
Raises `Ecto.NoResultsError` if the membership does not exist.
## Examples
iex> get_membership_by_userid_and_organization_id!(123, 456)
%membership{}
iex> get_membership_by_userid_and_organization_id!(456, 789)
** (Ecto.NoResultsError)
"""
def get_membership_by_userid_and_organizationid!(user_id, organization_id, preloads \\ []) do
Membership
|> where([m], m.user_id == ^user_id and m.organization_id == ^organization_id)
|> Repo.one!()
|> Repo.preload(preloads)
end

@doc """
Updates an membership.
Expand Down Expand Up @@ -423,4 +444,18 @@ defmodule Atomic.Organizations do
def change_user_organization(%UserOrganization{} = user_organization, attrs \\ %{}) do
UserOrganization.changeset(user_organization, attrs)
end

@doc """
Returns the amount of members in an organization.
## Examples
iex> get_total_organization_members(organization_id)
5
"""
def get_total_organization_members(organization_id) do
from(m in Membership, where: m.organization_id == ^organization_id)
|> Repo.aggregate(:count, :id)
end
end
5 changes: 4 additions & 1 deletion lib/atomic_web/live/activity_live/show.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ defmodule AtomicWeb.ActivityLive.Show do
if organization_id in organizations do
{:noreply,
socket
|> assign(:enrolled?, Activities.is_participating?(activity, socket.assigns.current_user))
|> assign(
:enrolled?,
Activities.is_participating?(activity.id, socket.assigns.current_user.id)
)
|> assign(:page_title, page_title(socket.assigns.live_action))
|> assign(:breadcrumb_entries, entries)
|> assign(:current_page, :activities)
Expand Down
15 changes: 10 additions & 5 deletions lib/atomic_web/live/department_live/index.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,20 @@

<ul role="list" class="mt-5 grid grid-cols-1 gap-6 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4">
<%= for department <- @departments do %>
<li class="col-span-1 flex flex-col border-4 border-gray-200 rounded-lg bg-white text-center shadow hover:bg-gray-50">
<li class="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.department_show_path(@socket, :show, @current_organization, department), class: "" do %>
<div class="flex flex-1 flex-col p-4">
<span class="flex justify-center items-center mr-1.5 w-10 h-10 bg-zinc-500 rounded-xl">
<span class="text-lg font-medium w-full leading-none text-white">
<%= Atomic.Accounts.extract_initials(department.name) %>
<div class="flex-1 flex-col w-full p-4">
<span class="w-full flex items-center justify-center">
<span class="flex justify-center items-center mr-1.5 w-16 h-16 bg-zinc-500 rounded-xl">
<span class="text-lg font-medium w-full text-white">
<%= Atomic.Accounts.extract_initials(department.name) %>
</span>
</span>
</span>
<h3 class="mt-6 text-md font-bold font-medium text-gray-900"><%= department.name %></h3>
<span class="text-md font-semibold text-gray-500">
Descrição do Departamento
</span>
</div>
<div>
<div class="-mt-px flex divide-x divide-gray-200">
Expand Down
2 changes: 1 addition & 1 deletion lib/atomic_web/live/department_live/show.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ defmodule AtomicWeb.DepartmentLive.Show do
route: Routes.department_index_path(socket, :index, organization_id)
},
%{
name: gettext("Department"),
name: gettext("%{name}", name: department.name),
route: Routes.department_show_path(socket, :show, organization_id, id)
}
]
Expand Down
6 changes: 3 additions & 3 deletions lib/atomic_web/live/department_live/show.html.heex
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
<h1>Show Department</h1>

<%= if @live_action in [:edit] do %>
<.modal return_to={Routes.department_show_path(@socket, :show, @current_organization, @department)}>
<.live_component module={AtomicWeb.DepartmentLive.FormComponent} id={@department.id} title={@page_title} action={@live_action} department={@department} return_to={Routes.department_show_path(@socket, :show, @current_organization, @department)} />
Expand All @@ -20,7 +18,9 @@
</div>
</div>
<div class="flex flex-col border-t border-zinc-200 1.5xl:flex-row">
<strong>Activities:</strong>
<h1 class="text-2xl font-bold text-zinc-900 sm:text-3xl">
Latest Activities
</h1>
<ul role="list" class="mt-5 grid grid-cols-1 gap-6 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-3">
<%= for activity <- @department.activities 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">
Expand Down
4 changes: 2 additions & 2 deletions lib/atomic_web/live/home_live/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ defmodule AtomicWeb.HomeLive.Index do
partners =
Partnerships.list_partnerships_by_organization_id(socket.assigns.current_organization.id)

actitivities =
activities =
Activities.list_activities_by_organization_id(socket.assigns.current_organization.id, [])

mode = params["mode"] || "month"
Expand All @@ -38,7 +38,7 @@ defmodule AtomicWeb.HomeLive.Index do
|> assign(:breadcrumb_entries, entries)
|> assign(:current_page, :home)
|> assign(:partners, partners)
|> assign(:activities, actitivities)
|> assign(:activities, activities)
|> assign(:time_zone, socket.assigns.time_zone)
|> assign(:params, params)
|> assign(:mode, mode)
Expand Down
6 changes: 3 additions & 3 deletions lib/atomic_web/live/home_live/index.html.heex
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="flex flex-col py-8">
<div class="flex flex-row items-center border-b-2 border-gray-200">
<img src={Card.url({@current_organization.card_image, @current_organization}, :original)} class="mb-8 w-96 h-full hidden lg:block " />
<div class="flex flex-col items-center border-b-2 border-gray-200 lg:flex-row">
<img src={Card.url({@current_organization.card_image, @current_organization}, :original)} class="mb-8 w-96 h-full block " />
<div class="flex flex-col px-8">
<h2 class="text-xl font-bold leading-7 text-zinc-900 w-fit sm:truncate sm:text-4xl">
<%= @current_organization.name %>
Expand Down Expand Up @@ -52,7 +52,7 @@
</div>
<ul class="">
<%= for partner <- Enum.take(@partners,5) do %>
<li class="w-full items-center justify-between border-b-2 border-gray-200 gap-x-6 py-5">
<li class="w-full items-center justify-between border-b-2 border-gray-200 gap-x-6 py-2">
<div class="">
<div class="flex flex-col">
<div class="flex flex-row items-center">
Expand Down
72 changes: 65 additions & 7 deletions lib/atomic_web/live/organization_live/show.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ defmodule AtomicWeb.OrganizationLive.Show do
use AtomicWeb, :live_view

alias Atomic.Accounts
alias Atomic.Activities
alias Atomic.Organizations
alias Atomic.Uploaders.Card

import AtomicWeb.Components.Calendar
import AtomicWeb.CalendarUtils

@impl true
def mount(_params, session, socket) do
Expand All @@ -13,8 +18,10 @@ defmodule AtomicWeb.OrganizationLive.Show do

@impl true
def handle_params(%{"organization_id" => id}, _, socket) do
org = Organizations.get_organization!(id, [:departments])
organization = Organizations.get_organization!(id, [:departments])
user = socket.assigns.current_user
activities = Activities.list_activities_by_organization_id(id, [])
departments = organization.departments

if user.default_organization_id != id do
Accounts.update_user(user, %{"default_organization_id" => id})
Expand All @@ -26,18 +33,26 @@ defmodule AtomicWeb.OrganizationLive.Show do
route: Routes.organization_index_path(socket, :index)
},
%{
name: gettext("Show Organization"),
name: gettext("%{name}", name: organization.name),
route: Routes.organization_show_path(socket, :show, id)
}
]

mode = "month"

{:noreply,
socket
|> assign(:page_title, page_title(socket.assigns.live_action))
|> assign(:organization, org)
|> assign(:page_title, page_title(socket.assigns.live_action, organization.name))
|> assign(:time_zone, socket.assigns.time_zone)
|> assign(:mode, mode)
|> assign(:params, %{})
|> assign(:organization, organization)
|> assign(:activities, activities)
|> assign(:breadcrumb_entries, entries)
|> assign(:current_page, :organizations)
|> assign(:following, Organizations.is_member_of?(socket.assigns.current_user, org))}
|> assign(:departments, departments)
|> assign(:following, Organizations.is_member_of?(socket.assigns.current_user, organization))
|> assign(list_activities(socket.assigns.time_zone, mode, %{}, socket.assigns.current_user))}
end

@impl true
Expand All @@ -61,6 +76,49 @@ defmodule AtomicWeb.OrganizationLive.Show do
end
end

defp page_title(:show), do: "Show Organization"
defp page_title(:edit), do: "Edit Organization"
@impl true
def handle_event("unfollow", _payload, socket) do
membership =
Organizations.get_membership_by_userid_and_organizationid!(
socket.assigns.current_user.id,
socket.assigns.organization.id,
[]
)

case Organizations.delete_membership(membership) do
{:ok, _organization} ->
{:noreply,
socket
|> put_flash(:success, "Stopped following " <> socket.assigns.organization.name)
|> push_redirect(to: Routes.organization_index_path(socket, :index))}

{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, :changeset, changeset)}
end
end

defp list_activities(time_zone, mode, params, _user) do
current = current_from_params(time_zone, params)

start =
if mode == "month" do
Timex.beginning_of_month(current) |> Timex.to_naive_datetime()
else
Timex.beginning_of_week(current) |> Timex.to_naive_datetime()
end

finish =
if mode == "month" do
Timex.end_of_month(current) |> Timex.to_naive_datetime()
else
Timex.end_of_week(current) |> Timex.to_naive_datetime()
end

%{
sessions: Activities.list_sessions_from_to(start, finish, preloads: [:activity])
}
end

defp page_title(:show, organization), do: "#{organization}"
defp page_title(:edit, organization), do: "Edit #{organization}"
end
Loading

0 comments on commit c6db5e1

Please sign in to comment.