Merge pull request 'devel' (#140) from devel into main

Reviewed-on: #140
This commit is contained in:
inhji 2023-06-23 07:00:30 +02:00
commit a8cfbbb937
17 changed files with 62 additions and 33 deletions

View file

@ -14,6 +14,10 @@ defmodule Chiya.Tags.TagUpdater do
{:ok, note} {:ok, note}
end end
def update_tags({:error, changeset}, _attrs) do
{:error, changeset}
end
@doc """ @doc """
Updates the tags for the given schema Updates the tags for the given schema

View file

@ -23,21 +23,22 @@ defmodule ChiyaWeb.NoteController do
end end
def new(conn, _params) do def new(conn, _params) do
settings = Chiya.Site.get_settings() default_channels = get_default_channels(conn)
changeset = changeset =
%Note{channels: [settings.default_channel]} %Note{}
|> Notes.preload_note()
|> Notes.change_note() |> Notes.change_note()
render(conn, :new, render(conn, :new,
changeset: changeset, changeset: changeset,
channels: to_channel_options(), channels: to_channel_options(),
selected_channels: default_channels,
tags: [] tags: []
) )
end end
def create(conn, %{"note" => note_params}) do def create(conn, %{"note" => note_params}) do
IO.inspect(note_params)
note_params = from_channel_ids(note_params) note_params = from_channel_ids(note_params)
case Notes.create_note(note_params) do case Notes.create_note(note_params) do
@ -46,8 +47,15 @@ defmodule ChiyaWeb.NoteController do
|> put_flash(:info, "Note created successfully.") |> put_flash(:info, "Note created successfully.")
|> redirect(to: ~p"/admin/notes/#{note}") |> redirect(to: ~p"/admin/notes/#{note}")
# TODO: set channels from changeset when error happened?
{:error, %Ecto.Changeset{} = changeset} -> {:error, %Ecto.Changeset{} = changeset} ->
render(conn, :new, changeset: changeset, channels: to_channel_options(), tags: []) render(conn, :new,
changeset: changeset,
channels: to_channel_options(),
selected_channels: nil,
tags: []
)
end end
end end
@ -60,11 +68,13 @@ defmodule ChiyaWeb.NoteController do
def edit(conn, %{"id" => id}) do def edit(conn, %{"id" => id}) do
note = Notes.get_note_preloaded!(id) note = Notes.get_note_preloaded!(id)
changeset = Notes.change_note(note) changeset = Notes.change_note(note)
selected_channels = Enum.map(note.channels, fn c -> c.id end)
render(conn, :edit, render(conn, :edit,
note: note, note: note,
changeset: changeset, changeset: changeset,
channels: to_channel_options(), channels: to_channel_options(),
selected_channels: selected_channels,
tags: note.tags tags: note.tags
) )
end end
@ -79,11 +89,14 @@ defmodule ChiyaWeb.NoteController do
|> put_flash(:info, "Note updated successfully.") |> put_flash(:info, "Note updated successfully.")
|> redirect(to: ~p"/admin/notes/#{note}") |> redirect(to: ~p"/admin/notes/#{note}")
# TODO: set channels from changeset when error happened?
{:error, %Ecto.Changeset{} = changeset} -> {:error, %Ecto.Changeset{} = changeset} ->
render(conn, :edit, render(conn, :edit,
note: note, note: note,
changeset: changeset, changeset: changeset,
channels: to_channel_options(), channels: to_channel_options(),
selected_channels: nil,
tags: note.tags tags: note.tags
) )
end end
@ -218,6 +231,14 @@ defmodule ChiyaWeb.NoteController do
) )
end end
defp get_default_channels(conn = %Plug.Conn{}) do
if conn.assigns.settings.default_channel do
[conn.assigns.settings.default_channel.id]
else
[]
end
end
defp from_channel_ids(note_params) do defp from_channel_ids(note_params) do
selected_ids = Enum.map(note_params["channels"] || [], &String.to_integer/1) selected_ids = Enum.map(note_params["channels"] || [], &String.to_integer/1)

View file

@ -10,11 +10,9 @@ defmodule ChiyaWeb.NoteHTML do
attr(:action, :string, required: true) attr(:action, :string, required: true)
attr(:channels, :list, required: true) attr(:channels, :list, required: true)
attr(:tags, :list, required: true) attr(:tags, :list, required: true)
attr(:selected_channels, :list, required: true)
def note_form(assigns) def note_form(assigns)
def selected_channels(changeset),
do: Enum.map(changeset.data.channels, fn c -> c.id end)
def tags_to_string(tags), do: Enum.map_join(tags, ", ", fn t -> t.name end) def tags_to_string(tags), do: Enum.map_join(tags, ", ", fn t -> t.name end)
end end

View file

@ -7,6 +7,7 @@
changeset={@changeset} changeset={@changeset}
action={~p"/admin/notes/#{@note}"} action={~p"/admin/notes/#{@note}"}
channels={@channels} channels={@channels}
selected_channels={@selected_channels}
tags={@tags} tags={@tags}
/> />

