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}
end
def update_tags({:error, changeset}, _attrs) do
{:error, changeset}
end
@doc """
Updates the tags for the given schema

View file

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

View file

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

View file

@ -3,6 +3,11 @@
<:subtitle>Use this form to manage note records in your database.</:subtitle>
</.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>

View file

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

View file

@ -12,7 +12,7 @@
<li>
<a
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" />
</a>

View file

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

View file

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

View file

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

View file

@ -26,7 +26,7 @@ defmodule ChiyaWeb.NoteControllerTest do
describe "index" do
test "lists all notes", %{conn: conn} do
conn = get(conn, ~p"/admin/notes")
assert html_response(conn, 200) =~ "Listing Notes"
assert html_response(conn, 200) =~ "Notes"
end
end
@ -45,7 +45,7 @@ defmodule ChiyaWeb.NoteControllerTest do
assert redirected_to(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
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}"
conn = get(conn, ~p"/admin/notes/#{id}")
assert html_response(conn, 200) =~ "Note #{id}"
assert html_response(conn, 200) =~ @create_attrs.name
end
end
@ -86,7 +86,7 @@ defmodule ChiyaWeb.NoteControllerTest do
assert redirected_to(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
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}"
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

View file

@ -20,9 +20,7 @@ defmodule ChiyaWeb.UserSessionControllerTest do
# Now do a logged in request and assert on the menu
conn = get(conn, ~p"/")
response = html_response(conn, 200)
assert response =~ user.email
assert response =~ ~p"/user"
assert response =~ ~p"/user/log_out"
assert response =~ ~p"/admin"
end
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
{:ok, _lv, html} = live(conn, ~p"/user/log_in")
assert html =~ "Log in"
assert html =~ "Register"
assert html =~ "Sign in"
assert html =~ "Sign up"
assert html =~ "Forgot your password?"
end

View file

@ -9,7 +9,7 @@ defmodule ChiyaWeb.UserRegistrationLiveTest do
{:ok, _lv, html} = live(conn, ~p"/user/register")
assert html =~ "Register"
assert html =~ "Log in"
assert html =~ "Sign in"
end
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
conn = get(conn, "/")
response = html_response(conn, 200)
assert response =~ email
assert response =~ "Admin"
assert response =~ "Log out"
end
end
@ -83,7 +81,7 @@ defmodule ChiyaWeb.UserRegistrationLiveTest do
|> render_click()
|> follow_redirect(conn, ~p"/user/log_in")
assert login_html =~ "Log in"
assert login_html =~ "Sign in"
end
end
end

View file

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

View file

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

View file

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