defmodule VigilWeb.ProjectLive.New do
@moduledoc "Making a project: a name, its platform, and a team to give it to."
use VigilWeb, :live_view
alias Vigil.Accounts.Scope
alias Vigil.{Organizations, Projects}
alias Vigil.Projects.{Platforms, Project}
@impl true
def mount(_params, _session, socket) do
scope = socket.assigns.current_scope
if Scope.can?(scope, "project:write") do
{:ok,
socket
|> assign(:page_title, "New project")
|> assign(:teams, Organizations.list_teams(scope.organization))
|> assign(:form, to_form(Projects.change_project(%Project{}, %{"platform" => "elixir"})))}
else
{:ok,
socket
|> put_flash(:error, "Ask an admin to make projects.")
|> push_navigate(to: ~p"/orgs/#{scope.organization.slug}")}
end
end
@impl true
def render(assigns) do
~H"""
<Layouts.app flash={@flash} current_scope={@current_scope} active={:projects}>
<div class="shader-glass mx-auto max-w-xl space-y-6">
<.header>
New project
<:subtitle>One project per app or service; its errors land here.</:subtitle>
</.header>
<.form for={@form} id="project-form" phx-change="validate" phx-submit="save" class="space-y-4">
<.input field={@form[:name]} label="Name" placeholder="web-app" required />
<fieldset>
<legend class="mb-2 text-sm font-medium">Platform</legend>
<div class="grid grid-cols-2 gap-2 sm:grid-cols-3" id="platform-picker">
<label
:for={p <- Platforms.all()}
class={[
"flex cursor-pointer items-center gap-2 rounded-box border p-3 text-sm transition hover:border-primary/60",
if(@form[:platform].value == p.id,
do: "border-primary bg-primary/5 font-semibold",
else: "border-base-300"
)
]}
>
<input
type="radio"
name={@form[:platform].name}
value={p.id}
checked={@form[:platform].value == p.id}
class="radio radio-primary radio-xs"
/>
{p.name}
</label>
</div>
</fieldset>
<.input
:if={@teams != []}
type="select"
name="project[team_id]"
id="project-team"
label="Team"
value=""
options={[{"No team", ""} | Enum.map(@teams, &{&1.name, &1.id})]}
/>
<.button class="btn btn-primary w-full" phx-disable-with="Creating…">Create project</.button>
</.form>
</div>
</Layouts.app>
"""
end
@impl true
def handle_event("validate", %{"project" => params}, socket) do
changeset = Projects.change_project(%Project{}, params)
{:noreply, assign(socket, :form, to_form(changeset, action: :validate))}
end
def handle_event("save", %{"project" => params}, socket) do
scope = socket.assigns.current_scope
case Projects.create_project(scope, params) do
{:ok, project} ->
{:noreply,
socket
|> put_flash(:info, "#{project.name} is ready. Point an SDK at its DSN.")
|> push_navigate(to: ~p"/orgs/#{scope.organization.slug}/projects/#{project.slug}/setup")}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, :form, to_form(changeset))}
{:error, :forbidden} ->
{:noreply, put_flash(socket, :error, "Ask an admin to make projects.")}
end
end
end
neiam
/vigil
public · Issues · Pulls · Labels · Forks · Compare · Actions success · Badges · Packages
⭐
Log in to mark this repository.
3.5 KiB · text
History
11accb3