create gists
This commit is contained in:
parent
79a17290d5
commit
43a8412f06
90 changed files with 1777 additions and 2107 deletions
83
lib/zoeyscomputer_web/live/gist_live/form_component.ex
Normal file
83
lib/zoeyscomputer_web/live/gist_live/form_component.ex
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
defmodule ZoeyscomputerWeb.GistLive.FormComponent do
|
||||
use ZoeyscomputerWeb, :live_component
|
||||
|
||||
alias Zoeyscomputer.Gists
|
||||
|
||||
@impl true
|
||||
def render(assigns) do
|
||||
~H"""
|
||||
<div>
|
||||
<.header>
|
||||
<%= @title %>
|
||||
<:subtitle>Use this form to manage gist records in your database.</:subtitle>
|
||||
</.header>
|
||||
|
||||
<.simple_form
|
||||
for={@form}
|
||||
id="gist-form"
|
||||
phx-target={@myself}
|
||||
phx-change="validate"
|
||||
phx-submit="save"
|
||||
>
|
||||
<.input field={@form[:code]} type="text" label="Code" />
|
||||
<.input field={@form[:lang]} type="text" label="Lang" />
|
||||
<:actions>
|
||||
<.button phx-disable-with="Saving...">Save Gist</.button>
|
||||
</:actions>
|
||||
</.simple_form>
|
||||
</div>
|
||||
"""
|
||||
end
|
||||
|
||||
@impl true
|
||||
def update(%{gist: gist} = assigns, socket) do
|
||||
{:ok,
|
||||
socket
|
||||
|> assign(assigns)
|
||||
|> assign_new(:form, fn ->
|
||||
to_form(Gists.change_gist(gist))
|
||||
end)}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event("validate", %{"gist" => gist_params}, socket) do
|
||||
changeset = Gists.change_gist(socket.assigns.gist, gist_params)
|
||||
{:noreply, assign(socket, form: to_form(changeset, action: :validate))}
|
||||
end
|
||||
|
||||
def handle_event("save", %{"gist" => gist_params}, socket) do
|
||||
save_gist(socket, socket.assigns.action, gist_params)
|
||||
end
|
||||
|
||||
defp save_gist(socket, :edit, gist_params) do
|
||||
case Gists.update_gist(socket.assigns.gist, gist_params) do
|
||||
{:ok, gist} ->
|
||||
notify_parent({:saved, gist})
|
||||
|
||||
{:noreply,
|
||||
socket
|
||||
|> put_flash(:info, "Gist updated successfully")
|
||||
|> push_patch(to: socket.assigns.patch)}
|
||||
|
||||
{:error, %Ecto.Changeset{} = changeset} ->
|
||||
{:noreply, assign(socket, form: to_form(changeset))}
|
||||
end
|
||||
end
|
||||
|
||||
defp save_gist(socket, :new, gist_params) do
|
||||
case Gists.create_gist(gist_params) do
|
||||
{:ok, gist} ->
|
||||
notify_parent({:saved, gist})
|
||||
|
||||
{:noreply,
|
||||
socket
|
||||
|> put_flash(:info, "Gist created successfully")
|
||||
|> push_patch(to: socket.assigns.patch)}
|
||||
|
||||
{:error, %Ecto.Changeset{} = changeset} ->
|
||||
{:noreply, assign(socket, form: to_form(changeset))}
|
||||
end
|
||||
end
|
||||
|
||||
defp notify_parent(msg), do: send(self(), {__MODULE__, msg})
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue