Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reorganize LiveView modules into a consistent and maintainable structure #5

Merged
merged 4 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
For a fitness-focused application, I'd suggest the following design philosophy:
Color Scheme:
Primary: A strong, energetic color like deep blue (#2563eb) - represents trust, strength, and professionalism
Secondary: A vibrant accent like coral (#f97316) - adds energy and motivation
Supporting colors:
Dark gray (#1f2937) for text
Light gray (#f3f4f6) for backgrounds
White for cards and contrast
Design Principles:
Bold and confident typography
Clean, structured layouts
High contrast for readability
Subtle shadows and elevation for depth
Smooth transitions for interactivity
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule GymLiveWeb.UserConfirmationInstructionsLive do
defmodule GymLiveWeb.Live.Auth.UserConfirmationInstructions do
use GymLiveWeb, :live_view

alias GymLive.Account
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule GymLiveWeb.UserConfirmationLive do
defmodule GymLiveWeb.Live.Auth.UserConfirmation do
use GymLiveWeb, :live_view

alias GymLive.Account
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule GymLiveWeb.UserForgotPasswordLive do
defmodule GymLiveWeb.Live.Auth.UserForgotPassword do
use GymLiveWeb, :live_view

alias GymLive.Account
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule GymLiveWeb.UserLoginLive do
defmodule GymLiveWeb.Live.Auth.UserLogin do
use GymLiveWeb, :live_view

def render(assigns) do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule GymLiveWeb.UserRegistrationLive do
defmodule GymLiveWeb.Live.Auth.UserRegistration do
use GymLiveWeb, :live_view

alias GymLive.Account
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule GymLiveWeb.UserResetPasswordLive do
defmodule GymLiveWeb.Live.Auth.UserResetPassword do
use GymLiveWeb, :live_view

alias GymLive.Account
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule GymLiveWeb.UserSettingsLive do
defmodule GymLiveWeb.Live.Auth.UserSettings do
use GymLiveWeb, :live_view

alias GymLive.Account
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule GymLiveWeb.ViewCharts do
defmodule GymLiveWeb.Live.Charts.ViewCharts do
use GymLiveWeb, :live_app
import GymLiveWeb.Charts
alias GymLive.{Strength, Training}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
defmodule GymLiveWeb.Nav do
alias GymLiveWeb.{EditWorkout, ViewCharts, ViewWorkout, ViewWorkouts}
defmodule GymLiveWeb.Live.Components.Layout.Nav do
alias GymLiveWeb.Live.Workouts.{EditWorkout, ViewWorkout, ViewWorkouts}
alias GymLiveWeb.Live.Charts.ViewCharts
import Phoenix.LiveView
use Phoenix.Component

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule GymLiveWeb.EditWorkout do
defmodule GymLiveWeb.Live.Workouts.EditWorkout do
use GymLiveWeb, :live_app
alias GymLive.Training
alias GymLive.Training.{Exercises, Set, Workout}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule GymLiveWeb.ViewWorkout do
defmodule GymLiveWeb.Live.Workouts.ViewWorkout do
use GymLiveWeb, :live_app
import GymLiveWeb.StrongMan
alias GymLive.Strength
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule GymLiveWeb.ViewWorkouts do
defmodule GymLiveWeb.Live.Workouts.ViewWorkouts do
use GymLiveWeb, :live_app
alias GymLive.Training
alias GymLive.Repo
Expand Down
51 changes: 22 additions & 29 deletions lib/gym_live_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,9 @@ defmodule GymLiveWeb.Router do
plug :accepts, ["json"]
end

# Other scopes may use custom stacks.
# scope "/api", GymLiveWeb do
# pipe_through :api
# end

# Enable LiveDashboard and Swoosh mailbox preview in development
# Development routes
if Application.compile_env(:gym_live, :dev_routes) do
# If you want to use the LiveDashboard in production, you should put
# it behind authentication and allow only admins to access it.
# If your application does not have an admins-only section yet,
# you can use Plug.BasicAuth to set up some basic authentication
# as long as you are also using SSL (which you should anyway).
# Enable LiveDashboard and Swoosh mailbox preview in development
import Phoenix.LiveDashboard.Router

scope "/dev" do
Expand All @@ -39,18 +30,17 @@ defmodule GymLiveWeb.Router do
end
end

## Authentication routes

scope "/", GymLiveWeb do
pipe_through [:browser, :redirect_if_user_is_authenticated]

get "/", PageController, :home

live_session :redirect_if_user_is_authenticated,
on_mount: [{GymLiveWeb.UserAuth, :redirect_if_user_is_authenticated}] do
get "/", PageController, :home
live "/users/register", UserRegistrationLive, :new
live "/users/log_in", UserLoginLive, :new
live "/users/reset_password", UserForgotPasswordLive, :new
live "/users/reset_password/:token", UserResetPasswordLive, :edit
live "/users/register", Live.Auth.UserRegistration, :new
live "/users/log_in", Live.Auth.UserLogin, :new
live "/users/reset_password", Live.Auth.UserForgotPassword, :new
live "/users/reset_password/:token", Live.Auth.UserResetPassword, :edit
end

post "/users/log_in", UserSessionController, :create
Expand All @@ -60,15 +50,18 @@ defmodule GymLiveWeb.Router do
pipe_through [:browser, :require_authenticated_user]

live_session :require_authenticated_user,
on_mount: [{GymLiveWeb.UserAuth, :ensure_authenticated}, GymLiveWeb.Nav] do
live "/users/settings", UserSettingsLive, :edit
live "/users/settings/confirm_email/:token", UserSettingsLive, :confirm_email

live "/workout/:id", ViewWorkout
live "/workouts", ViewWorkouts
live "/edit_workout", EditWorkout

live "/charts", ViewCharts
on_mount: [
{GymLiveWeb.UserAuth, :ensure_authenticated},
GymLiveWeb.Live.Components.Layout.Nav
] do
live "/users/settings", Live.Auth.UserSettings, :edit
live "/users/settings/confirm_email/:token", Live.Auth.UserSettings, :confirm_email

live "/workout/:id", Live.Workouts.ViewWorkout
live "/workouts", Live.Workouts.ViewWorkouts
live "/edit_workout", Live.Workouts.EditWorkout

live "/charts", Live.Charts.ViewCharts
end
end

Expand All @@ -79,8 +72,8 @@ defmodule GymLiveWeb.Router do

live_session :current_user,
on_mount: [{GymLiveWeb.UserAuth, :mount_current_user}] do
live "/users/confirm/:token", UserConfirmationLive, :edit
live "/users/confirm", UserConfirmationInstructionsLive, :new
live "/users/confirm/:token", Live.Auth.UserConfirmation, :edit
live "/users/confirm", Live.Auth.UserConfirmationInstructions, :new
end
end
end
2 changes: 1 addition & 1 deletion test/gym_live_web/live/user_confirmation_live_test.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule GymLiveWeb.UserConfirmationLiveTest do
defmodule GymLiveWeb.Live.Auth.UserConfirmationTest do
use GymLiveWeb.ConnCase

import Phoenix.LiveViewTest
Expand Down
2 changes: 1 addition & 1 deletion test/gym_live_web/live/user_login_live_test.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule GymLiveWeb.UserLoginLiveTest do
defmodule GymLiveWeb.Live.Auth.UserLoginTest do
use GymLiveWeb.ConnCase

import Phoenix.LiveViewTest
Expand Down
2 changes: 1 addition & 1 deletion test/gym_live_web/live/user_registration_live_test.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule GymLiveWeb.UserRegistrationLiveTest do
defmodule GymLiveWeb.Live.Auth.UserRegistrationTest do
use GymLiveWeb.ConnCase

import Phoenix.LiveViewTest
Expand Down
19 changes: 11 additions & 8 deletions test/gym_live_web/live/user_settings_live_test.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
defmodule GymLiveWeb.UserSettingsLiveTest do
defmodule GymLiveWeb.Live.Auth.UserSettingsTest do
use GymLiveWeb.ConnCase

alias GymLive.Account
import Phoenix.LiveViewTest
import GymLive.AccountFixtures

Expand Down Expand Up @@ -46,7 +45,7 @@ defmodule GymLiveWeb.UserSettingsLiveTest do
|> render_submit()

assert result =~ "A link to confirm your email"
assert Account.get_user_by_email(user.email)
assert GymLive.Account.get_user_by_email(user.email)
end

test "renders errors with invalid data (phx-change)", %{conn: conn} do
Expand Down Expand Up @@ -115,7 +114,7 @@ defmodule GymLiveWeb.UserSettingsLiveTest do
assert Phoenix.Flash.get(new_password_conn.assigns.flash, :info) =~
"Password updated successfully"

assert Account.get_user_by_email_and_password(user.email, new_password)
assert GymLive.Account.get_user_by_email_and_password(user.email, new_password)
end

test "renders errors with invalid data (phx-change)", %{conn: conn} do
Expand Down Expand Up @@ -165,7 +164,11 @@ defmodule GymLiveWeb.UserSettingsLiveTest do

token =
extract_user_token(fn url ->
Account.deliver_user_update_email_instructions(%{user | email: email}, user.email, url)
GymLive.Account.deliver_user_update_email_instructions(
%{user | email: email},
user.email,
url
)
end)

%{conn: log_in_user(conn, user), token: token, email: email, user: user}
Expand All @@ -178,8 +181,8 @@ defmodule GymLiveWeb.UserSettingsLiveTest do
assert path == ~p"/users/settings"
assert %{"info" => message} = flash
assert message == "Email changed successfully."
refute Account.get_user_by_email(user.email)
assert Account.get_user_by_email(email)
refute GymLive.Account.get_user_by_email(user.email)
assert GymLive.Account.get_user_by_email(email)

# use confirm token again
{:error, redirect} = live(conn, ~p"/users/settings/confirm_email/#{token}")
Expand All @@ -195,7 +198,7 @@ defmodule GymLiveWeb.UserSettingsLiveTest do
assert path == ~p"/users/settings"
assert %{"error" => message} = flash
assert message == "Email change link is invalid or it has expired."
assert Account.get_user_by_email(user.email)
assert GymLive.Account.get_user_by_email(user.email)
end

test "redirects if user is not logged in", %{token: token} do
Expand Down