refactor: embed all html templates into html modules

For some reason I like it.
This commit is contained in:
Ivan R. 2024-08-25 23:58:22 +05:00
parent 1d52d9c71f
commit 818a7e4a31
Signed by: lumin
GPG key ID: E0937DC7CD6D3817
14 changed files with 148 additions and 121 deletions

View file

@ -89,7 +89,6 @@ defmodule ComfycampWeb do
import ComfycampWeb.Flash import ComfycampWeb.Flash
import ComfycampWeb.Gettext import ComfycampWeb.Gettext
import ComfycampWeb.NavBar import ComfycampWeb.NavBar
import ComfycampWeb.Home
# Shortcut for generating JS commands # Shortcut for generating JS commands
alias Phoenix.LiveView.JS alias Phoenix.LiveView.JS

View file

@ -1,37 +0,0 @@
defmodule ComfycampWeb.Home do
@moduledoc """
Components for the home page.
"""
use Phoenix.Component
import ComfycampWeb.Icons
@doc """
A component representing one service, like mastodon or nextcloud.
"""
attr :name, :string, required: true
attr :domain, :string, required: true
attr :description, :string, required: true
attr :enable_link, :boolean, required: false, default: true
attr :learn_more_url, :string, required: false
def service(assigns) do
~H"""
<div class="service">
<h3><%= @name %></h3>
<%= if @enable_link do %>
<a class="link" href={"https://" <> @domain} target="_blank">
<%= @domain %> <.arrow_top_right_on_square_icon />
</a>
<% else %>
<span class="link"><%= @domain %></span>
<% end %>
<p>
<%= @description %>
<%= if assigns[:learn_more_url] do %>
<.link href={@learn_more_url}>Узнать больше</.link>
<% end %>
</p>
</div>
"""
end
end

View file

@ -4,5 +4,9 @@ defmodule ComfycampWeb.CinemaHTML do
""" """
use ComfycampWeb, :html use ComfycampWeb, :html
embed_templates "cinema_html/*" def index(assigns) do
~H"""
<p>Кинотеатр</p>
"""
end
end end

View file

@ -1 +0,0 @@
<p>Кинотеатр</p>

View file

@ -3,6 +3,7 @@ defmodule ComfycampWeb.HomeHTML do
This module contains pages rendered by HomeController. This module contains pages rendered by HomeController.
""" """
use ComfycampWeb, :html use ComfycampWeb, :html
import ComfycampWeb.Icons
def index(assigns) do def index(assigns) do
~H""" ~H"""
@ -70,6 +71,36 @@ defmodule ComfycampWeb.HomeHTML do
""" """
end end
@doc """
A component representing one service, like mastodon or nextcloud.
"""
attr :name, :string, required: true
attr :domain, :string, required: true
attr :description, :string, required: true
attr :enable_link, :boolean, required: false, default: true
attr :learn_more_url, :string, required: false
def service(assigns) do
~H"""
<div class="service">
<h3><%= @name %></h3>
<%= if @enable_link do %>
<a class="link" href={"https://" <> @domain} target="_blank">
<%= @domain %> <.arrow_top_right_on_square_icon />
</a>
<% else %>
<span class="link"><%= @domain %></span>
<% end %>
<p>
<%= @description %>
<%= if assigns[:learn_more_url] do %>
<.link href={@learn_more_url}>Узнать больше</.link>
<% end %>
</p>
</div>
"""
end
def mastodon(assigns) do def mastodon(assigns) do
~H""" ~H"""
<h1>Mastodon</h1> <h1>Mastodon</h1>

View file

