create gists

This commit is contained in:
zack 2024-10-26 15:01:33 -04:00
parent 79a17290d5
commit 43a8412f06
No known key found for this signature in database
GPG key ID: 5F873416BCF59F35
90 changed files with 1777 additions and 2107 deletions

View 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

View file

@ -0,0 +1,47 @@
defmodule ZoeyscomputerWeb.GistLive.Index do
use ZoeyscomputerWeb, :live_view
alias Zoeyscomputer.Gists
alias Zoeyscomputer.Gists.Gist
@impl true
def mount(_params, _session, socket) do
{:ok, stream(socket, :gists, Gists.list_gists())}
end
@impl true
def handle_params(params, _url, socket) do
{:noreply, apply_action(socket, socket.assigns.live_action, params)}
end
defp apply_action(socket, :edit, %{"id" => id}) do
socket
|> assign(:page_title, "Edit Gist")
|> assign(:gist, Gists.get_gist!(id))
end
defp apply_action(socket, :new, _params) do
socket
|> assign(:page_title, "New Gist")
|> assign(:gist, %Gist{})
end
defp apply_action(socket, :index, _params) do
socket
|> assign(:page_title, "Listing Gists")
|> assign(:gist, nil)
end
@impl true
def handle_info({ZoeyscomputerWeb.GistLive.FormComponent, {:saved, gist}}, socket) do
{:noreply, stream_insert(socket, :gists, gist)}
end
@impl true
def handle_event("delete", %{"id" => id}, socket) do
gist = Gists.get_gist!(id)
{:ok, _} = Gists.delete_gist(gist)
{:noreply, stream_delete(socket, :gists, gist)}
end
end

View file

@ -0,0 +1,42 @@
<.header>
Listing Gists
<:actions>
<.link patch={~p"/gists/new"}>
<.button>New Gist</.button>
</.link>
</:actions>
</.header>
<.table
id="gists"
rows={@streams.gists}
row_click={fn {_id, gist} -> JS.navigate(~p"/gists/#{gist}") end}
>
<:col :let={{_id, gist}} label="Code"><%= gist.code %></:col>
<:col :let={{_id, gist}} label="Lang"><%= gist.lang %></:col>
<:action :let={{_id, gist}}>
<div class="sr-only">
<.link navigate={~p"/gists/#{gist}"}>Show</.link>
</div>
<.link patch={~p"/gists/#{gist}/edit"}>Edit</.link>
</:action>
<:action :let={{id, gist}}>
<.link
phx-click={JS.push("delete", value: %{id: gist.id}) |> hide("##{id}")}
data-confirm="Are you sure?"
>
Delete
</.link>
</:action>
</.table>
<.modal :if={@live_action in [:new, :edit]} id="gist-modal" show on_cancel={JS.patch(~p"/gists")}>
<.live_component
module={ZoeyscomputerWeb.GistLive.FormComponent}
id={@gist.id || :new}
title={@page_title}
action={@live_action}
gist={@gist}
patch={~p"/gists"}
/>
</.modal>

View file

@ -0,0 +1,21 @@
defmodule ZoeyscomputerWeb.GistLive.Show do
use ZoeyscomputerWeb, :live_view
alias Zoeyscomputer.Gists
@impl true
def mount(_params, _session, socket) do
{:ok, socket}
end
@impl true
def handle_params(%{"id" => id}, _, socket) do
{:noreply,
socket
|> assign(:page_title, page_title(socket.assigns.live_action))
|> assign(:gist, Gists.get_gist!(id))}
end
defp page_title(:show), do: "Show Gist"
defp page_title(:edit), do: "Edit Gist"
end

View file

@ -0,0 +1,27 @@
<.header>
Gist <%= @gist.id %>
<:subtitle>This is a gist record from your database.</:subtitle>
<:actions>
<.link patch={~p"/gists/#{@gist}/show/edit"} phx-click={JS.push_focus()}>
<.button>Edit gist</.button>
</.link>
</:actions>
</.header>
<.list>
<:item title="Code"><%= @gist.code %></:item>
<:item title="Lang"><%= @gist.lang %></:item>
</.list>
<.back navigate={~p"/gists"}>Back to gists</.back>
<.modal :if={@live_action == :edit} id="gist-modal" show on_cancel={JS.patch(~p"/gists/#{@gist}")}>
<.live_component
module={ZoeyscomputerWeb.GistLive.FormComponent}
id={@gist.id}
title={@page_title}
action={@live_action}
gist={@gist}
patch={~p"/gists/#{@gist}"}
/>
</.modal>

View file

