From 8d57cd64789af107c6532ac237047650efa94544 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rio=20Rodrigues?= Date: Mon, 7 Aug 2023 23:41:03 +0100 Subject: [PATCH] Change organization with live component --- lib/atomic_web/components/organizations.ex | 68 +++++++++++++++++++ lib/atomic_web/router.ex | 2 +- .../templates/layout/live.html.heex | 34 ++-------- 3 files changed, 73 insertions(+), 31 deletions(-) create mode 100644 lib/atomic_web/components/organizations.ex diff --git a/lib/atomic_web/components/organizations.ex b/lib/atomic_web/components/organizations.ex new file mode 100644 index 000000000..bb9a95e2b --- /dev/null +++ b/lib/atomic_web/components/organizations.ex @@ -0,0 +1,68 @@ +defmodule AtomicWeb.Components.Organizations do + @moduledoc """ + This component is used to render the list of organizations in the sidebar. + """ + use Phoenix.LiveComponent + + alias Atomic.Accounts + alias Atomic.Organizations + alias Atomic.Uploaders.Logo + + def render( + %{ + current_user: current_user, + current_organization: current_organization + } = assigns + ) do + ~H""" +
+ <%= for organization <- Accounts.get_user_organizations(current_user) do %> + <%= if organization.id != current_organization.id do %> +
+ + + + <%= if organization.logo do %> + + <% else %> + + + <%= Atomic.Accounts.extract_initials(organization.name) %> + + + <% end %> + + + + <%= organization.name %> + + + <%= AtomicWeb.ViewUtils.capitalize_first_letter(Atomic.Organizations.get_role(current_user.id, organization.id)) %> + + + + + +
+ <% end %> + <% end %> +
+ """ + end + + def handle_event("default_organization", %{"organization_id" => organization_id}, socket) do + organization = Organizations.get_organization!(organization_id) + user = socket.assigns.current_user + + case Accounts.update_user(user, %{default_organization_id: organization.id}) do + {:ok, _} -> + {:noreply, + socket + |> assign(:current_organization, organization) + |> redirect(to: "/")} + + {:error, _} -> + {:noreply, socket} + end + end +end diff --git a/lib/atomic_web/router.ex b/lib/atomic_web/router.ex index 31be90739..9a0985e41 100644 --- a/lib/atomic_web/router.ex +++ b/lib/atomic_web/router.ex @@ -33,6 +33,7 @@ defmodule AtomicWeb.Router do pipe_through :browser live_session :general, on_mount: [{AtomicWeb.Hooks, :general_user_state}] do + live "/", HomeLive.Index, :index live "/organizations", OrganizationLive.Index, :index live "/organizations/:organization_id", OrganizationLive.Show, :show @@ -47,7 +48,6 @@ defmodule AtomicWeb.Router do pipe_through [:browser, :require_authenticated_user] live_session :logged_in, on_mount: [{AtomicWeb.Hooks, :authenticated_user_state}] do - live "/", HomeLive.Index, :index live "/scanner", ScannerLive.Index, :index live "/calendar", CalendarLive.Show, :show diff --git a/lib/atomic_web/templates/layout/live.html.heex b/lib/atomic_web/templates/layout/live.html.heex index 1a17c5701..6197c615a 100644 --- a/lib/atomic_web/templates/layout/live.html.heex +++ b/lib/atomic_web/templates/layout/live.html.heex @@ -47,6 +47,9 @@
- <%= for organization <- Atomic.Accounts.get_user_organizations(@current_user) do %> - <%= if organization.id != @current_organization.id do %> -
- - - - <%= if organization.logo do %> - - <% else %> - - - <%= Atomic.Accounts.extract_initials(organization.name) %> - - - <% end %> - - - - <%= organization.name %> - - - <%= AtomicWeb.ViewUtils.capitalize_first_letter(Atomic.Organizations.get_role(@current_user.id, organization.id)) %> - - - - - -
- <% end %> - <% end %> + <.live_component module={AtomicWeb.Components.Organizations} id="organizations" current_organization={@current_organization} current_user={@current_user} />