From 4f69414e0672b3aeab7528958e693b69628981c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rio=20Rodrigues?= Date: Wed, 9 Aug 2023 15:10:53 +0100 Subject: [PATCH] Add organization index page --- lib/atomic/organizations.ex | 7 +- .../live/activity_live/show.html.heex | 19 ++--- .../live/department_live/index.html.heex | 27 ++++--- .../live/department_live/show.html.heex | 13 +++- .../live/membership_live/index.html.heex | 4 +- .../live/organization_live/form_component.ex | 5 +- .../form_component.html.heex | 3 - .../live/organization_live/index.ex | 33 +++------ .../live/organization_live/index.html.heex | 73 +++++++++++-------- .../live/organization_live/show.html.heex | 29 ++++++-- lib/atomic_web/live/scanner_live/index.ex | 12 +++ test/atomic/organizations_test.exs | 2 +- 12 files changed, 137 insertions(+), 90 deletions(-) diff --git a/lib/atomic/organizations.ex b/lib/atomic/organizations.ex index caa191208..8053622e7 100644 --- a/lib/atomic/organizations.ex +++ b/lib/atomic/organizations.ex @@ -20,8 +20,11 @@ defmodule Atomic.Organizations do [%Organization{}, ...] """ - def list_organizations do - Repo.all(Organization) + + def list_organizations(opts) do + Organization + |> apply_filters(opts) + |> Repo.all() end @doc """ diff --git a/lib/atomic_web/live/activity_live/show.html.heex b/lib/atomic_web/live/activity_live/show.html.heex index ed1d6603b..f1666b530 100644 --- a/lib/atomic_web/live/activity_live/show.html.heex +++ b/lib/atomic_web/live/activity_live/show.html.heex @@ -167,14 +167,15 @@
<%= if @current_user.role in [:admin] or is_admin?(@current_user, @activity) do %> - - <%= live_patch("Edit", to: Routes.activity_edit_path(@socket, :edit, @activity, @current_organization), class: "button") %> - - - <%= link("Delete", to: "#", phx_click: "delete", phx_value_id: @activity.id, data: [confirm: "Are you sure?"]) %> - + <%= live_patch to: Routes.activity_edit_path(@socket, :edit, @activity, @current_organization), class: "button" do %> + + <% end %> + <%= link to: "#", phx_click: "delete", phx_value_id: @activity.id, data: [confirm: "Are you sure?"] do %> + + <% end %> <% end %> - - <%= live_redirect("Back", to: Routes.activity_index_path(@socket, :index, @current_organization)) %> -
diff --git a/lib/atomic_web/live/department_live/index.html.heex b/lib/atomic_web/live/department_live/index.html.heex index 555862df6..7947f50b4 100644 --- a/lib/atomic_web/live/department_live/index.html.heex +++ b/lib/atomic_web/live/department_live/index.html.heex @@ -49,21 +49,30 @@ <%= if Organizations.get_role(@current_user.id, @current_organization.id) in [:owner, :admin] do %>
-
-
- <%= live_patch("Edit", +
+
+ <%= live_patch( to: Routes.department_index_path(@socket, :edit, @current_organization, department), - class: "relative -mr-px inline-flex w-0 flex-1 items-center justify-center gap-x-3 rounded-bl-lg border border-transparent py-4 text-sm font-semibold text-gray-900 hover:bg-gray-300" - ) %> + class: "w-full relative inline-flex flex-1 items-center justify-center gap-x-3 rounded-bl-lg border border-transparent text-sm font-semibold text-gray-900" + ) do %> + + <% end %>
+
- <%= link("Delete", + <%= link( to: "#", phx_click: "delete", - class: "relative -mr-px inline-flex w-0 flex-1 items-center justify-center gap-x-3 rounded-bl-lg border border-transparent py-4 text-sm font-semibold text-gray-900 hover:bg-gray-300", + class: "w-full relative inline-flex flex-1 items-center justify-center gap-x-3 rounded-bl-lg border border-transparent text-sm font-semibold text-gray-900", phx_value_id: department.id, - data: [confirm: "Are you sure?"] - ) %> + data: [confirm: "Are you sure?"]) + do %> + + <% end %>
diff --git a/lib/atomic_web/live/department_live/show.html.heex b/lib/atomic_web/live/department_live/show.html.heex index b7b5cb8a5..b0523968c 100644 --- a/lib/atomic_web/live/department_live/show.html.heex +++ b/lib/atomic_web/live/department_live/show.html.heex @@ -102,6 +102,13 @@ <%= capitalize_first_letter(collaborator.accepted) %> <%= display_date(collaborator.inserted_at) %> <%= display_time(collaborator.inserted_at) %> <%= display_date(collaborator.updated_at) %> <%= display_time(collaborator.updated_at) %> + + <%= live_patch to: Routes.department_show_path(@socket, :edit, @current_organization, @department), class: "button" do %> + + <% end %> + <% end %> @@ -114,5 +121,9 @@
<%= if Organizations.get_role(@current_user.id, @current_organization.id) in [:owner, :admin] do %> - <%= live_patch("Edit", to: Routes.department_show_path(@socket, :edit, @current_organization, @department), class: "button") %> + <%= live_patch to: Routes.department_show_path(@socket, :edit, @current_organization, @department), class: "button" do %> + + <% end %> <% end %> diff --git a/lib/atomic_web/live/membership_live/index.html.heex b/lib/atomic_web/live/membership_live/index.html.heex index 763cebce9..96955f2a1 100644 --- a/lib/atomic_web/live/membership_live/index.html.heex +++ b/lib/atomic_web/live/membership_live/index.html.heex @@ -38,7 +38,9 @@ <%= display_date(membership.updated_at) %> <%= display_time(membership.updated_at) %> <%= link to: Routes.membership_edit_path(@socket, :edit, membership.organization_id, membership), class: "text-indigo-600 hover:text-indigo-900" do %> - Edit + <% end %> diff --git a/lib/atomic_web/live/organization_live/form_component.ex b/lib/atomic_web/live/organization_live/form_component.ex index af398cb74..1f77f29ff 100644 --- a/lib/atomic_web/live/organization_live/form_component.ex +++ b/lib/atomic_web/live/organization_live/form_component.ex @@ -2,21 +2,18 @@ defmodule AtomicWeb.OrganizationLive.FormComponent do use AtomicWeb, :live_component alias Atomic.Activities - alias Atomic.Departments alias Atomic.Organizations @extensions_whitelist ~w(.svg .jpg .jpeg .gif .png) @impl true def mount(socket) do - departments = Departments.list_departments() speakers = Activities.list_speakers() {:ok, socket |> allow_upload(:card, accept: @extensions_whitelist, max_entries: 1) - |> assign(:speakers, speakers) - |> assign(:departments, departments)} + |> assign(:speakers, speakers)} end @impl true diff --git a/lib/atomic_web/live/organization_live/form_component.html.heex b/lib/atomic_web/live/organization_live/form_component.html.heex index db61c3101..8667e5f0f 100644 --- a/lib/atomic_web/live/organization_live/form_component.html.heex +++ b/lib/atomic_web/live/organization_live/form_component.html.heex @@ -10,9 +10,6 @@ <%= text_input(f, :description) %> <%= error_tag(f, :description) %> - <%= label(f, :departments) %> - <%= multiple_select(f, :departments, Enum.map(@departments, &{&1.name, &1.id}), selected: Enum.map(@departments, & &1.id)) %> -
<%= live_file_input(@uploads.card, class: "hidden") %>
- {:ok, - socket - |> assign(:organizations, list_organizations())} - - token -> - user = Accounts.get_user_by_session_token(token) - - {:ok, - socket - |> assign(:organizations, list_organizations()) - |> assign(:current_user, user) - |> assign( - :current_organization, - session["current_organization"] - )} - end + def mount(_params, _session, socket) do + {:ok, socket} end @impl true def handle_params(params, _url, socket) do + organizations = list_organizations(params) + entries = [ %{ name: gettext("Organizations"), @@ -40,6 +25,8 @@ defmodule AtomicWeb.OrganizationLive.Index do socket |> apply_action(socket.assigns.live_action, params) |> assign(:breadcrumb_entries, entries) + |> assign(:params, params) + |> assign(:organizations, organizations) |> assign(:current_page, :organizations)} end @@ -72,10 +59,10 @@ defmodule AtomicWeb.OrganizationLive.Index do organization = Organizations.get_organization!(id) {:ok, _} = Organizations.delete_organization(organization) - {:noreply, assign(socket, :organizations, list_organizations())} + {:noreply, assign(socket, :organizations, list_organizations(socket.assigns.params))} end - defp list_organizations do - Organizations.list_organizations() + defp list_organizations(params) do + Organizations.list_organizations(params) end end diff --git a/lib/atomic_web/live/organization_live/index.html.heex b/lib/atomic_web/live/organization_live/index.html.heex index 8a22ba3cb..aa33a5181 100644 --- a/lib/atomic_web/live/organization_live/index.html.heex +++ b/lib/atomic_web/live/organization_live/index.html.heex @@ -1,32 +1,47 @@ -<%= if @live_action in [:new, :edit] do %> - <.modal return_to={Routes.organization_index_path(@socket, :index)}> - <.live_component module={AtomicWeb.OrganizationLive.FormComponent} id={@organization || :new} title={@page_title} action={@live_action} organization={@organization} return_to={Routes.organization_index_path(@socket, :index)} /> - -<% end %> +
+
+
+

<%= gettext("Organizations") %>

+ <%= if @current_user.role in [:admin] do %> +
+ <%= live_patch("+ New Organization", to: Routes.organization_index_path(@socket, :new), class: "border-2 rounded-md bg-white text-lg border-orange-500 py-2 px-3.5 text-sm font-medium text-orange-500 shadow-sm hover:bg-orange-500 hover:text-white") %> +
+ <% end %> +
+
- - - - - - - - - - +
    <%= for organization <- @organizations do %> -
- - - - - + <%= live_redirect to: Routes.organization_show_path(@socket, :show, organization) do %> +
  • +
    +
    +
    +
    + <%= if is_nil(Uploaders.Logo.url({organization.logo, organization}, :original)) do %> +
    + + + <%= Atomic.Accounts.extract_initials(organization.name) %> + + +
    + <% else %> +
    + +
    + <% end %> +
    +
    +
    + <%= organization.name %> +
    +
    +
    +
    +
    +
  • + <% end %> <% end %> - -
    NameDescription
    <%= organization.name %><%= organization.description %> - <%= live_patch("Show", to: Routes.organization_show_path(@socket, :show, organization.id)) %> - <%= live_patch("Edit", to: Routes.organization_index_path(@socket, :edit, organization.id)) %> - <%= link("Delete", to: "#", phx_click: "delete", phx_value_id: organization.id, data: [confirm: "Are you sure?"]) %> -
    - -<%= live_patch("New Organization", to: Routes.organization_index_path(@socket, :new)) %> + +
    diff --git a/lib/atomic_web/live/organization_live/show.html.heex b/lib/atomic_web/live/organization_live/show.html.heex index 7183a9e06..ffdfd00c8 100644 --- a/lib/atomic_web/live/organization_live/show.html.heex +++ b/lib/atomic_web/live/organization_live/show.html.heex @@ -17,15 +17,28 @@ <% end %>
    -
    -

    - <%= @organization.name %> - <%= if not @following do %> - - <% else %> - +
    + <%= if Organizations.get_role(@current_user.id, @organization.id) in [:owner, :admin] do %> + <%= live_patch to: Routes.organization_index_path(@socket, :edit, @organization), class: "button" do %> + <% end %> -

    + <%= link to: "#", phx_click: "delete", phx_value_id: @organization.id, data: [confirm: "Are you sure?"] do %> + + <% end %> + <% else %> +

    + <%= @organization.name %> + <%= if not @following do %> + + <% else %> + + <% end %> +

    + <% end %>
    <%= @organization.description %> diff --git a/lib/atomic_web/live/scanner_live/index.ex b/lib/atomic_web/live/scanner_live/index.ex index 6e8d5ab64..e7b8580c0 100644 --- a/lib/atomic_web/live/scanner_live/index.ex +++ b/lib/atomic_web/live/scanner_live/index.ex @@ -26,6 +26,15 @@ defmodule AtomicWeb.ScannerLive.Index do |> assign(:breadcrumb_entries, entries)} end + @doc """ + Handles the scan event. + Basically it does two checks: + 1) Verifies if current_organization is in organizations that are related to the session of the activity. + 2) Verifies if current_user is admin or owner of the organization, or , if current_user is admin of the system. + + If 1) and 2) are true, then confirm_participation is called. + Else a flash message is shown and the user is redirected to the scanner index. + """ @impl true def handle_event("scan", pathname, socket) do [_, session_id, user_id | _] = String.split(pathname, "/") @@ -47,6 +56,9 @@ defmodule AtomicWeb.ScannerLive.Index do end end + # Updates the enrollment of the user in the session, setting present to true. + # If the update is successful, a flash message is shown and the user is redirected to the scanner index. + # Else a flash message is shown and the user is redirected to the scanner index. defp confirm_participation(socket, session_id, user_id) do case Activities.update_enrollment(Activities.get_enrollment!(session_id, user_id), %{ present: true diff --git a/test/atomic/organizations_test.exs b/test/atomic/organizations_test.exs index 12c9320d4..21b05806e 100644 --- a/test/atomic/organizations_test.exs +++ b/test/atomic/organizations_test.exs @@ -13,7 +13,7 @@ defmodule Atomic.OrganizationsTest do organization = insert(:organization) organizations = - Organizations.list_organizations() + Organizations.list_organizations([]) |> Enum.map(& &1.id) assert organizations == [organization.id]