From 7f77ad55285259ef9c39899377afdbaf96e9965e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Mon, 3 Apr 2023 13:19:07 +0200 Subject: [PATCH] Refactor user settings Use resources instead of custom controllers, following the Rails way and making things much cleaner in the process. --- .../settings/account_controller.rb | 13 ------- .../settings/profile_controller.rb | 11 ------ app/controllers/settings_controller.rb | 30 ++++++++++++++-- app/views/settings/_account.html.erb | 19 +++++++++++ app/views/settings/_profile.html.erb | 30 ++++++++++++++++ app/views/settings/account/index.html.erb | 23 ------------- app/views/settings/profile/index.html.erb | 34 ------------------- app/views/settings/show.html.erb | 5 +++ app/views/shared/_main_nav.html.erb | 2 +- app/views/shared/_sidenav_settings.html.erb | 8 ++--- config/routes.rb | 13 ++++--- 11 files changed, 92 insertions(+), 96 deletions(-) delete mode 100644 app/controllers/settings/account_controller.rb delete mode 100644 app/controllers/settings/profile_controller.rb create mode 100644 app/views/settings/_account.html.erb create mode 100644 app/views/settings/_profile.html.erb delete mode 100644 app/views/settings/account/index.html.erb delete mode 100644 app/views/settings/profile/index.html.erb create mode 100644 app/views/settings/show.html.erb diff --git a/app/controllers/settings/account_controller.rb b/app/controllers/settings/account_controller.rb deleted file mode 100644 index 385a9ff..0000000 --- a/app/controllers/settings/account_controller.rb +++ /dev/null @@ -1,13 +0,0 @@ -class Settings::AccountController < SettingsController - - def index - end - - def reset_password - current_user.send_reset_password_instructions - sign_out current_user - msg = "We have sent you an email with a link to reset your password." - redirect_to check_your_email_path, notice: msg - end - -end diff --git a/app/controllers/settings/profile_controller.rb b/app/controllers/settings/profile_controller.rb deleted file mode 100644 index 645bd20..0000000 --- a/app/controllers/settings/profile_controller.rb +++ /dev/null @@ -1,11 +0,0 @@ -class Settings::ProfileController < SettingsController - - def index - @user = current_user - end - - def update - - end - -end diff --git a/app/controllers/settings_controller.rb b/app/controllers/settings_controller.rb index d2fc3e5..85627a6 100644 --- a/app/controllers/settings_controller.rb +++ b/app/controllers/settings_controller.rb @@ -1,13 +1,37 @@ class SettingsController < ApplicationController - before_action :require_user_signed_in - before_action :set_current_section + before_action :authenticate_user! + before_action :set_main_nav_section + before_action :set_settings_section, only: ['show', 'update'] def index + redirect_to setting_path(:profile) + end + + def show + @user = current_user + end + + def update + end + + def reset_password + current_user.send_reset_password_instructions + sign_out current_user + msg = "We have sent you an email with a link to reset your password." + redirect_to check_your_email_path, notice: msg end private - def set_current_section + def set_main_nav_section @current_section = :settings end + + def set_settings_section + @settings_section = params[:section] + + unless [:profile, :account].include?(@settings_section.to_sym) + redirect_to setting_path(:profile) + end + end end diff --git a/app/views/settings/_account.html.erb b/app/views/settings/_account.html.erb new file mode 100644 index 0000000..df0a2ad --- /dev/null +++ b/app/views/settings/_account.html.erb @@ -0,0 +1,19 @@ +
+

E-Mail

+

+ <%= label :email, 'Address', class: 'font-bold' %> +

+

+ disabled="disabled" /> +

+
+
+

Password

+

Use the following button to request an email with a password reset link:

+ <%= form_with(url: reset_password_settings_path, method: :post) do %> +

+ <%= submit_tag("Send me a password reset link", class: 'btn-md btn-gray w-full sm:w-auto') %> +

+ <% end %> +
diff --git a/app/views/settings/_profile.html.erb b/app/views/settings/_profile.html.erb new file mode 100644 index 0000000..f1d14ae --- /dev/null +++ b/app/views/settings/_profile.html.erb @@ -0,0 +1,30 @@ +
+

Profile

+

+ <%= label :user_address, 'User address', class: 'font-bold' %> +

+

+ disabled="disabled" + data-clipboard-target="source" /> + +

+

+ Your user address for Chat and Lightning Network. +

+ + <%# <%= form_for(@user, as: "profile", url: settings_profile_path) do |f| %> + <%#

+ <%# <%= f.submit "Save changes", class: 'btn-md btn-blue w-full sm:w-auto' %> + <%#

