2.6 KiB · text History 6280797
defmodule GitGudWeb.OrgLive.New do
use GitGudWeb, :live_view
alias GitGud.Organizations
alias GitGud.Organizations.Organization
@impl true
def mount(_params, _session, socket) do
{:ok,
socket
|> assign(:page_title, "New organization")
|> assign_form(Organization.changeset(%Organization{}, %{}))}
end
defp assign_form(socket, cs), do: assign(socket, :form, to_form(cs))
@impl true
def handle_event("validate", %{"organization" => attrs}, socket) do
cs = %Organization{} |> Organization.changeset(attrs) |> Map.put(:action, :validate)
{:noreply, assign_form(socket, cs)}
end
def handle_event("save", %{"organization" => attrs}, socket) do
case Organizations.create_organization(socket.assigns.current_scope.user, attrs) do
{:ok, org} ->
{:noreply,
socket
|> put_flash(:info, "Organization created.")
|> push_navigate(to: ~p"/orgs/#{org.handle}")}
{:error, cs} ->
{:noreply, assign_form(socket, cs)}
end
end
@impl true
def render(assigns) do
~H"""
<Layouts.app flash={@flash} current_scope={@current_scope}>
<div class="space-y-4 max-w-lg">
<div>
<.link navigate={~p"/orgs"} class="text-xs link link-hover opacity-60">
← Organizations
</.link>
<h1 class="text-2xl font-semibold mt-1">New organization</h1>
<p class="text-sm opacity-70 mt-1">
You'll be its first admin. A profile repo is created alongside it, so the
org page can carry a README.
</p>
</div>
<.form
for={@form}
id="new-org-form"
phx-change="validate"
phx-submit="save"
class="space-y-3"
>
<.input field={@form[:handle]} label="Handle" required placeholder="acme" />
<.input field={@form[:name]} label="Display name" placeholder="Acme Corp" />
<.input field={@form[:description]} label="Description" />
<.input
field={@form[:visibility]}
type="select"
label="Visibility"
options={[
{"Public — anyone, no login required", "public"},
{"Internal — any signed-in user", "internal"},
{"Private — members only", "private"}
]}
/>
<p class="text-xs opacity-70">
Repos in this org can't be more open than the org itself. Tightening this
later pulls its repos down to match.
</p>
<button type="submit" class="btn btn-primary">Create organization</button>
</.form>
</div>
</Layouts.app>
"""
end
end