@ -1,5 +1,84 @@
defmodule ComfycampWeb.NotesEditorHTML do defmodule ComfycampWeb.NotesEditorHTML do
use ComfycampWeb, :html use ComfycampWeb, :html
embed_templates "notes_editor_html/*" def index(assigns) do
~H"""
<div>
<.link href={~p"/admin/notes/new"}>
Создать заметку
</.link>
<ul>
<%= for note <- @notes do %>
<li>
<.link href={~p"/admin/notes/#{note}"}>
<%= note.title %>
</.link>
<%= note.updated_at %>
</li>
<% end %>
</ul>
</div>
"""
end
def show(assigns) do
~H"""
<div>
<.back navigate={~p"/admin/notes"}>Назад</.back>
<h3><%= @note.title %></h3>
<.link href={~p"/admin/notes/#{@note}/edit"}>
Редактировать
</.link>
<p>Создана: <%= @note.inserted_at %></p>
<p>Обновлена: <%= @note.updated_at %></p>
<pre><%= @note.markdown %></pre>
</div>
"""
end
def new(assigns) do
~H"""
<div>
<h2>Новая заметка</h2>
<.note_form changeset={@changeset} action={~p"/admin/notes"} />
</div>
"""
end
def edit(assigns) do
~H"""
<div>
<h2>Редактировать заметку</h2>
<.note_form changeset={@changeset} action={~p"/admin/notes/#{@changeset.data.id}"} />
</div>
"""
end
def note_form(assigns) do
~H"""
<.simple_form :let={f} for={@changeset} action={@action}>
<.input field={f[:title]} type="text" label="Заголовок" />
<.input field={f[:markdown]} type="textarea" label="Содержание (markdown)" />
<:actions>
<.button>Сохранить</.button>
</:actions>
</.simple_form>
<%= if @changeset.data.id do %>
<.link
href={~p"/admin/notes/#{@changeset.data}"}
method="DELETE"
data-confirm="Вы уверены?"
>
Удалить
</.link>
<% end %>
"""
end
end end

View file

@ -1,4 +0,0 @@
<div>
<h2>Редактировать заметку</h2>
<.note_form changeset={@changeset} action={~p"/admin/notes/#{@changeset.data.id}"} />
</div>

View file

@ -1,17 +0,0 @@
<div>
<.link href={~p"/admin/notes/new"}>
Создать заметку
</.link>
<ul>
<%= for note <- @notes do %>
<li>
<.link href={~p"/admin/notes/#{note}"}>
<%= note.title %>
</.link>
<%= note.updated_at %>
</li>
<% end %>
</ul>
</div>

View file

@ -1,4 +0,0 @@
<div>
<h2>Новая заметка</h2>
<.note_form changeset={@changeset} action={~p"/admin/notes"} />
</div>

View file

@ -1,18 +0,0 @@
<.simple_form :let={f} for={@changeset} action={@action}>
<.input field={f[:title]} type="text" label="Заголовок" />
<.input field={f[:markdown]} type="textarea" label="Содержание (markdown)" />
<:actions>
<.button>Сохранить</.button>
</:actions>
</.simple_form>
<%= if @changeset.data.id do %>
<.link
href={~p"/admin/notes/#{@changeset.data}"}
method="DELETE"
data-confirm="Вы уверены?"
>
Удалить
</.link>
<% end %>

View file

@ -1,13 +0,0 @@
<div>
<.back navigate={~p"/admin/notes"}>Назад</.back>
<h3><%= @note.title %></h3>
<.link href={~p"/admin/notes/#{@note}/edit"}>
Редактировать
</.link>
<p>Создана: <%= @note.inserted_at %></p>
<p>Обновлена: <%= @note.updated_at %></p>
<pre><%= @note.markdown %></pre>
</div>

View file

@ -1,8 +1,38 @@
defmodule ComfycampWeb.NotesHTML do defmodule ComfycampWeb.NotesHTML do
@moduledoc """ @moduledoc """
This module contains pages rendered by BlogController. This module contains pages rendered by NotesController.
""" """
use ComfycampWeb, :html use ComfycampWeb, :html
embed_templates "notes_html/*" def index(assigns) do
~H"""
<div>
<h1>Заметки</h1>
<%= for note <- @notes do %>
<div>
<.link href={~p"/notes/#{note}"}>
<h2><%= note.title %></h2>
</.link>
<%= note.inserted_at %>
</div>
<% end %>
</div>
"""
end
def show(assigns) do
~H"""
<div>
<h1><%= @note.title %></h1>
<.back navigate={~p"/notes"}>К списку заметок</.back>
<p>Создана: <%= @note.inserted_at %></p>
<p>Обновлена: <%= @note.updated_at %></p>
<%= raw(@note_body) %>
</div>
"""
end
end end

View file

@ -1,12 +0,0 @@
<div>
<h1>Заметки</h1>
<%= for note <- @notes do %>
<div>
<.link href={~p"/notes/#{note}"}>
<h2><%= note.title %></h2>
</.link>
<%= note.inserted_at %>
</div>
<% end %>
</div>

View file

@ -1,10 +0,0 @@
<div>
<h1><%= @note.title %></h1>
<.back navigate={~p"/notes"}>К списку заметок</.back>
<p>Создана: <%= @note.inserted_at %></p>
<p>Обновлена: <%= @note.updated_at %></p>
<%= raw(@note_body) %>
</div>