-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add basic profile page \w user import
- Loading branch information
Showing
10 changed files
with
135 additions
and
281 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
defmodule SwipexWeb.Profile do | ||
use SwipexWeb, :verified_routes | ||
import Plug.Conn | ||
import Phoenix.Controller | ||
|
||
def on_mount(:current_user, _params, session, socket) do | ||
# Generate user info if not present in the session | ||
if !Map.has_key?(session, :user) do | ||
# Generate a random user from a random number | ||
user_id = System.unique_integer([:positive]) | ||
|
||
{:cont, | ||
Phoenix.Component.assign_new(socket, :user, fn -> | ||
%{ | ||
id: user_id, | ||
name: "User #{user_id}", | ||
avatar: "https://api.dicebear.com/6.x/pixel-art/svg?seed=#{user_id}&background=%23fff", | ||
bio: "" | ||
} | ||
end)} | ||
else | ||
{:cont, Phoenix.Component.assign(socket, :user, session.user)} | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
defmodule SwipexWeb.ProfileLive do | ||
use SwipexWeb, :live_view | ||
alias SwipexWeb.ProfileLive.EditForm | ||
|
||
def handle_event("import", %{"id" => id}, socket) do | ||
id = String.to_integer(id) | ||
conn = Bolt.Sips.conn() | ||
|
||
case Bolt.Sips.query(conn, "MATCH (u:User {id: $id}) RETURN u", %{id: id}) do | ||
{:ok, response} -> | ||
user = get_user_from_response(response) | ||
|
||
if user == %{} do | ||
{:noreply, put_flash(socket, :error, "Invalid user ID.")} | ||
else | ||
{:noreply, assign(socket, :user, user) |> put_flash(:info, "User imported!")} | ||
end | ||
|
||
{:error, _error} -> | ||
{:noreply, put_flash(socket, :error, "Invalid user ID.")} | ||
end | ||
end | ||
|
||
def handle_event("save", %{"name" => name, "bio" => bio}, socket) do | ||
conn = Bolt.Sips.conn() | ||
|
||
Bolt.Sips.transaction(conn, fn conn -> | ||
# Create a new user if one doesn't exist | ||
Bolt.Sips.query(conn, "MERGE (u:User {id: $id}) RETURN u", %{id: socket.assigns.user.id}) | ||
# Update the fields | ||
Bolt.Sips.query(conn, "MATCH (u:User {id: $id}) SET u.bio = $bio RETURN u", %{ | ||
id: socket.assigns.user.id, | ||
bio: bio | ||
}) | ||
|
||
Bolt.Sips.query(conn, "MATCH (u:User {id: $id}) SET u.avatar = $avatar RETURN u", %{ | ||
id: socket.assigns.user.id, | ||
avatar: socket.assigns.user.avatar | ||
}) | ||
|
||
Bolt.Sips.query(conn, "MATCH (u:User {id: $id}) SET u.name = $name RETURN u", %{ | ||
id: socket.assigns.user.id, | ||
name: name | ||
}) | ||
end) | ||
|
||
user = | ||
Bolt.Sips.query!(conn, "MATCH (u:User {id: $id}) RETURN u", %{id: socket.assigns.user.id}) | ||
|> get_user_from_response() | ||
|
||
{:noreply, assign(socket, :user, user) |> put_flash(:info, "User updated!")} | ||
end | ||
|
||
def render(assigns) do | ||
~H""" | ||
<div class="py-12 flex flex-col gap-3"> | ||
<h1 class="text-4xl"><%= @user.name %></h1> | ||
<img src={@user.avatar} class="w-32 h-32 rounded-full" /> | ||
<p>Hey there, <%= @user.name %>, this is your profile page.</p> | ||
<p>Why don't you tell us something about yourself?</p> | ||
<div class="flex flex-col gap-3"> | ||
<.live_component id="edit_form" module={EditForm} user={@user} /> | ||
</div> | ||
<hr /> | ||
<div class="flex flex-col gap-3"> | ||
<p>Already have an account?</p> | ||
<form phx-submit="import" class="flex flex-col gap-4"> | ||
<input type="number" name="id" placeholder="User ID" /> | ||
<button type="submit" class="bg-blue-300 text-white rounded-md p-3">Import</button> | ||
</form> | ||
</div> | ||
</div> | ||
""" | ||
end | ||
|
||
defp get_user_from_response(response) do | ||
case response | ||
|> Bolt.Sips.Response.first() do | ||
nil -> | ||
%{} | ||
|
||
user -> | ||
user | ||
|> Map.get("u") | ||
|> Map.get(:properties) | ||
|> Map.new(fn {k, v} -> {String.to_atom(k), v} end) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
defmodule SwipexWeb.ProfileLive.EditForm do | ||
use SwipexWeb, :live_component | ||
|
||
def render(assigns) do | ||
~H""" | ||
<form phx-submit="save" class="flex flex-col gap-4"> | ||
<input type="text" name="name" placeholder="Name" value={@user.name} /> | ||
<textarea name="bio" placeholder="Bio"><%= @user.bio %></textarea> | ||
<button type="submit" class="bg-blue-300 text-white rounded-md p-3">Save</button> | ||
</form> | ||
""" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters