Compare commits

...

5 Commits

Author SHA1 Message Date
inhji cf3fa19044 Merge pull request 'devel' (#352) from devel into main
Reviewed-on: #352
2023-09-24 11:01:36 +02:00
inhji 94976b3414 Merge branch 'main' into devel 2023-09-24 11:00:33 +02:00
Inhji 644cabd24f Merge remote-tracking branch 'origin/devel' into devel 2023-09-24 11:00:16 +02:00
Inhji 2bb625af23 "improve" note_show_live 2023-09-24 11:00:02 +02:00
Inhji 1b68c68644 add note_edit_live 2023-09-24 10:38:24 +02:00
4 changed files with 162 additions and 16 deletions

View File

@ -586,7 +586,9 @@ defmodule ChiyaWeb.CoreComponents do
<dt class="w-1/4 flex-none text-[0.8125rem] leading-6 text-gray-500 dark:text-gray-300">
<%= item.title %>
</dt>
<dd class="text-sm leading-6 text-gray-700 dark:text-gray-400"><%= render_slot(item) %></dd>
<dd class="text-sm leading-6 text-gray-700 dark:text-gray-400 overflow-auto">
<%= render_slot(item) %>
</dd>
</div>
</dl>
</div>

View File

@ -0,0 +1,132 @@
defmodule ChiyaWeb.NoteEditLive do
use ChiyaWeb, :live_view
def render(assigns) do
~H"""
<.header>
<:actions>
<.link href={~p"/admin/notes/#{@note.id}"}>
<.button><.icon name="hero-arrow-left" /> Back to Note</.button>
</.link>
</:actions>
</.header>
<.simple_form for={@note_form} id="note_form" phx-change="validate_note" phx-submit="update_note">
<header>
<.input
field={@note_form[:name]}
type="text"
class="border-none dark:border-none text-2xl dark:text-2xl"
/>
<.input field={@note_form[:slug]} type="text" class="bg-gray-200 dark:bg-gray-900 font-mono" />
</header>
<section class="grid grid-cols-5 gap-6">
<section class="col-span-5 md:col-span-3">
<.input
field={@note_form[:content]}
type="textarea"
label="Content"
rows="20"
class="font-mono"
/>
</section>
<section class="col-span-5 md:col-span-2 flex flex-col gap-6">
<.input field={@note_form[:published_at]} type="datetime-local" label="Published at" />
<.input
field={@note_form[:kind]}
type="select"
label="Kind"
prompt="Choose a value"
options={Ecto.Enum.values(Chiya.Notes.Note, :kind)}
/>
<.input field={@note_form[:url]} type="text" label="Url" />
<.input
field={@note_form[:tags_string]}
type="text"
label="Tags"
value={tags_to_string(@note.tags)}
/>
<.input
field={@note_form[:channels]}
type="select"
label="Channel"
multiple={true}
options={@channels}
value={@selected_channels}
/>
</section>
</section>
<:actions>
<.button>Save Note</.button>
</:actions>
</.simple_form>
"""
end
def mount(%{"id" => id}, _session, socket) do
note = Chiya.Notes.get_note_preloaded!(id)
changeset = Chiya.Notes.change_note(note)
selected_channels = Enum.map(note.channels, fn c -> c.id end)
{:ok,
socket
|> assign(%{
note_form: to_form(changeset),
note: note,
action: ~p"/admin/notes/#{note}",
channels: to_channel_options(),
selected_channels: selected_channels
})}
end
def handle_event("validate_note", params, socket) do
%{"note" => note_params} = params
note_form =
socket.assigns.note
|> Chiya.Notes.change_note(note_params)
|> Map.put(:action, :validate)
|> to_form()
{:noreply, socket |> assign(:note_form, note_form)}
end
def handle_event("update_note", params, socket) do
%{"note" => note_params} = params
note_params = from_channel_ids(note_params)
note = socket.assigns.note
case Chiya.Notes.update_note(note, note_params) do
{:ok, note} ->
{:noreply,
socket
|> put_flash(:info, "Note updated successfully.")
|> redirect(to: ~p"/admin/notes/#{note}")}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, :note_form, to_form(Map.put(changeset, :action, :update)))}
end
end
def tags_to_string(tags), do: Enum.map_join(tags, ", ", fn t -> t.name end)
defp from_channel_ids(note_params) do
selected_ids = Enum.map(note_params["channels"] || [], &String.to_integer/1)
selected_channels =
Chiya.Channels.list_channels()
|> Enum.filter(fn c -> Enum.member?(selected_ids, c.id) end)
Map.put(note_params, "channels", selected_channels)
end
defp to_channel_options(items \\ nil),
do:
Enum.map(items || Chiya.Channels.list_channels(), fn c ->
{Chiya.Channels.Channel.icon(c) <> " " <> c.name, c.id}
end)
end

View File

@ -37,20 +37,31 @@ defmodule ChiyaWeb.NoteShowLive do
</:actions>
</.header>
<.list>
<:item title="Published at">
<%= pretty_date(@note.published_at) %> <span>(<%= from_now(@note.published_at) %>)</span>
</:item>
<:item title="Channels"><%= note_channels(@note.channels) %></:item>
<:item title="Kind"><%= @note.kind %></:item>
<:item title="Url"><%= @note.url %></:item>
<:item title="Tags"><%= note_tags(@note.tags) %></:item>
<:item title="Links outgoing"><%= note_links(@note.links_from) %></:item>
<:item title="Links incoming"><%= note_links(@note.links_to) %></:item>
<:item title="Embed">
<pre class="p-1 bg-gray-100 text-black rounded select-all">[[<%= @note.slug %>]]</pre>
</:item>
</.list>
<section class="mt-4">
<div class="select-all font-mono bg-white p-1 rounded">[[<%= @note.slug %>]]</div>
</section>
<section class="grid grid-cols-2">
<section class="col-span-1">
<.list>
<:item title="Published at">
<%= pretty_date(@note.published_at) %> <span>(<%= from_now(@note.published_at) %>)</span>
</:item>
<:item title="Channels"><%= note_channels(@note.channels) %></:item>
<:item title="Kind"><%= @note.kind %></:item>
<:item title="Url"><a href={@note.url} target="_blank"><%= @note.url %></a></:item>
<:item title="Tags"><%= note_tags(@note.tags) %></:item>
<:item title="Links outgoing"><%= note_links(@note.links_from) %></:item>
<:item title="Links incoming"><%= note_links(@note.links_to) %></:item>
</.list>
</section>
<section class="col-span-1 p-6">
<section class="prose">
<%= raw(Markdown.render(@note.content)) %>
</section>
</section>
</section>
<.line />

View File

@ -70,7 +70,7 @@ defmodule ChiyaWeb.Router do
live "/", AdminHomeLive, :index
resources "/channels", ChannelController
resources "/notes", NoteController, except: [:show, :index]
resources "/notes", NoteController, except: [:show, :index, :edit]
resources "/settings", SettingController, singleton: true
resources "/identities", IdentityController
resources "/comments", CommentController, only: [:index, :show]
@ -85,6 +85,7 @@ defmodule ChiyaWeb.Router do
live "/notes", NoteListLive, :index
live "/notes/:id", NoteShowLive, :show
live "/notes/:id/edit", NoteEditLive, :edit
get "/notes/:id/raw", NoteController, :raw
get "/notes/:id/publish", NoteController, :publish