|
|
@ -3,6 +3,8 @@ defmodule LinkShortenerWeb.LinkLive.FormComponent do
|
|
|
|
|
|
|
|
|
|
|
|
alias LinkShortener.Links
|
|
|
|
alias LinkShortener.Links
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
attr :is_checked, :boolean, default: true
|
|
|
|
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
@impl true
|
|
|
|
def render(assigns) do
|
|
|
|
def render(assigns) do
|
|
|
|
~H"""
|
|
|
|
~H"""
|
|
|
@ -21,7 +23,36 @@ defmodule LinkShortenerWeb.LinkLive.FormComponent do
|
|
|
|
>
|
|
|
|
>
|
|
|
|
<.input field={@form[:name]} type="text" label="Name" placeholder="Enter a name here" />
|
|
|
|
<.input field={@form[:name]} type="text" label="Name" placeholder="Enter a name here" />
|
|
|
|
<.input field={@form[:url]} type="text" label="Url" placeholder="Enter an url here" />
|
|
|
|
<.input field={@form[:url]} type="text" label="Url" placeholder="Enter an url here" />
|
|
|
|
<.input field={@form[:shorten]} type="text" label="Shorten" placeholder="Enter a shorten here" />
|
|
|
|
|
|
|
|
|
|
|
|
<%= if @is_checked and @action == :new do %>
|
|
|
|
|
|
|
|
<.label>Shorten</.label>
|
|
|
|
|
|
|
|
<% end %>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<%= unless @is_checked and @action != :edit do %>
|
|
|
|
|
|
|
|
<.input
|
|
|
|
|
|
|
|
field={@form[:shorten]}
|
|
|
|
|
|
|
|
type="text"
|
|
|
|
|
|
|
|
label="Shorten"
|
|
|
|
|
|
|
|
placeholder="Enter a shorten here"
|
|
|
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
<% end %>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<%= if @action == :new do %>
|
|
|
|
|
|
|
|
<label class="inline-flex items-center cursor-pointer">
|
|
|
|
|
|
|
|
<.input
|
|
|
|
|
|
|
|
id="random-toggle"
|
|
|
|
|
|
|
|
field={@form[:toggle]}
|
|
|
|
|
|
|
|
type="checkbox"
|
|
|
|
|
|
|
|
label="Random shorten"
|
|
|
|
|
|
|
|
checked={@is_checked}
|
|
|
|
|
|
|
|
phx-click="toggle"
|
|
|
|
|
|
|
|
phx-target={@myself}
|
|
|
|
|
|
|
|
phx-value-checked={!@is_checked}
|
|
|
|
|
|
|
|
class="peer sr-only"
|
|
|
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
</label>
|
|
|
|
|
|
|
|
<% end %>
|
|
|
|
|
|
|
|
|
|
|
|
<:actions>
|
|
|
|
<:actions>
|
|
|
|
<.button phx-disable-with="Saving...">Save Link</.button>
|
|
|
|
<.button phx-disable-with="Saving...">Save Link</.button>
|
|
|
|
</:actions>
|
|
|
|
</:actions>
|
|
|
@ -47,6 +78,12 @@ defmodule LinkShortenerWeb.LinkLive.FormComponent do
|
|
|
|
{:noreply, assign(socket, form: to_form(changeset, action: :validate))}
|
|
|
|
{:noreply, assign(socket, form: to_form(changeset, action: :validate))}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
|
|
|
|
def handle_event("toggle", params, socket) do
|
|
|
|
|
|
|
|
is_checked = Map.get(params, "value", "false") == "true"
|
|
|
|
|
|
|
|
{:noreply, assign(socket, :is_checked, is_checked)}
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def handle_event("save", %{"link" => link_params}, socket) do
|
|
|
|
def handle_event("save", %{"link" => link_params}, socket) do
|
|
|
|
save_link(socket, socket.assigns.action, link_params)
|
|
|
|
save_link(socket, socket.assigns.action, link_params)
|
|
|
|
end
|
|
|
|
end
|
|
|
@ -67,11 +104,14 @@ defmodule LinkShortenerWeb.LinkLive.FormComponent do
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
defp save_link(socket, :new, link_params) do
|
|
|
|
defp save_link(socket, :new, link_params) do
|
|
|
|
|
|
|
|
{is_random_string, link_params} = Map.pop(link_params, "toggle")
|
|
|
|
|
|
|
|
is_random = is_random_string == "true"
|
|
|
|
|
|
|
|
|
|
|
|
link_params =
|
|
|
|
link_params =
|
|
|
|
link_params
|
|
|
|
link_params
|
|
|
|
|> with_user_id(socket.assigns.current_user)
|
|
|
|
|> with_user_id(socket.assigns.current_user)
|
|
|
|
|
|
|
|
|
|
|
|
case Links.insert_one(link_params) do
|
|
|
|
case create_link(link_params, is_random) do
|
|
|
|
{:ok, link} ->
|
|
|
|
{:ok, link} ->
|
|
|
|
notify_parent({:saved, link})
|
|
|
|
notify_parent({:saved, link})
|
|
|
|
|
|
|
|
|
|
|
@ -85,6 +125,14 @@ defmodule LinkShortenerWeb.LinkLive.FormComponent do
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
defp create_link(link_params, is_shorten_random) do
|
|
|
|
|
|
|
|
if is_shorten_random do
|
|
|
|
|
|
|
|
Links.create_one(link_params, is_atom_based: false)
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
Links.insert_one(link_params)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
defp notify_parent(msg), do: send(self(), {__MODULE__, msg})
|
|
|
|
defp notify_parent(msg), do: send(self(), {__MODULE__, msg})
|
|
|
|
|
|
|
|
|
|
|
|
defp with_user_id(params, current_user) do
|
|
|
|
defp with_user_id(params, current_user) do
|
|
|
|