Skip to content

Commit

Permalink
fix: fix breaking profile page
Browse files Browse the repository at this point in the history
  • Loading branch information
zvonimirr committed Jun 16, 2024
1 parent cb6ae65 commit 75c9e1b
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 50 deletions.
11 changes: 11 additions & 0 deletions lib/swipex/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,15 @@ defmodule Swipex.User do
_ -> {:error, "Invalid name or password."}
end
end

def get_user_by_id(id) do
conn = Bolt.Sips.conn()

with {:ok, %Bolt.Sips.Response{results: [%{"u" => %{properties: user}}]}} <-
Bolt.Sips.query(conn, "MATCH (u:User {id: $id}) RETURN u", %{id: id}) do
{:ok, user}
else
_ -> {:error, "User not found."}
end
end
end
10 changes: 8 additions & 2 deletions lib/swipex_web/controllers/page.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@ defmodule SwipexWeb.PageController do
use SwipexWeb, :controller

def index(conn, _params) do
render(conn, :index)
case get_session(conn, :user_id) do
nil -> render(conn, :index)
_ -> redirect(conn, to: "/profile")
end
end

def register(conn, _params) do
render(conn, :register)
case get_session(conn, :user_id) do
nil -> render(conn, :register)
_ -> redirect(conn, to: "/profile")
end
end

def do_register(conn, %{
Expand Down
19 changes: 3 additions & 16 deletions lib/swipex_web/controllers/profile.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,9 @@ defmodule SwipexWeb.Profile do
use SwipexWeb, :verified_routes

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)}
case Swipex.User.get_user_by_id(Map.get(session, "user_id")) do
{:ok, user} -> {:cont, Phoenix.Component.assign(socket, :user, user)}
{:error, _} -> {:cont, Phoenix.Component.assign(socket, :user, nil)}
end
end
end
47 changes: 18 additions & 29 deletions lib/swipex_web/live/profile_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@ defmodule SwipexWeb.ProfileLive do
alias Phoenix.PubSub
alias SwipexWeb.ProfileLive.EditForm

def mount(_params, _session, socket) do
PubSub.subscribe(Swipex.PubSub, "swipex")
{:ok, socket}
def mount(_params, session, socket) do
if Map.has_key?(session, "user_id") do
PubSub.subscribe(Swipex.PubSub, "swipex")
{:ok, socket}
else
{:ok,
socket
|> put_flash(:error, "You must be logged in to access this page.")
|> redirect(to: "/login")}
end
end

def handle_event("import", %{"id" => id}, socket) do
Expand Down Expand Up @@ -146,49 +153,33 @@ defmodule SwipexWeb.ProfileLive do
end

def render(assigns) do
assigns = assign(assigns, :potential_match, get_potential_match(assigns.user.id))
assigns = assign(assigns, :matches, get_matches(assigns.user.id))
assigns = assign(assigns, :potential_match, get_potential_match(assigns.user["id"]))
assigns = assign(assigns, :matches, get_matches(assigns.user["id"]))

~H"""
<div class="flex gap-4 justify-between w-full mb-3">
<div class="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>
<h1 class="text-4xl"><%= @user["name"] %></h1>
<p>Hey there, <%= @user["name"] %>, this is your profile page.</p>
<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>
<%= if @potential_match do %>
<div class="flex flex-col gap-3">
<p class="text-2xl">Let's swipe!</p>
<div class="flex flex-col gap-3">
<img src={@potential_match.avatar} class="w-32 h-32 rounded-full" />
<p><%= @potential_match.name %></p>
<p><%= @potential_match.bio %></p>
<p><%= @potential_match["name"] %></p>
<div class="flex gap-3">
<button
phx-click="like"
phx-value-potential-match={@potential_match.id}
phx-value-potential-match={@potential_match["id"]}
class="bg-green-300 text-white rounded-md p-3"
>
Like
</button>
<button
phx-click="dislike"
phx-value-potential-match={@potential_match.id}
phx-value-potential-match={@potential_match["id"]}
class="bg-red-300 text-white rounded-md p-3"
>
Dislike
Expand All @@ -199,7 +190,6 @@ defmodule SwipexWeb.ProfileLive do
<% else %>
<div class="flex flex-col">
<p class="text-2xl">No more potential matches!</p>
<p>(Note: You must save your profile before you can start swiping)</p>
</div>
<% end %>
</div>
Expand All @@ -212,8 +202,7 @@ defmodule SwipexWeb.ProfileLive do
<% end %>
<%= for match <- @matches do %>
<div class="flex flex-col gap-3">
<img src={match.avatar} class="w-32 h-32 rounded-full" />
<p class="text-center"><%= match.name %></p>
<p class="text-center"><%= match["name"] %></p>
</div>
<% end %>
</div>
Expand Down
6 changes: 3 additions & 3 deletions lib/swipex_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ defmodule SwipexWeb.Router do
get "/login", PageController, :login
post "/login", PageController, :do_login

# live_session :default, on_mount: [{SwipexWeb.Profile, :current_user}] do
# live "/", ProfileLive, :index
# end
live_session :default, on_mount: [{SwipexWeb.Profile, :current_user}] do
live "/profile", ProfileLive, :index
end
end

# Other scopes may use custom stacks.
Expand Down

0 comments on commit 75c9e1b

Please sign in to comment.