2 Commits

  1. 9
      CHANGELOG.md
  2. 1
      assets/css/_forms.scss
  3. 7
      lib/mirage/lists.ex
  4. 4
      lib/mirage/lists/list.ex
  5. 2
      lib/mirage/notes.ex
  6. 4
      lib/mirage/notes/note.ex
  7. 10
      lib/mirage_web/live/blog_live.ex
  8. 37
      lib/mirage_web/live/blog_live.html.leex
  9. 6
      lib/mirage_web/live/list_live/show.ex
  10. 41
      lib/mirage_web/live/list_live/show.html.leex
  11. 5
      lib/mirage_web/live/note_live/form_component.ex
  12. 4
      lib/mirage_web/live/note_live/form_component.html.leex
  13. 12
      lib/mirage_web/live/note_live/show.html.leex
  14. 1
      lib/mirage_web/router.ex
  15. 1
      lib/mirage_web/templates/layout/_header.html.eex
  16. 2
      mix.exs
  17. 9
      priv/repo/migrations/20210310073420_add_list_id_to_note.exs

9
CHANGELOG.md

@ -5,6 +5,15 @@ See [Conventional Commits](Https://conventionalcommits.org) for commit guideline
<!-- changelog -->
## [v0.68.0](https://git.inhji.de/inhji/mirage/compare/v0.67.0...v0.68.0) (2021-03-10)
### Features:
* add blog
## [v0.67.0](https://git.inhji.de/inhji/mirage/compare/v0.66.1...v0.67.0) (2021-03-09)

1
assets/css/_forms.scss

@ -28,6 +28,7 @@ form {
margin-bottom: 0;
}
select,
input[type=text],
input[type=url],
input[type=email],

7
lib/mirage/lists.ex

@ -8,6 +8,8 @@ defmodule Mirage.Lists do
alias Mirage.Lists.List
@list_preloads [notes: [:links, :topics, :backlinks, :list]]
@doc """
Returns the list of lists.
@ -19,6 +21,7 @@ defmodule Mirage.Lists do
"""
def list_lists do
Repo.all(List)
|> Repo.preload(@list_preloads)
end
@doc """
@ -35,7 +38,9 @@ defmodule Mirage.Lists do
** (Ecto.NoResultsError)
"""
def get_list!(id), do: Repo.get!(List, id)
def get_list!(id), do: Repo.get!(List, id) |> Repo.preload(@list_preloads)
def get_list_by_name(name), do: Repo.get_by(List, name: name) |> Repo.preload(@list_preloads)
@doc """
Creates a list.

4
lib/mirage/lists/list.ex

@ -3,8 +3,10 @@ defmodule Mirage.Lists.List do
import Ecto.Changeset
schema "lists" do
field :is_public, :boolean, default: false
field :name, :string
field :is_public, :boolean, default: false
has_many :notes, Mirage.Notes.Note
timestamps()
end

2
lib/mirage/notes.ex

@ -8,7 +8,7 @@ defmodule Mirage.Notes do
alias Mirage.Notes.{Note, NoteNote, NoteTopic, NoteLink}
@note_preloads [:links, :topics, :backlinks]
@note_preloads [:links, :topics, :backlinks, :list]
defmacro contains(content, search_term) do
quote do

4
lib/mirage/notes/note.ex

@ -16,6 +16,8 @@ defmodule Mirage.Notes.Note do
join_through: "notes_notes",
join_keys: [target_id: :id, source_id: :id]
belongs_to :list, Mirage.Lists.List
field :topic_string, :string,
virtual: true,
default: ""
@ -26,7 +28,7 @@ defmodule Mirage.Notes.Note do
@doc false
def changeset(note, attrs) do
note
|> cast(attrs, [:content, :title, :views, :viewed_at])
|> cast(attrs, [:content, :title, :views, :viewed_at, :list_id])
|> validate_required([:content, :title])
|> Mirage.Markdown.maybe_render_markdown(:content, :content_html)
|> Mirage.Tags.maybe_load_tag_string()

10
lib/mirage_web/live/blog_live.ex

@ -0,0 +1,10 @@
defmodule MirageWeb.BlogLive do
use MirageWeb, :live_view
@impl true
def mount(_params, session, socket) do
list = Mirage.Lists.get_list_by_name("@blog")
{:ok, assign(socket, %{list: list}) |> with_user(session)}
end
end

37
lib/mirage_web/live/blog_live.html.leex

@ -0,0 +1,37 @@
<header class="hero">
<h1>Blog</h1>
</header>
<%= if @live_action in [:edit] do %>
<%= live_modal @socket, MirageWeb.ListLive.FormComponent,
id: @list.id,
title: @page_title,
action: @live_action,
list: @list,
return_to: Routes.list_show_path(@socket, :show, @list) %>
<% end %>
<%= if is_nil(@list) do %>
<section class="width-full bg-content">
Looks like you did not create a blog list yet. Just name it <code>@blog</code> and it will show up here.
</section>
<% else %>
<section class="bg-content width-full">
<%= for note <- @list.notes do %>
<article class="note single" id="note-<%= note.id %>">
<%= live_patch to: Routes.note_show_path(MirageWeb.Endpoint, :show, note) do %>
<header class="width-full">
<h2 class="title">
<%= note.title %>
</h2>
<p class="tags">
<%= for topic <- note.topics do %>
<span class="tag"><%= topic.text %></span>
<% end %>
</p>
</header>
<% end %>
</article>
<% end %>
</section>
<% end %>

6
lib/mirage_web/live/list_live/show.ex

@ -4,8 +4,8 @@ defmodule MirageWeb.ListLive.Show do
alias Mirage.Lists
@impl true
def mount(_params, _session, socket) do
{:ok, socket}
def mount(_params, session, socket) do
{:ok, socket |> with_user(session)}
end
@impl true
@ -13,7 +13,7 @@ defmodule MirageWeb.ListLive.Show do
{:noreply,
socket
|> assign(:page_title, page_title(socket.assigns.live_action))
|> assign(:list, Lists.get_list!(id))}
|> assign(:list, Lists.get_list!(id) |> IO.inspect())}
end
defp page_title(:show), do: "Show List"

41
lib/mirage_web/live/list_live/show.html.leex

@ -1,4 +1,6 @@
<h1>Show List</h1>
<header class="hero">
<h1><%= @list.name %></h1>
</header>
<%= if @live_action in [:edit] do %>
<%= live_modal @socket, MirageWeb.ListLive.FormComponent,
@ -9,19 +11,28 @@
return_to: Routes.list_show_path(@socket, :show, @list) %>
<% end %>
<ul>
<section class="bg-content width-full">
<%= for note <- @list.notes do %>
<li>
<strong>Name:</strong>
<%= @list.name %>
</li>
<article class="note single" id="note-<%= note.id %>">
<%= live_patch to: Routes.note_show_path(MirageWeb.Endpoint, :show, note) do %>
<header class="width-full">
<h2 class="title">
<%= note.title %>
</h2>
<p class="tags">
<%= for topic <- note.topics do %>
<span class="tag"><%= topic.text %></span>
<% end %>
</p>
</header>
<% end %>
</article>
<% end %>
</section>
<li>
<strong>Is public:</strong>
<%= @list.is_public %>
</li>
</ul>
<span><%= live_patch "Edit", to: Routes.list_show_path(@socket, :edit, @list), class: "button" %></span>
<span><%= live_redirect "Back", to: Routes.list_index_path(@socket, :index) %></span>
<%= if @current_user do %>
<div class="buttons">
<%= live_patch "Edit", to: Routes.list_show_path(@socket, :edit, @list), class: "button" %>
</div>
<% end %>

5
lib/mirage_web/live/note_live/form_component.ex

@ -8,10 +8,15 @@ defmodule MirageWeb.NoteLive.FormComponent do
def update(%{note: note} = assigns, socket) do
changeset = Notes.change_note(note)
lists =
Mirage.Lists.list_lists()
|> Enum.map(fn l -> [key: l.name, value: l.id] end)
{:ok,
socket
|> assign(assigns)
|> assign(:changeset, changeset)
|> assign(:lists, lists)
|> assign(:tags, Mirage.Tags.list_tags_as_string())}
end

4
lib/mirage_web/live/note_live/form_component.html.leex

@ -20,6 +20,10 @@
<%= error_tag f, :topic_string %>
</fieldset>
<fieldset>
<%= select f, :list_id, @lists, prompt: "Choose a list to publish to" %>
</fieldset>
<div>
<%= submit "Save", phx_disable_with: "Saving..." %>
</div>

12
lib/mirage_web/live/note_live/show.html.leex

@ -15,13 +15,6 @@
<header class="hero">
<h1><span class="id"><%= "##{@note.id}" %></span> <%= @note.title %></h1>
<p><time datetime="<%= @note.inserted_at %>"><%= Timex.from_now(@note.inserted_at) %></time> / <time datetime="<%= @note.updated_at %>"><%= Timex.from_now(@note.updated_at) %></time></p>
<p class="tags">
<%= for topic <- @note.topics do %>
<span class="tag">
<%= link topic.text, to: Routes.topic_path(MirageWeb.Endpoint, :show, topic) %>
</span>
<% end %>
</p>
</header>
<div class="width-full bg-content" phx-update="ignore" id="note-<%= @note.id %>">
@ -33,6 +26,11 @@
<footer class="width-full bg-content">
<p class="tags">
<span class="tag">Views: <%= @note.views %></span>
<%= for topic <- @note.topics do %>
<span class="tag">
<%= link topic.text, to: Routes.topic_path(MirageWeb.Endpoint, :show, topic) %>
</span>
<% end %>
</p>
</footer>
</article>

1
lib/mirage_web/router.ex

@ -27,6 +27,7 @@ defmodule MirageWeb.Router do
pipe_through :browser
live "/", HomeLive, :index
live "/blog", BlogLive, :index
resources "/topics", TopicController

1
lib/mirage_web/templates/layout/_header.html.eex

@ -2,6 +2,7 @@
<nav role="navigation">
<ul>
<%= active_link(@conn, @_s.header_title.value, to: "/", wrap_tag: :li) %>
<%= active_link(@conn, "Blog", to: "/blog", wrap_tag: :li) %>
<li class="spacer"></li>
<%= render "_user_menu.html", assigns %>
</ul>

2
mix.exs

@ -1,7 +1,7 @@
defmodule Mirage.MixProject do
use Mix.Project
@version "0.67.0"
@version "0.68.0"
def project do
[

9
priv/repo/migrations/20210310073420_add_list_id_to_note.exs

@ -0,0 +1,9 @@
defmodule Mirage.Repo.Migrations.AddListIdToNote do
use Ecto.Migration
def change do
alter table(:notes) do
add :list_id, :integer
end
end
end
Loading…
Cancel
Save