Skip to content

Commit

Permalink
feat: add organization tab and useful discover tab
Browse files Browse the repository at this point in the history
  • Loading branch information
joaodiaslobo committed Aug 1, 2024
1 parent 626643c commit 1137a6c
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
34 changes: 34 additions & 0 deletions lib/atomic/activities.ex
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,40 @@ defmodule Atomic.Activities do
|> Flop.validate_and_run(flop, for: Activity)
end

@doc """
Returns the list of activities a user has enrolled in.
## Examples
iex> list_user_activities(user_id)
[%Activity{}, ...]
"""

def list_organization_activities(organization_id, params \\ %{})

def list_organization_activities(organization_id, opts) when is_list(opts) do
from(a in Activity,
where: a.organization_id == ^organization_id
)
|> apply_filters(opts)
|> Repo.all()
end

def list_organization_activities(organization_id, flop) do
from(a in Activity,
where: a.organization_id == ^organization_id
)
|> Flop.validate_and_run(flop, for: Activity)
end

def list_organization_activities(organization_id, %{} = flop, opts) when is_list(opts) do
from(a in Activity,
where: a.organization_id == ^organization_id
)
|> apply_filters(opts)
|> Flop.validate_and_run(flop, for: Activity)
end

@doc """
Returns the count of upcoming activities a user has enrolled in.
Expand Down
21 changes: 19 additions & 2 deletions lib/atomic_web/live/activity_live/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ defmodule AtomicWeb.ActivityLive.Index do
|> assign(:current_tab, current_tab(socket, params))
|> assign(:params, params)
|> assign(:has_permissions?, has_permissions?(socket))
|> assign(:has_current_organization?, has_current_organization?(socket))
|> assign(:upcoming_enrolled_count, user_activities_count(socket))
|> assign(list_activities(socket, params))
|> then(fn complete_socket ->
Expand All @@ -33,15 +34,28 @@ defmodule AtomicWeb.ActivityLive.Index do
params = Map.put(params, "page_size", 6)

case current_tab(socket, params) do
"organization" -> list_organization_activities(socket, params)
"discover" -> list_discover_activities(socket, params)
"following" -> list_following_activities(socket, params)
"enrolled" -> list_enrolled_activities(socket, params)
"past" -> list_past_activities(socket, params)
end
end

defp list_organization_activities(socket, params) do
case Activities.list_organization_activities(socket.assigns.current_organization.id, params,
preloads: [:speakers, :activity_enrollments, :organization]
) do
{:ok, {activities, meta}} ->
%{activities: activities, meta: meta}

{:error, flop} ->
%{activities: [], meta: flop}
end
end

defp list_discover_activities(_socket, params) do
case Activities.list_activities(params,
case Activities.list_upcoming_activities(params,
preloads: [:speakers, :activity_enrollments, :organization]
) do
{:ok, {activities, meta}} ->
Expand Down Expand Up @@ -92,7 +106,10 @@ defmodule AtomicWeb.ActivityLive.Index do
end

defp current_tab(_socket, params) when is_map_key(params, "tab"), do: params["tab"]
defp current_tab(_socket, _params), do: "discover"

defp current_tab(socket, _params) do
if has_current_organization?(socket), do: "organization", else: "discover"
end

defp has_permissions?(socket) when not socket.assigns.is_authenticated?, do: false

Expand Down
9 changes: 9 additions & 0 deletions lib/atomic_web/live/activity_live/index.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@
</:actions>
<!-- Tabs -->
<.tabs class="overflow-scroll scrollbar-hide max-w-5-xl mx-auto px-4 sm:px-6 lg:px-8">
<%= if @is_authenticated? and @has_current_organization? do %>
<.link patch="?tab=organization" replace={false}>
<.tab active={@current_tab == "organization"}>
<.icon name={:globe_alt} class="h-5 w-5 mr-1" />
<%= @current_user.current_organization.name <> gettext("'s activities") %>
</.tab>
</.link>
<% end %>

<.link patch="?tab=discover" replace={false}>
<.tab active={@current_tab == "discover"}>
<.icon name={:globe_alt} class="h-5 w-5 mr-1" />
Expand Down

0 comments on commit 1137a6c

Please sign in to comment.