View file

@ -3,6 +3,11 @@
<:subtitle>Use this form to manage note records in your database.</:subtitle> <:subtitle>Use this form to manage note records in your database.</:subtitle>
</.header> </.header>
<.note_form changeset={@changeset} action={~p"/admin/notes"} channels={@channels} tags={@tags} /> <.note_form
changeset={@changeset}
action={~p"/admin/notes"}
channels={@channels}
tags={@tags}
selected_channels={@selected_channels} />
<.back navigate={~p"/admin/notes"}>Back to notes</.back> <.back navigate={~p"/admin/notes"}>Back to notes</.back>

View file

@ -21,7 +21,7 @@
label="Channel" label="Channel"
multiple={true} multiple={true}
options={@channels} options={@channels}
value={selected_channels(@changeset)} value={@selected_channels}
/> />
<:actions> <:actions>

View file

@ -12,7 +12,7 @@
<li> <li>
<a <a
href="#" href="#"
class="inline-block text-theme-base px-3 py-2.5 hover:bg-theme-background1 rounded transition font-semibold" class="inline-block text-theme-base px-3 py-2.5 border border-theme-background1 hover:bg-theme-background1 rounded transition font-semibold"
> >
<.icon name="hero-megaphone" /> <.icon name="hero-megaphone" />
</a> </a>

View file

@ -23,7 +23,8 @@ defmodule Chiya.ChannelsTest do
valid_attrs = %{ valid_attrs = %{
content: "some content", content: "some content",
name: "some name", name: "some name",
visibility: :public visibility: :public,
layout: :default
} }
assert {:ok, %Channel{} = channel} = Channels.create_channel(valid_attrs) assert {:ok, %Channel{} = channel} = Channels.create_channel(valid_attrs)
@ -44,7 +45,8 @@ defmodule Chiya.ChannelsTest do
content: "some updated content", content: "some updated content",
name: "some updated name", name: "some updated name",
slug: "some updated slug", slug: "some updated slug",
visibility: :private visibility: :private,
layout: :default
} }
assert {:ok, %Channel{} = channel} = Channels.update_channel(channel, update_attrs) assert {:ok, %Channel{} = channel} = Channels.update_channel(channel, update_attrs)

View file

@ -7,12 +7,14 @@ defmodule ChiyaWeb.ChannelControllerTest do
content: "some content", content: "some content",
name: "some name", name: "some name",
slug: "some slug", slug: "some slug",
layout: :default,
visibility: :public visibility: :public
} }
@update_attrs %{ @update_attrs %{
content: "some updated content", content: "some updated content",
name: "some updated name", name: "some updated name",
slug: "some updated slug", slug: "some updated slug",
layout: :default,
visibility: :private visibility: :private
} }
@invalid_attrs %{content: nil, name: nil, slug: nil, visibility: nil} @invalid_attrs %{content: nil, name: nil, slug: nil, visibility: nil}
@ -22,7 +24,7 @@ defmodule ChiyaWeb.ChannelControllerTest do
describe "index" do describe "index" do
test "lists all channels", %{conn: conn} do test "lists all channels", %{conn: conn} do
conn = get(conn, ~p"/admin/channels") conn = get(conn, ~p"/admin/channels")
assert html_response(conn, 200) =~ "Listing Channels" assert html_response(conn, 200) =~ "Channels"
end end
end end

View file

@ -26,7 +26,7 @@ defmodule ChiyaWeb.IdentityControllerTest do
describe "index" do describe "index" do
test "lists all identities", %{conn: conn} do test "lists all identities", %{conn: conn} do
conn = get(conn, ~p"/admin/identities") conn = get(conn, ~p"/admin/identities")
assert html_response(conn, 200) =~ "Listing Identities" assert html_response(conn, 200) =~ "Identities"
end end
end end

View file