+ <%# <% end %> +
diff --git a/app/views/settings/account/index.html.erb b/app/views/settings/account/index.html.erb deleted file mode 100644 index effe9c2..0000000 --- a/app/views/settings/account/index.html.erb +++ /dev/null @@ -1,23 +0,0 @@ -<%= render HeaderComponent.new(title: "Settings") %> - -<%= render MainWithSidenavComponent.new(sidenav_partial: 'shared/sidenav_settings') do %> -
-

E-Mail

-

- <%= label :email, 'Address', class: 'font-bold' %> -

-

- disabled="disabled" /> -

-
-
-

Password

-

Use the following button to request an email with a password reset link:

- <%= form_with(url: settings_reset_password_path, method: :post) do %> -

- <%= submit_tag("Send me a password reset link", class: 'btn-md btn-gray w-full sm:w-auto') %> -

- <% end %> -
-<% end %> diff --git a/app/views/settings/profile/index.html.erb b/app/views/settings/profile/index.html.erb deleted file mode 100644 index 3e91709..0000000 --- a/app/views/settings/profile/index.html.erb +++ /dev/null @@ -1,34 +0,0 @@ -<%= render HeaderComponent.new(title: "Settings") %> - -<%= render MainWithSidenavComponent.new(sidenav_partial: 'shared/sidenav_settings') do %> -
-

Profile

-

- <%= label :user_address, 'User address', class: 'font-bold' %> -

-

- disabled="disabled" - data-clipboard-target="source" /> - -

-

- Your user address for Chat and Lightning Network. -

- - <%# <%= form_for(@user, as: "profile", url: settings_profile_path) do |f| %> - <%#

- <%# <%= f.submit "Save changes", class: 'btn-md btn-blue w-full sm:w-auto' %> - <%#

- <%# <% end %> -
-<% end %> diff --git a/app/views/settings/show.html.erb b/app/views/settings/show.html.erb new file mode 100644 index 0000000..38ddcb5 --- /dev/null +++ b/app/views/settings/show.html.erb @@ -0,0 +1,5 @@ +<%= render HeaderComponent.new(title: "Settings") %> + +<%= render MainWithSidenavComponent.new(sidenav_partial: 'shared/sidenav_settings') do %> + <%= render partial: @settings_section %> +<% end %> diff --git a/app/views/shared/_main_nav.html.erb b/app/views/shared/_main_nav.html.erb index de5b8eb..7f5ee02 100644 --- a/app/views/shared/_main_nav.html.erb +++ b/app/views/shared/_main_nav.html.erb @@ -6,5 +6,5 @@ class: main_nav_class(@current_section, :invitations) %> <%= link_to "Wallet", wallet_path, class: main_nav_class(@current_section, :wallet) %> -<%= link_to "Settings", settings_profile_path, +<%= link_to "Settings", settings_path, class: main_nav_class(@current_section, :settings) %> diff --git a/app/views/shared/_sidenav_settings.html.erb b/app/views/shared/_sidenav_settings.html.erb index 25d345a..d33c555 100644 --- a/app/views/shared/_sidenav_settings.html.erb +++ b/app/views/shared/_sidenav_settings.html.erb @@ -1,10 +1,10 @@ <%= render SidenavLinkComponent.new( - name: "Profile", path: settings_profile_path, icon: "user", - active: current_page?(settings_profile_path) + name: "Profile", path: setting_path(:profile), icon: "user", + active: current_page?(setting_path(:profile)) ) %> <%= render SidenavLinkComponent.new( - name: "Account", path: settings_account_path, icon: "key", - active: current_page?(settings_account_path) + name: "Account", path: setting_path(:account), icon: "key", + active: current_page?(setting_path(:account)) ) %> <%= render SidenavLinkComponent.new( name: "Security", path: "#", icon: "shield", disabled: true diff --git a/config/routes.rb b/config/routes.rb index e729019..e384d1c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -10,13 +10,6 @@ Rails.application.routes.draw do match 'signup/:step', to: 'signup#steps', as: :signup_steps, via: [:get, :post] post 'signup_validate', to: 'signup#validate' - namespace :settings do - get 'profile', to: 'profile#index' - post 'profile', to: 'profile#update' - get 'account', to: 'account#index' - post 'reset_password', to: 'account#reset_password' - end - namespace :contributions do root to: 'donations#index' get 'projects', to: 'projects#index' @@ -28,6 +21,12 @@ Rails.application.routes.draw do get 'wallet', to: 'wallet#index' get 'wallet/transactions', to: 'wallet#transactions' + resources :settings, param: 'section', only: ['index', 'show', 'update'] do + collection do + post 'reset_password' + end + end + get 'lnurlpay/:address', to: 'lnurlpay#index', as: 'lightning_address', constraints: { address: /[^\/]+/} get 'lnurlpay/:address/invoice', to: 'lnurlpay#invoice',