2023-06-20 23:10:40 +02:00
|
|
|
defmodule ChiyaWeb.AdminHomeLive do
|
|
|
|
use ChiyaWeb, :live_view
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def mount(_params, _session, socket) do
|
|
|
|
changeset = Chiya.Notes.change_note(%Chiya.Notes.Note{})
|
|
|
|
{:ok, socket |> assign(:form, to_form(changeset))}
|
|
|
|
end
|
|
|
|
|
2023-06-26 23:16:56 +02:00
|
|
|
@impl true
|
2023-06-20 23:10:40 +02:00
|
|
|
def handle_event("validate", %{"note" => params}, socket) do
|
|
|
|
form =
|
|
|
|
%Chiya.Notes.Note{}
|
|
|
|
|> Chiya.Notes.change_note(params)
|
|
|
|
|> Map.put(:action, :insert)
|
|
|
|
|> to_form()
|
|
|
|
|
|
|
|
{:noreply, assign(socket, form: form)}
|
|
|
|
end
|
|
|
|
|
2023-06-26 23:16:56 +02:00
|
|
|
@impl true
|
2023-06-20 23:10:40 +02:00
|
|
|
def handle_event("save", %{"note" => params}, socket) do
|
|
|
|
name = Chiya.Notes.Note.note_title(params["content"])
|
|
|
|
settings = Chiya.Site.get_settings()
|
|
|
|
|
|
|
|
params =
|
|
|
|
params
|
|
|
|
|> Map.put_new("name", name)
|
|
|
|
|> Map.put_new("channels", [settings.home_channel])
|
|
|
|
|> Map.put_new("published_at", NaiveDateTime.local_now())
|
|
|
|
|
|
|
|
case Chiya.Notes.create_note(params) do
|
2023-07-04 06:59:43 +02:00
|
|
|
{:ok, note} ->
|
2023-07-05 06:49:37 +02:00
|
|
|
{:noreply,
|
|
|
|
socket
|
|
|
|
|> put_flash(:info, "Note created!")
|
|
|
|
|> push_navigate(to: ~p"/note/#{note.slug}")}
|
2023-06-20 23:10:40 +02:00
|
|
|
|
|
|
|
{:error, %Ecto.Changeset{} = changeset} ->
|
|
|
|
{:noreply,
|
2023-07-05 06:49:37 +02:00
|
|
|
socket
|
|
|
|
|> put_flash(:error, "Could not create note!")
|
2023-07-04 06:59:43 +02:00
|
|
|
|> assign(form: to_form(changeset))}
|
2023-06-20 23:10:40 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def render(assigns) do
|
|
|
|
~H"""
|
|
|
|
<.header>
|
|
|
|
<.icon name="hero-document-text" /> Welcome back!
|
|
|
|
<:subtitle>This is the admin area</:subtitle>
|
|
|
|
<:actions>
|
|
|
|
<.link href={~p"/user"}>
|
|
|
|
<.button>Profile</.button>
|
|
|
|
</.link>
|
|
|
|
</:actions>
|
|
|
|
</.header>
|
|
|
|
|
|
|
|
<section>
|
|
|
|
<.simple_form for={@form} phx-change="validate" phx-submit="save">
|
|
|
|
<.input field={@form[:content]} type="textarea" />
|
|
|
|
<:actions>
|
2023-07-04 06:59:43 +02:00
|
|
|
<.button>Publish</.button>
|
2023-06-20 23:10:40 +02:00
|
|
|
</:actions>
|
|
|
|
</.simple_form>
|
|
|
|
</section>
|
|
|
|
"""
|
|
|
|
end
|
|
|
|
end
|