@ -26,7 +26,7 @@ defmodule ChiyaWeb.NoteControllerTest do
describe "index" do describe "index" do
test "lists all notes", %{conn: conn} do test "lists all notes", %{conn: conn} do
conn = get(conn, ~p"/admin/notes") conn = get(conn, ~p"/admin/notes")
assert html_response(conn, 200) =~ "Listing Notes" assert html_response(conn, 200) =~ "Notes"
end end
end end
@ -45,7 +45,7 @@ defmodule ChiyaWeb.NoteControllerTest do
assert redirected_to(conn) == ~p"/admin/notes/#{id}" assert redirected_to(conn) == ~p"/admin/notes/#{id}"
conn = get(conn, ~p"/admin/notes/#{id}") conn = get(conn, ~p"/admin/notes/#{id}")
assert html_response(conn, 200) =~ "Note #{id}" assert html_response(conn, 200) =~ @create_attrs.name
end end
test "renders errors when data is invalid", %{conn: conn} do test "renders errors when data is invalid", %{conn: conn} do
@ -65,7 +65,7 @@ defmodule ChiyaWeb.NoteControllerTest do
assert redirected_to(conn) == ~p"/admin/notes/#{id}" assert redirected_to(conn) == ~p"/admin/notes/#{id}"
conn = get(conn, ~p"/admin/notes/#{id}") conn = get(conn, ~p"/admin/notes/#{id}")
assert html_response(conn, 200) =~ "Note #{id}" assert html_response(conn, 200) =~ @create_attrs.name
end end
end end
@ -86,7 +86,7 @@ defmodule ChiyaWeb.NoteControllerTest do
assert redirected_to(conn) == ~p"/admin/notes/#{note}" assert redirected_to(conn) == ~p"/admin/notes/#{note}"
conn = get(conn, ~p"/admin/notes/#{note}") conn = get(conn, ~p"/admin/notes/#{note}")
assert html_response(conn, 200) =~ "some updated content" assert html_response(conn, 200) =~ "some updated name"
end end
test "renders errors when data is invalid", %{conn: conn, note: note} do test "renders errors when data is invalid", %{conn: conn, note: note} do
@ -113,7 +113,7 @@ defmodule ChiyaWeb.NoteControllerTest do
assert redirected_to(conn) == ~p"/admin/notes/#{note}" assert redirected_to(conn) == ~p"/admin/notes/#{note}"
conn = get(conn, ~p"/admin/notes/#{note}") conn = get(conn, ~p"/admin/notes/#{note}")
assert html_response(conn, 200) =~ "some updated content" assert html_response(conn, 200) =~ "some updated name"
end end
end end

View file

@ -20,9 +20,7 @@ defmodule ChiyaWeb.UserSessionControllerTest do
# Now do a logged in request and assert on the menu # Now do a logged in request and assert on the menu
conn = get(conn, ~p"/") conn = get(conn, ~p"/")
response = html_response(conn, 200) response = html_response(conn, 200)
assert response =~ user.email assert response =~ ~p"/admin"
assert response =~ ~p"/user"
assert response =~ ~p"/user/log_out"
end end
test "logs the user in with remember me", %{conn: conn, user: user} do test "logs the user in with remember me", %{conn: conn, user: user} do

View file

@ -8,8 +8,8 @@ defmodule ChiyaWeb.UserLoginLiveTest do
test "renders log in page", %{conn: conn} do test "renders log in page", %{conn: conn} do
{:ok, _lv, html} = live(conn, ~p"/user/log_in") {:ok, _lv, html} = live(conn, ~p"/user/log_in")
assert html =~ "Log in" assert html =~ "Sign in"
assert html =~ "Register" assert html =~ "Sign up"
assert html =~ "Forgot your password?" assert html =~ "Forgot your password?"
end end

View file

@ -9,7 +9,7 @@ defmodule ChiyaWeb.UserRegistrationLiveTest do
{:ok, _lv, html} = live(conn, ~p"/user/register") {:ok, _lv, html} = live(conn, ~p"/user/register")
assert html =~ "Register" assert html =~ "Register"
assert html =~ "Log in" assert html =~ "Sign in"
end end
test "redirects if already logged in", %{conn: conn} do test "redirects if already logged in", %{conn: conn} do
@ -50,9 +50,7 @@ defmodule ChiyaWeb.UserRegistrationLiveTest do
# Now do a logged in request and assert on the menu # Now do a logged in request and assert on the menu
conn = get(conn, "/") conn = get(conn, "/")
response = html_response(conn, 200) response = html_response(conn, 200)
assert response =~ email
assert response =~ "Admin" assert response =~ "Admin"
assert response =~ "Log out"
end end
end end
@ -83,7 +81,7 @@ defmodule ChiyaWeb.UserRegistrationLiveTest do
|> render_click() |> render_click()
|> follow_redirect(conn, ~p"/user/log_in") |> follow_redirect(conn, ~p"/user/log_in")
assert login_html =~ "Log in" assert login_html =~ "Sign in"
end end
end end
end end

View file

@ -97,7 +97,7 @@ defmodule ChiyaWeb.UserResetPasswordLiveTest do
|> render_click() |> render_click()
|> follow_redirect(conn, ~p"/user/log_in") |> follow_redirect(conn, ~p"/user/log_in")
assert conn.resp_body =~ "Log in" assert conn.resp_body =~ "Sign in"
end end
test "redirects to password reset page when the Register button is clicked", %{ test "redirects to password reset page when the Register button is clicked", %{

View file

@ -17,9 +17,9 @@ defmodule Chiya.ChannelsFixtures do
attrs attrs
|> Enum.into(%{ |> Enum.into(%{
content: "some content", content: "some content",
name: "some name", name: "some name #{System.unique_integer([:positive])}",
slug: unique_channel_slug(), visibility: :public,
visibility: :public layout: :default
}) })
|> Chiya.Channels.create_channel() |> Chiya.Channels.create_channel()

View file

@ -25,6 +25,6 @@ defmodule Chiya.NotesFixtures do
}) })
|> Chiya.Notes.create_note() |> Chiya.Notes.create_note()
note Chiya.Notes.preload_note(note)
end end
end end