Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hosting and Tenant: Add site selector #100

Merged
merged 10 commits into from
Feb 9, 2024
Merged

Conversation

alexandrexaviersm
Copy link
Collaborator

@alexandrexaviersm alexandrexaviersm commented Jan 26, 2024

#8

  • Created a select input showing all available sites
  • Added an event to redirect the user to another LiveView if they change the sites through the select input.

layouts example:
Screen Shot 2024-01-26 at 7 35 32 PM

error_pages example:
Screen Shot 2024-01-26 at 7 35 56 PM

@AZholtkevych AZholtkevych linked an issue Jan 26, 2024 that may be closed by this pull request
Copy link
Contributor

@leandrocp leandrocp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's working well but I realized we can implement it with less code and make it work for all pages without duplicating the code.

Every page in admin use PageBuilder (https://github.com/BeaconCMS/beacon_live_admin/blob/main/lib/beacon/live_admin/page_builder.ex) which works as a base module to define shared behavior on all pages.

In that module, inside defmacro __using__, you can add a global handle_event:

      @impl true
      def handle_event("change-site", %{"site" => site}, socket) do
        site = String.to_existing_atom(site)
        
        path =
          case String.split(socket.assigns.beacon_page.path, "/") do
            ["", path | _] -> beacon_live_admin_path(socket, site, path)
            _ -> beacon_live_admin_path(socket)
          end

        {:noreply, push_navigate(socket, to: path)}
      end

Now for the frontend part, we also have a module/template that's shared across all pages and that is https://github.com/BeaconCMS/beacon_live_admin/blob/main/lib/beacon/live_admin/page_live.ex and https://github.com/BeaconCMS/beacon_live_admin/blob/main/lib/beacon/live_admin/page_live.html.heex

So in that module you can define that @running_sites assign and then call the <.site_selector> component in page_live.html.heex

By doing so you can remove the duplicated code in each live view (see the suggestions on the file component_editor_live/index.ex

Thanks!

attr :selected_site, :string, default: ""
attr :options, :list, default: []

def site_selector(assigns) do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏻

lib/beacon/live_admin/live/component_editor_live/index.ex Outdated Show resolved Hide resolved
lib/beacon/live_admin/live/component_editor_live/index.ex Outdated Show resolved Hide resolved
lib/beacon/live_admin/live/component_editor_live/index.ex Outdated Show resolved Hide resolved
lib/beacon/live_admin/live/component_editor_live/index.ex Outdated Show resolved Hide resolved
@alexandrexaviersm alexandrexaviersm force-pushed the ax/add_site_selector_on_topbar branch from 4cccb62 to e00b986 Compare February 2, 2024 12:09
@alexandrexaviersm alexandrexaviersm self-assigned this Feb 5, 2024
@leandrocp
Copy link
Contributor

❤️

@leandrocp leandrocp merged commit f696889 into main Feb 9, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Hosting and Tenant: Add site selector on navbar
2 participants