-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
394 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
defmodule PolarWeb.Dashboard.Credential.NewLive do | ||
alias PolarWeb.CoreComponents | ||
use PolarWeb, :live_view | ||
|
||
alias Polar.Repo | ||
alias Polar.Accounts | ||
alias Polar.Accounts.Space | ||
|
||
def render(assigns) do | ||
~H""" | ||
<div class="space-y-10 divide-y divide-gray-900/10"> | ||
<div class="grid grid-cols-1 gap-x-8 gap-y-8 md:grid-cols-3"> | ||
<div class="px-4 sm:px-0"> | ||
<h2 class="text-base font-semibold leading-7 text-slate-200"> | ||
<%= gettext("Create a credential") %> | ||
</h2> | ||
<p class="mt-1 text-sm leading-6 text-slate-400"> | ||
<%= gettext("Credentials allow you to control your simplestreams feed.") %> | ||
</p> | ||
</div> | ||
<.simple_form | ||
for={@credential_form} | ||
id="new-credential-form" | ||
phx-submit="save" | ||
phx-change="validate" | ||
class="bg-white shadow-sm ring-1 ring-slate-900/5 sm:rounded-xl md:col-span-2" | ||
> | ||
<div class="px-4 py-6 sm:p-8"> | ||
<div class="grid max-w-2xl grid-cols-1 gap-x-6 gap-y-8 sm:grid-cols-6"> | ||
<div class="sm:col-span-4"> | ||
<.input field={@credential_form[:name]} label={gettext("Name")} required /> | ||
</div> | ||
<div class="col-span-full"> | ||
<div class="flex items-center justify-between"> | ||
<h2 class="text-sm font-medium leading-6 text-gray-900"> | ||
<%= gettext("Expiry days") %> | ||
</h2> | ||
</div> | ||
<fieldset class="mt-2"> | ||
<legend class="sr-only"><%= gettext("Choose expiry policy") %></legend> | ||
<input | ||
type="hidden" | ||
name={@credential_form[:expires_in].name} | ||
value={@credential_form.source.changes[:expires_in]} | ||
/> | ||
<div class="grid grid-cols-3 gap-3 sm:grid-cols-6"> | ||
<label | ||
:for={range <- Space.Credential.expires_in_range()} | ||
for={Phoenix.HTML.Form.input_id(:credential, :expires_in, range.label)} | ||
class={[ | ||
"flex items-center justify-center rounded-md py-3 px-3 text-sm font-semibold sm:flex-1 cursor-pointer focus:outline-none", | ||
"#{if @credential_form.source.changes[:expires_in] == range.value, do: "bg-indigo-600 text-white hover:bg-indigo-500", else: "ring-1 ring-inset ring-slate-300 bg-white text-slate-900 hover:bg-slate-50"}" | ||
]} | ||
> | ||
<.input | ||
type="radio" | ||
id={Phoenix.HTML.Form.input_id(:credential, :expires_in, range.label)} | ||
field={@credential_form[:expires_in]} | ||
value={range.value} | ||
class="sr-only" | ||
/> | ||
<span><%= Phoenix.Naming.humanize(range.label) %></span> | ||
</label> | ||
</div> | ||
</fieldset> | ||
</div> | ||
<div class="col-span-full"> | ||
<div class="flex items-center justify-between"> | ||
<h2 class="text-sm font-medium leading-6 text-gray-900"> | ||
<%= gettext("Type") %> | ||
</h2> | ||
</div> | ||
<fieldset class="mt-2"> | ||
<legend class="sr-only"><%= gettext("Choose credential type") %></legend> | ||
<input | ||
type="hidden" | ||
name={@credential_form[:type].name} | ||
value={@credential_form.source.changes[:type]} | ||
/> | ||
<div class="grid grid-cols-3 gap-3 sm:grid-cols-6"> | ||
<label | ||
:for={type <- Space.Credential.types()} | ||
for={Phoenix.HTML.Form.input_id(:credential, :type, type)} | ||
class={[ | ||
"flex items-center justify-center rounded-md py-3 px-3 text-sm font-semibold sm:flex-1 cursor-pointer focus:outline-none", | ||
"#{if @credential_form.source.changes[:type] == type, do: "bg-indigo-600 text-white hover:bg-indigo-500", else: "ring-1 ring-inset ring-slate-300 bg-white text-slate-900 hover:bg-slate-50"}" | ||
]} | ||
> | ||
<.input | ||
id={Phoenix.HTML.Form.input_id(:credential, :type, type)} | ||
type="radio" | ||
field={@credential_form[:type]} | ||
value={type} | ||
class="sr-only" | ||
/> | ||
<span><%= type %></span> | ||
</label> | ||
</div> | ||
</fieldset> | ||
<.error :for={ | ||
msg <- Enum.map(@credential_form[:type].errors, &CoreComponents.translate_error/1) | ||
}> | ||
<%= msg %> | ||
</.error> | ||
</div> | ||
</div> | ||
</div> | ||
<:actions> | ||
<div class="flex items-center justify-end gap-x-6 border-t border-gray-900/10 px-4 py-4 sm:px-8"> | ||
<.link | ||
navigate={~p"/dashboard/spaces/#{@space.id}"} | ||
class="text-sm font-semibold leading-6 text-gray-900" | ||
> | ||
<%= gettext("Cancel") %> | ||
</.link> | ||
<.button | ||
type="submit" | ||
class="rounded-md bg-indigo-600 px-3 py-2 text-sm font-semibold text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600" | ||
> | ||
<%= gettext("Save") %> | ||
</.button> | ||
</div> | ||
</:actions> | ||
</.simple_form> | ||
</div> | ||
</div> | ||
""" | ||
end | ||
|
||
def mount(%{"space_id" => space_id}, _session, %{assigns: assigns} = socket) do | ||
space = Repo.get_by!(Space, owner_id: assigns.current_user.id, id: space_id) | ||
|
||
credential_form = | ||
to_form(Accounts.change_space_credential(%Space.Credential{space_id: space.id})) | ||
|
||
socket = | ||
socket | ||
|> assign(:credential_form, credential_form) | ||
|> assign(:space, space) | ||
|> assign(:page_title, gettext("New credential")) | ||
|> assign(:current_path, ~p"/dashboard") | ||
|
||
{:ok, socket} | ||
end | ||
|
||
def handle_event("validate", %{"credential" => credential_params}, %{assigns: assigns} = socket) do | ||
credential_form = | ||
assigns.credential_form.source | ||
|> Accounts.change_space_credential(credential_params) | ||
|> Map.put(:action, :validate) | ||
|> to_form() | ||
|
||
socket = | ||
socket | ||
|> assign(:credential_form, credential_form) | ||
|
||
{:noreply, socket} | ||
end | ||
|
||
def handle_event("save", %{"credential" => credential_params}, %{assigns: assigns} = socket) do | ||
case Accounts.create_space_credential(assigns.space, assigns.current_user, credential_params) do | ||
{:ok, credential} -> | ||
socket = | ||
socket | ||
|> put_flash(:info, gettext("Credential successfully created!")) | ||
|> push_navigate( | ||
to: ~p"/dashboard/spaces/#{assigns.space.id}/credentials/#{credential.id}" | ||
) | ||
|
||
{:noreply, socket} | ||
|
||
{:error, %Ecto.Changeset{} = changeset} -> | ||
credential_form = to_form(changeset) | ||
|
||
socket = | ||
socket | ||
|> assign(:check_errors, true) | ||
|> assign(:credential_form, credential_form) | ||
|
||
{:noreply, socket} | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
defmodule PolarWeb.Dashboard.CredentialLive do | ||
use PolarWeb, :live_view | ||
|
||
alias Polar.Repo | ||
alias Polar.Accounts.Space | ||
|
||
@cli_tools %{ | ||
"lxd" => "lxc", | ||
"incus" => "incus" | ||
} | ||
|
||
def render(assigns) do | ||
~H""" | ||
<div class="overflow-hidden bg-white shadow sm:rounded-lg"> | ||
<div class="px-4 py-6 sm:px-6"> | ||
<h3 class="text-base font-semibold leading-7 text-gray-900"> | ||
<%= gettext("Credential Information") %> | ||
</h3> | ||
<p class="mt-1 max-w-2xl text-sm leading-6 text-gray-500"> | ||
<%= gettext("Details about the credential and instructions on how to use it.") %> | ||
</p> | ||
</div> | ||
<div class="border-t border-gray-100"> | ||
<dl class="divide-y divide-gray-100"> | ||
<div class="px-4 py-6 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6"> | ||
<dt class="text-sm font-medium text-gray-900"><%= gettext("Name") %></dt> | ||
<dd class="mt-1 text-sm leading-6 text-gray-700 sm:col-span-2 sm:mt-0"> | ||
<%= @credential.name %> | ||
</dd> | ||
</div> | ||
<div class="px-4 py-6 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6"> | ||
<dt class="text-sm font-medium text-gray-900"><%= gettext("Type") %></dt> | ||
<dd class="mt-1 text-sm leading-6 text-gray-700 sm:col-span-2 sm:mt-0"> | ||
<%= @credential.type %> | ||
</dd> | ||
</div> | ||
<div class="px-4 py-6 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6"> | ||
<dt class="text-sm font-medium text-gray-900"><%= gettext("Expires At") %></dt> | ||
<dd class="mt-1 text-sm leading-6 text-gray-700 sm:col-span-2 sm:mt-0"> | ||
<%= if @credential.expires_at do %> | ||
<%= Calendar.strftime( | ||
@credential.expires_at, | ||
"%d %b %Y" | ||
) %> | ||
<% else %> | ||
<%= gettext("Never Expires") %> | ||
<% end %> | ||
</dd> | ||
</div> | ||
<div class="px-4 py-6 sm:gap-4 sm:px-6"> | ||
<div class="px-4 py-6 sm:gap-4 sm:px-6 font-mono bg-slate-800 text-slate-300 rounded-md"> | ||
<%= Map.fetch!(@cli_tools, @credential.type) %> remote add opsmaru <%= url( | ||
@socket, | ||
~p"/spaces/#{@credential.token}" | ||
) %> | ||
</div> | ||
</div> | ||
</dl> | ||
</div> | ||
</div> | ||
""" | ||
end | ||
|
||
def mount(%{"space_id" => space_id, "id" => id}, _session, %{assigns: assigns} = socket) do | ||
space = Repo.get_by!(Space, owner_id: assigns.current_user.id, id: space_id) | ||
credential = Repo.get_by!(Space.Credential, space_id: space.id, id: id) | ||
|
||
socket = | ||
socket | ||
|> assign(:page_title, credential.name) | ||
|> assign(:current_path, ~p"/dashboard") | ||
|> assign(:space, space) | ||
|> assign(:credential, credential) | ||
|> assign(:cli_tools, @cli_tools) | ||
|
||
{:ok, socket} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
defmodule PolarWeb.Dashboard.Space.DataLoader do | ||
alias Polar.Repo | ||
alias Polar.Accounts.Space | ||
|
||
import Ecto.Query, only: [from: 2] | ||
|
||
def load_credentials(%Space{} = space) do | ||
from(sc in Space.Credential, | ||
where: | ||
sc.space_id == ^space.id and | ||
sc.current_state == ^"active" | ||
) | ||
|> Repo.all() | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.