From f10ef9ff9cb941ddeb21483d5e775ab351dd0448 Mon Sep 17 00:00:00 2001 From: Inhji Date: Mon, 3 Jul 2023 20:16:06 +0200 Subject: [PATCH 1/3] fix kbar --- assets/js/app.js | 2 +- assets/js/kbar.js | 2 +- assets/package-lock.json | 190 ++++++++++++++++---------------- assets/package.json | 4 +- lib/chiya/tags/tag_updater.ex | 7 +- test/chiya/tag_updater_test.exs | 57 ++++++++++ 6 files changed, 162 insertions(+), 100 deletions(-) create mode 100644 test/chiya/tag_updater_test.exs diff --git a/assets/js/app.js b/assets/js/app.js index fd3b1cb..e245ae5 100644 --- a/assets/js/app.js +++ b/assets/js/app.js @@ -23,7 +23,7 @@ import { LiveSocket } from "phoenix_live_view" import topbar from "../vendor/topbar" import lolight from "../vendor/lolight" import React from "react" -import { createRoot } from 'react-dom' +import { createRoot } from 'react-dom/client' import KBar from "./kbar" lolight("pre code") diff --git a/assets/js/kbar.js b/assets/js/kbar.js index 98c9844..d3e265c 100644 --- a/assets/js/kbar.js +++ b/assets/js/kbar.js @@ -44,7 +44,7 @@ function RenderResults() { items={results} onRender={({ item, active }) => typeof item === "string" ? ( -
{item}
+
{item}
) : ( =12" }, "optionalDependencies": { - "@esbuild/android-arm": "0.18.0", - "@esbuild/android-arm64": "0.18.0", - "@esbuild/android-x64": "0.18.0", - "@esbuild/darwin-arm64": "0.18.0", - "@esbuild/darwin-x64": "0.18.0", - "@esbuild/freebsd-arm64": "0.18.0", - "@esbuild/freebsd-x64": "0.18.0", - "@esbuild/linux-arm": "0.18.0", - "@esbuild/linux-arm64": "0.18.0", - "@esbuild/linux-ia32": "0.18.0", - "@esbuild/linux-loong64": "0.18.0", - "@esbuild/linux-mips64el": "0.18.0", - "@esbuild/linux-ppc64": "0.18.0", - "@esbuild/linux-riscv64": "0.18.0", - "@esbuild/linux-s390x": "0.18.0", - "@esbuild/linux-x64": "0.18.0", - "@esbuild/netbsd-x64": "0.18.0", - "@esbuild/openbsd-x64": "0.18.0", - "@esbuild/sunos-x64": "0.18.0", - "@esbuild/win32-arm64": "0.18.0", - "@esbuild/win32-ia32": "0.18.0", - "@esbuild/win32-x64": "0.18.0" + "@esbuild/android-arm": "0.18.11", + "@esbuild/android-arm64": "0.18.11", + "@esbuild/android-x64": "0.18.11", + "@esbuild/darwin-arm64": "0.18.11", + "@esbuild/darwin-x64": "0.18.11", + "@esbuild/freebsd-arm64": "0.18.11", + "@esbuild/freebsd-x64": "0.18.11", + "@esbuild/linux-arm": "0.18.11", + "@esbuild/linux-arm64": "0.18.11", + "@esbuild/linux-ia32": "0.18.11", + "@esbuild/linux-loong64": "0.18.11", + "@esbuild/linux-mips64el": "0.18.11", + "@esbuild/linux-ppc64": "0.18.11", + "@esbuild/linux-riscv64": "0.18.11", + "@esbuild/linux-s390x": "0.18.11", + "@esbuild/linux-x64": "0.18.11", + "@esbuild/netbsd-x64": "0.18.11", + "@esbuild/openbsd-x64": "0.18.11", + "@esbuild/sunos-x64": "0.18.11", + "@esbuild/win32-arm64": "0.18.11", + "@esbuild/win32-ia32": "0.18.11", + "@esbuild/win32-x64": "0.18.11" } }, "node_modules/fast-equals": { diff --git a/assets/package.json b/assets/package.json index 05929c6..6d04eb6 100644 --- a/assets/package.json +++ b/assets/package.json @@ -11,7 +11,7 @@ "phoenix_live_view": "file:../deps/phoenix_live_view" }, "peerDependencies": { - "react": "^17.0.0", - "react-dom": "^17.0.0" + "react": "^18.0", + "react-dom": "^18.0" } } diff --git a/lib/chiya/tags/tag_updater.ex b/lib/chiya/tags/tag_updater.ex index 60b8a99..89b6b06 100644 --- a/lib/chiya/tags/tag_updater.ex +++ b/lib/chiya/tags/tag_updater.ex @@ -43,7 +43,12 @@ defmodule Chiya.Tags.TagUpdater do end def update_tags(%{tags: tags} = schema, new_tags) when is_list(new_tags) do - old_tags = Enum.map(tags, fn tag -> tag.name end) + old_tags = Enum.map(tags, fn tag -> String.downcase(tag.slug) end) + new_tags = Enum.map(new_tags, fn tag -> + tag + |> String.downcase() + |> Slugger.slugify() + end) Logger.info("Adding tags #{inspect(new_tags -- old_tags)}") Logger.info("Removing tags #{inspect(old_tags -- new_tags)}") diff --git a/test/chiya/tag_updater_test.exs b/test/chiya/tag_updater_test.exs new file mode 100644 index 0000000..5d995ec --- /dev/null +++ b/test/chiya/tag_updater_test.exs @@ -0,0 +1,57 @@ +defmodule Chiya.TagUpdaterTest do + use Chiya.DataCase + + import Chiya.NotesFixtures + alias Chiya.Tags.TagUpdater + + describe "update_tags/2" do + test "with a single tag updates a note with the given tag" do + note = note_fixture() + + assert note.tags == [] + TagUpdater.update_tags(note, "foo") + note = Chiya.Notes.get_note_preloaded!(note.id) + assert Enum.count(note.tags) == 1 + end + + test "with a list of new tags replaces exisiting tags"do + note = note_fixture() + + assert note.tags == [] + + TagUpdater.update_tags(note, "foo") + note = Chiya.Notes.get_note_preloaded!(note.id) + assert Enum.count(note.tags) == 1 + + TagUpdater.update_tags(note, ["bar", "baz"]) + note = Chiya.Notes.get_note_preloaded!(note.id) + assert Enum.count(note.tags) == 2 + end + + test "with a map representing the attributes replaces existing tags" do + note = note_fixture() + + assert note.tags == [] + + TagUpdater.update_tags(note, %{tags_string: "foo,bar,baz"}) + note = Chiya.Notes.get_note_preloaded!(note.id) + assert Enum.count(note.tags) == 3 + end + + test "with the same tags in different capitalization replaces exisiting tags" do + note = note_fixture() + assert note.tags == [] + + TagUpdater.update_tags(note, "foo") + note = Chiya.Notes.get_note_preloaded!(note.id) + assert Enum.count(note.tags) == 1 + + TagUpdater.update_tags(note, ["Foo"]) + note = Chiya.Notes.get_note_preloaded!(note.id) + assert Enum.count(note.tags) == 1 + + tag = List.first(note.tags) + assert tag.name == "Foo" + end + end +end From 7d2c3aceef43a71ef88e8ac4cb37688a6ce70d57 Mon Sep 17 00:00:00 2001 From: Inhji Date: Mon, 3 Jul 2023 20:16:50 +0200 Subject: [PATCH 2/3] add user fields --- lib/chiya/accounts.ex | 22 +++++++++ lib/chiya/accounts/user.ex | 12 +++++ lib/chiya_web/live/user_profile_live.ex | 3 ++ lib/chiya_web/live/user_settings_live.ex | 46 +++++++++++++++++++ .../20230703173933_add_user_fields.exs | 11 +++++ 5 files changed, 94 insertions(+) create mode 100644 priv/repo/migrations/20230703173933_add_user_fields.exs diff --git a/lib/chiya/accounts.ex b/lib/chiya/accounts.ex index d0664bd..40c0dd9 100644 --- a/lib/chiya/accounts.ex +++ b/lib/chiya/accounts.ex @@ -116,6 +116,19 @@ defmodule Chiya.Accounts do User.email_changeset(user, attrs, validate_email: false) end + @doc """ + Returns an `%Ecto.Changeset{}` for changing the user profile. + + ## Examples + + iex> change_user_profile(user) + %Ecto.Changeset{data: %User{}} + + """ + def change_user_profile(user, attrs \\ %{}) do + User.profile_changeset(user, attrs) + end + @doc """ Returns an `%Ecto.Changeset{}` for changing the user avatar. @@ -149,6 +162,15 @@ defmodule Chiya.Accounts do |> Ecto.Changeset.apply_action(:update) end + @doc """ + Updates the user profile. + """ + def update_user_profile(user, attrs) do + user + |> User.profile_changeset(attrs) + |> Repo.update() + end + @doc """ Updates the user email using the given token. diff --git a/lib/chiya/accounts/user.ex b/lib/chiya/accounts/user.ex index f25b8d3..8591814 100644 --- a/lib/chiya/accounts/user.ex +++ b/lib/chiya/accounts/user.ex @@ -4,6 +4,10 @@ defmodule Chiya.Accounts.User do import Ecto.Changeset schema "users" do + field :name, :string + field :handle, :string + field :bio, :string + field :email, :string field :password, :string, virtual: true, redact: true field :hashed_password, :string, redact: true @@ -116,6 +120,14 @@ defmodule Chiya.Accounts.User do end end + @doc """ + Returns an `%Ecto.Changeset{}` for tracking user profile changes. + """ + def profile_changeset(user, attrs) do + user + |> cast(attrs, [:name, :handle, :bio]) + end + @doc """ A user changeset for changing the password. diff --git a/lib/chiya_web/live/user_profile_live.ex b/lib/chiya_web/live/user_profile_live.ex index 301c088..1b9ac17 100644 --- a/lib/chiya_web/live/user_profile_live.ex +++ b/lib/chiya_web/live/user_profile_live.ex @@ -13,6 +13,9 @@ defmodule ChiyaWeb.UserProfileLive do <.list> + <:item title="Name"><%= @current_user.name %> + <:item title="Handle"><%= @current_user.handle %> + <:item title="Bio"><%= @current_user.bio %> <:item title="Email"><%= @current_user.email %> """ diff --git a/lib/chiya_web/live/user_settings_live.ex b/lib/chiya_web/live/user_settings_live.ex index 20b53bd..4c03a12 100644 --- a/lib/chiya_web/live/user_settings_live.ex +++ b/lib/chiya_web/live/user_settings_live.ex @@ -29,6 +29,25 @@ defmodule ChiyaWeb.UserSettingsLive do <.line /> + <.header>Change Profile + + <.simple_form + for={@profile_form} + id="profile_form" + phx-submit="update_profile" + phx-change="validate_profile" + > + <.input field={@profile_form[:name]} label="Name" required /> + <.input field={@profile_form[:handle]} label="Handle" required /> + <.input field={@profile_form[:bio]} type="textarea" label="Bio" required /> + + <:actions> + <.button phx-disable-with="Changing...">Change Email + + + + <.line /> + <.header>Change Email <.simple_form @@ -107,6 +126,7 @@ defmodule ChiyaWeb.UserSettingsLive do email_changeset = Accounts.change_user_email(user) password_changeset = Accounts.change_user_password(user) image_changeset = Accounts.change_user_image(user) + profile_changeset = Accounts.change_user_profile(user) socket = socket @@ -116,6 +136,7 @@ defmodule ChiyaWeb.UserSettingsLive do |> assign(:current_email, user.email) |> assign(:email_form, to_form(email_changeset)) |> assign(:password_form, to_form(password_changeset)) + |> assign(:profile_form, to_form(profile_changeset)) |> assign(:image_form, to_form(image_changeset)) |> assign(:trigger_submit, false) |> assign(:uploaded_files, []) @@ -150,6 +171,31 @@ defmodule ChiyaWeb.UserSettingsLive do |> assign(:user, Accounts.get_user!(user.id))} end + def handle_event("validate_profile", params, socket) do + %{"user" => user_params} = params + + profile_form = + socket.assigns.current_user + |> Accounts.change_user_profile(user_params) + |> Map.put(:action, :validate) + |> to_form() + + {:noreply, assign(socket, profile_form: profile_form)} + end + + def handle_event("update_profile", params, socket) do + %{"user" => user_params} = params + user = socket.assigns.current_user + + case Accounts.update_user_profile(user, user_params) do + {:ok, _updated_user} -> + {:noreply, socket |> put_flash(:info, "User updated!")} + + {:error, changeset} -> + {:noreply, assign(socket, :profile_form, to_form(Map.put(changeset, :action, :insert)))} + end + end + def handle_event("validate_email", params, socket) do %{"current_password" => password, "user" => user_params} = params diff --git a/priv/repo/migrations/20230703173933_add_user_fields.exs b/priv/repo/migrations/20230703173933_add_user_fields.exs new file mode 100644 index 0000000..c301e3d --- /dev/null +++ b/priv/repo/migrations/20230703173933_add_user_fields.exs @@ -0,0 +1,11 @@ +defmodule Chiya.Repo.Migrations.AddUserFields do + use Ecto.Migration + + def change do + alter table(:users) do + add :handle, :string + add :name, :string + add :bio, :text + end + end +end From 6fddb85c2e083cdecb63c0180b6d72308a2a0ef2 Mon Sep 17 00:00:00 2001 From: Inhji Date: Mon, 3 Jul 2023 20:38:35 +0200 Subject: [PATCH 3/3] add basic hcard --- lib/chiya_web/controllers/page_controller.ex | 12 +++++++--- .../controllers/page_html/about.html.heex | 24 +++++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 lib/chiya_web/controllers/page_html/about.html.heex diff --git a/lib/chiya_web/controllers/page_controller.ex b/lib/chiya_web/controllers/page_controller.ex index 262f0d0..dd37f6f 100644 --- a/lib/chiya_web/controllers/page_controller.ex +++ b/lib/chiya_web/controllers/page_controller.ex @@ -55,9 +55,15 @@ defmodule ChiyaWeb.PageController do end end - ### ========= REDIRECTS - def about(conn, _params) do - redirect(conn, to: ~p"/note/about") + note = Chiya.Notes.get_note_by_slug_preloaded("about") + user = Chiya.Accounts.get_user!(1) + + render(conn, :about, + layout: {ChiyaWeb.Layouts, "public.html"}, + note: note, + user: user, + page_title: "About" + ) end end diff --git a/lib/chiya_web/controllers/page_html/about.html.heex b/lib/chiya_web/controllers/page_html/about.html.heex new file mode 100644 index 0000000..c69a620 --- /dev/null +++ b/lib/chiya_web/controllers/page_html/about.html.heex @@ -0,0 +1,24 @@ +
+
+
+ +
+
+

+ <%= @user.name %> +

+
+ <%= Markdown.render(@user.bio) |> raw() %> +
+
+
+ + <%= if @note do %> +
+ <%= Markdown.render(@note.content) |> raw %> +
+ <% end %> +
\ No newline at end of file