3.5 KiB · text History 573fef1
defmodule VigilWeb.OrgLive.Index do
  @moduledoc "Your organizations, and making a new one (you become its owner)."
  use VigilWeb, :live_view

  alias Vigil.Organizations
  alias Vigil.Organizations.Roles

  @impl true
  def mount(_params, _session, socket) do
    memberships = Organizations.list_memberships(socket.assigns.current_scope.user)

    {:ok,
     socket
     |> assign(:page_title, "Organizations")
     |> stream(:memberships, memberships)
     |> assign(:form, to_form(Organizations.change_new_organization()))}
  end

  @impl true
  def render(assigns) do
    ~H"""
    <Layouts.app flash={@flash} current_scope={@current_scope} active={:orgs}>
      <.header>
        Organizations
        <:subtitle>Projects live in organizations. Pick one, or start a new one.</:subtitle>
      </.header>

      <ul id="org-list" phx-update="stream" class="grid gap-3 sm:grid-cols-2">
        <li
          id="org-list-empty"
          class="hidden rounded-box border border-dashed border-base-300 p-8 text-center text-sm text-base-content/60 only:block sm:col-span-2"
        >
          You're not in any organization yet. Make one below, or open an invite you were sent.
        </li>
        <li :for={{dom_id, m} <- @streams.memberships} id={dom_id}>
          <.link
            navigate={~p"/orgs/#{m.organization.slug}"}
            class="group flex items-center gap-3 rounded-box border border-base-300 bg-base-100 p-4 transition hover:-translate-y-0.5 hover:border-primary/50 hover:shadow-md"
          >
            <span class="grid size-10 shrink-0 place-items-center rounded-lg bg-primary/10 font-bold text-primary transition-transform group-hover:-rotate-6">
              {m.organization.name |> String.first() |> String.upcase()}
            </span>
            <div class="min-w-0 flex-1">
              <p class="truncate font-semibold">{m.organization.name}</p>
              <p class="font-mono text-xs text-base-content/60">{m.organization.slug}</p>
            </div>
            <span class="badge badge-ghost badge-sm">{Roles.label(m.role)}</span>
          </.link>
        </li>
      </ul>

      <section class="mx-auto mt-10 max-w-md rounded-box border border-base-300 bg-base-200/60 p-5">
        <h2 class="mb-3 font-semibold">New organization</h2>
        <.form for={@form} id="org-form" phx-change="validate" phx-submit="save" class="space-y-1">
          <.input field={@form[:name]} label="Name" placeholder="Acme" required />
          <.input
            field={@form[:slug]}
            label="Slug"
            placeholder="made from the name"
            phx-debounce="300"
          />
          <.button class="btn btn-primary w-full" phx-disable-with="Creating…">
            Create organization
          </.button>
        </.form>
      </section>
    </Layouts.app>
    """
  end

  @impl true
  def handle_event("validate", %{"organization" => params}, socket) do
    changeset = Organizations.change_new_organization(params)
    {:noreply, assign(socket, :form, to_form(changeset, action: :validate))}
  end

  def handle_event("save", %{"organization" => params}, socket) do
    case Organizations.create_organization(socket.assigns.current_scope.user, params) do
      {:ok, org} ->
        {:noreply,
         socket
         |> put_flash(:info, "#{org.name} is ready. Make its first project.")
         |> push_navigate(to: ~p"/orgs/#{org.slug}/projects/new")}

      {:error, changeset} ->
        {:noreply, assign(socket, :form, to_form(changeset))}
    end
  end
end