Skip to content

Commit

Permalink
Check enrollments
Browse files Browse the repository at this point in the history
  • Loading branch information
MarioRodrigues10 committed Aug 12, 2023
1 parent 03c48f1 commit 4a7188a
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 38 deletions.
18 changes: 18 additions & 0 deletions lib/atomic/activities.ex
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,24 @@ defmodule Atomic.Activities do
|> Enum.map(& &1.organization_id)
end

@doc """
Verifies the maximum number of enrollments for an activity session.
## Examples
iex> verify_maximum_enrollments(session_id)
true
iex> verify_maximum_enrollments(session_id)
false
"""
def verify_maximum_enrollments?(session_id) do
session = get_session!(session_id)
total_enrolled = get_total_enrolled(session_id)

session.maximum_entries > total_enrolled
end

@doc """
Verifies if an user is enrolled in an activity session.
Expand Down
2 changes: 1 addition & 1 deletion lib/atomic_web/components/calendar/month.ex
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ defmodule AtomicWeb.Components.CalendarMonth do
<%= for session <- get_date_sessions(@sessions, @date) do %>
<li>
<%= if session.activity do %>
<%= live_patch to: Routes.activity_show_path(AtomicWeb.Endpoint, :show, session.activity, organization), class: "group flex" do %>
<%= live_patch to: Routes.activity_show_path(AtomicWeb.Endpoint, :show, organization, session.id), class: "group flex" do %>
<p class="text-zinc-600 group-hover:text-zinc-800 flex-auto truncate font-medium">
<%= session.activity.title %>
<%!-- <%= if Gettext.get_locale() == "en" do
Expand Down
14 changes: 10 additions & 4 deletions lib/atomic_web/live/activity_live/index.html.heex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div class="inline-block min-w-full align-middle">
<div x-data="{ open: true }" class="inline-block min-w-full align-middle">
<div class="bg-white">
<div class="pt-4 px-4">
<div class="flex items-center justify-between">
Expand Down Expand Up @@ -66,9 +66,15 @@
</div>
</div>
<div class="flex flex-col-reverse lg:flex-row-reverse lg:items-center lg:justify-between">
<div class="mt-2 flex items-center text-sm text-zinc-500 lg:mt-0">
<Heroicons.Solid.bell class="flex-shrink-0 mr-1.5 w-5 h-5 text-zinc-400" /> Closing
</div>
<%= if Activities.verify_maximum_enrollments?(session.id) do %>
<div class="mt-2 flex items-center text-sm text-zinc-500 lg:mt-0">
<Heroicons.Solid.bell class="flex-shrink-0 mr-1.5 w-5 h-5 text-zinc-400" /> Open
</div>
<% else %>
<div class="mt-2 flex items-center text-sm text-zinc-500 lg:mt-0">
<Heroicons.Solid.bell class="flex-shrink-0 mr-1.5 w-5 h-5 text-zinc-400" /> Closed
</div>
<% end %>
<div class="flex flex-row space-x-2">
<%= for speaker <- session.speakers do %>
<div class="mt-2 flex items-center">
Expand Down
30 changes: 18 additions & 12 deletions lib/atomic_web/live/activity_live/show.ex
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ defmodule AtomicWeb.ActivityLive.Show do
|> assign(:breadcrumb_entries, entries)
|> assign(:current_page, :activities)
|> assign(:session, %{session | enrolled: Activities.get_total_enrolled(id)})
|> assign(:max_enrolled?, Activities.verify_maximum_enrollments?(session.id))
|> assign(:activity, activity)}
else
raise MismatchError
Expand All @@ -57,18 +58,23 @@ defmodule AtomicWeb.ActivityLive.Show do
session_id = socket.assigns.id
current_user = socket.assigns.current_user

case Activities.create_enrollment(session_id, current_user) do
{:ok, _enrollment} ->
{:noreply,
socket
|> put_flash(:success, "Enrolled successufully!")
|> set_enrolled(session_id, current_user)}

{:error, _error} ->
{:noreply,
socket
|> put_flash(:error, "Unable to enroll")
|> set_enrolled(session_id, current_user)}
if Activities.verify_maximum_enrollments?(session_id) do
case Activities.create_enrollment(session_id, current_user) do
{:ok, _enrollment} ->
{:noreply,
socket
|> put_flash(:success, "Enrolled successufully!")
|> set_enrolled(session_id, current_user)}

{:error, _error} ->
{:noreply,
socket
|> put_flash(:error, "Unable to enroll")}
end
else
{:noreply,
socket
|> put_flash(:error, "Maximum number of entries reached")}
end
end

Expand Down
28 changes: 15 additions & 13 deletions lib/atomic_web/live/activity_live/show.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -143,19 +143,21 @@
</button>
</dd>
<% else %>
<div class="flex flex-col">
<dt class="text-sm font-medium text-zinc-500">
<%= gettext("You are not enrolled in this activity") %>
</dt>
<button
phx-click="enroll"
phx-disable-with={gettext("Enrolling...")}
phx-throttle="0"
class="w-fit mt-1 rounded-md bg-orange-500 py-2 px-3.5 text-sm font-medium text-white shadow-sm hover:bg-orange-600 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600"
>
<%= gettext("Enroll") %>
</button>
</div>
<%= if @max_enrolled? do %>
<div class="flex flex-col">
<dt class="text-sm font-medium text-zinc-500">
<%= gettext("You are not enrolled in this activity") %>
</dt>
<button
phx-click="enroll"
phx-disable-with={gettext("Enrolling...")}
phx-throttle="0"
class="w-fit mt-1 rounded-md bg-orange-500 py-2 px-3.5 text-sm font-medium text-white shadow-sm hover:bg-orange-600 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600"
>
<%= gettext("Enroll") %>
</button>
</div>
<% end %>
<% end %>
</div>
</dl>
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 session <- @sessions do %>
<%= 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">
<%= live_patch to: Routes.activity_show_path(@socket, :show, @current_organization, session), class: "" do %>
<%= live_patch to: Routes.activity_show_path(@socket, :show, @current_organization, activity), class: "" do %>
<div class="flex flex-1 flex-col p-4">
<h3 class="mt-6 text-md font-bold font-medium text-gray-900"><%= session.activity.title %></h3>
<h3 class="mt-6 text-md font-bold font-medium text-gray-900"><%= activity.title %></h3>
</div>
<div class="flex flex-col items-center justify-center">
<div class="-mt-px divide-x divide-gray-200">
<%= session.activity.description %>
<%= 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
10 changes: 6 additions & 4 deletions priv/repo/seeds/activities.exs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ defmodule Atomic.Repo.Seeds.Activities do
activity_sessions: [
%{
minimum_entries: 0,
maximum_entries: 10,
maximum_entries: Enum.random(10..50),
enrolled: 0,
start: DateTime.from_naive!(~N[2023-04-06 15:00:00], "Etc/UTC"),
finish: DateTime.from_naive!(~N[2023-04-06 17:00:00], "Etc/UTC"),
Expand Down Expand Up @@ -206,14 +206,16 @@ defmodule Atomic.Repo.Seeds.Activities do
end

def seed_enrollments() do
sessions = Repo.all(Session)

case Repo.all(Enrollment) do
[] ->
for session <- Repo.all(Session) do
for user <- Repo.all(User) do
for user <- Repo.all(User) do
for _ <- 1..Enum.random(1..3) do
%Enrollment{}
|> Enrollment.changeset(%{
user_id: user.id,
session_id: session.id,
session_id: Enum.random(sessions).id,
present: Enum.random([true, false])
})
|> Repo.insert!()
Expand Down

0 comments on commit 4a7188a

Please sign in to comment.