@ -1,5 +1,4 @@
defmodule ZoeyscomputerWeb.HomeLive do
alias Hex.API.Key
use ZoeyscomputerWeb, :live_view
def mount(_params, _session, socket) do
@ -40,8 +39,8 @@ defmodule ZoeyscomputerWeb.HomeLive do
def render(assigns) do
~H"""
<div class="container p-4 flex justify-center align-middle">
<div class="border border-ctp-overlay0 rounded-md p-4 flex-col flex items-center">
<div class="container p-8 flex justify-center align-middle">
<div class="border border-ctp-overlay0 rounded-md p-8 flex-col flex items-center">
<h1 class="font-bold text-ctp-mauve">zoey</h1>
<p class="text-ctp-text"><i>Software Engineer 🏳</i></p>
<p class="max-w-96 text-center mt-4 text-ctp-overlay2">

View file

@ -1,82 +0,0 @@
defmodule ZoeyscomputerWeb.LinkLive.FormComponent do
use ZoeyscomputerWeb, :live_component
alias Zoeyscomputer.Links
@impl true
def render(assigns) do
~H"""
<div>
<.header>
<%= @title %>
<:subtitle>Use this form to manage link records in your database.</:subtitle>
</.header>
<.simple_form
for={@form}
id="link-form"
phx-target={@myself}
phx-change="validate"
phx-submit="save"
>
<.input field={@form[:url]} type="text" label="Url" />
<:actions>
<.button phx-disable-with="Saving...">Save Link</.button>
</:actions>
</.simple_form>
</div>
"""
end
@impl true
def update(%{link: link} = assigns, socket) do
{:ok,
socket
|> assign(assigns)
|> assign_new(:form, fn ->
to_form(Links.change_link(link))
end)}
end
@impl true
def handle_event("validate", %{"link" => link_params}, socket) do
changeset = Links.change_link(socket.assigns.link, link_params)
{:noreply, assign(socket, form: to_form(changeset, action: :validate))}
end
def handle_event("save", %{"link" => link_params}, socket) do
save_link(socket, socket.assigns.action, link_params)
end
defp save_link(socket, :edit, link_params) do
case Links.update_link(socket.assigns.link, link_params) do
{:ok, link} ->
notify_parent({:saved, link})
{:noreply,
socket
|> put_flash(:info, "Link updated successfully")
|> push_patch(to: socket.assigns.patch)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, form: to_form(changeset))}
end
end
defp save_link(socket, :new, link_params) do
case Links.create_link(link_params) do
{:ok, link} ->
notify_parent({:saved, link})
{:noreply,
socket
|> put_flash(:info, "Link 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

View file

@ -1,39 +0,0 @@
defmodule ZoeyscomputerWeb.LinkLive.Index do
use ZoeyscomputerWeb, :live_view
alias Zoeyscomputer.Links
def mount(_params, _session, socket) do
user_id = socket.assigns.current_user.id
changeset = Links.Link.changeset(%Links.Link{})
socket =
socket
|> assign(:links, Links.list_links(user_id))
|> assign(:form, to_form(changeset))
{:ok, socket}
end
def handle_event("submit", %{"link" => link_params}, socket) do
params =
link_params
|> Map.put("user_id", socket.assigns.current_user.id)
case Links.create_link(params) do
{:ok, link} ->
socket =
socket
|> assign(:links, [link | socket.assigns.links])
{:noreply, socket}
{:error, changeset} ->
socket
|> assign(:form, to_form(changeset))
{:noreply, socket}
end
end
end

View file

@ -1,30 +0,0 @@
<div class="flex gap-2">
<h1 class="text-2xl grow font-bold">Links</h1>
<.link
navigate={~p"/links/new"}
class="bg-black border border-black hover:bg-gray-700 text-white font-bold py-2 px-3 rounded-md"
>
Add Link
</.link>
</div>
<div class="divide-y">
<div :for={link <- @links}>
<div>
<div class="font-bold"><%= link.url %></div>
<div class="text-sm"><%= link.inserted_at %></div>
</div>
</div>
</div>
<.form for={@form} phx-submit="submit">
<div class="flex gap-2 items-end">
<div class="grow">
<.input field={@form[:url]} type="text" label="url" />
</div>
<button class="bg-black border border-black hover:bg-gray-700 text-white font-bold py-2 px-3 rounded-md">
Create
</button>
</div>
</.form>

View file

@ -1,37 +0,0 @@
defmodule ZoeyscomputerWeb.LinkLive.New do
use ZoeyscomputerWeb, :live_view
alias Zoeyscomputer.Links
def mount(_params, _session, socket) do
changeset = Links.Link.changeset(%Links.Link{})
socket =
socket
|> assign(:form, to_form(changeset))
{:ok, socket}
end
def handle_event("submit", %{"link" => link_params}, socket) do
params =
link_params
|> Map.put("user_id", socket.assigns.current_user.id)
case Links.create_link(params) do
{:ok, _link} ->
socket =
socket
|> put_flash(:info, "Link created successfully")
|> push_navigate(to: ~p"/links")
{:noreply, socket}
{:error, changeset} ->
socket
|> assign(:form, to_form(changeset))
{:noreply, socket}
end
end
end

View file

@ -1,12 +0,0 @@
<h1 class="text-2xl grow font-bold mb-6">Create a new link</h1>
<.form for={@form} phx-submit="submit">
<div class="flex gap-2 items-end">
<div class="grow">
<.input field={@form[:url]} type="text" label="url" />
</div>
<button class="bg-black border border-black hover:bg-gray-700 text-white font-bold py-2 px-3 rounded-md">
Create
</button>
</div>
</.form>