From a8978b94a5e751d3831bfb6b1c08fd2e51e50f8e Mon Sep 17 00:00:00 2001
From: Teal Bauer <git@teal.is>
Date: Sun, 26 May 2024 16:17:17 +0200
Subject: [PATCH] blufprf

---
 .../stylesheets/application.tailwind.css      | 11 +++++
 app/controllers/application_controller.rb     |  7 +++
 app/controllers/users_controller.rb           | 47 +++++++++++++++++++
 app/helpers/application_helper.rb             |  3 ++
 app/helpers/users_helper.rb                   |  2 +
 app/javascript/application.js                 | 15 ++++++
 app/views/layouts/application.html.erb        | 18 +++++++
 app/views/shared/_flash.html.erb              | 10 ++++
 app/views/users/login.html.erb                | 10 ++++
 app/views/users/logout.html.erb               |  4 ++
 app/views/users/profile.html.erb              | 28 +++++++++++
 app/views/users/update.html.erb               |  4 ++
 config/routes.rb                              |  6 +++
 test/controllers/users_controller_test.rb     | 23 +++++++++
 14 files changed, 188 insertions(+)
 create mode 100644 app/controllers/users_controller.rb
 create mode 100644 app/helpers/users_helper.rb
 create mode 100644 app/views/shared/_flash.html.erb
 create mode 100644 app/views/users/login.html.erb
 create mode 100644 app/views/users/logout.html.erb
 create mode 100644 app/views/users/profile.html.erb
 create mode 100644 app/views/users/update.html.erb
 create mode 100644 test/controllers/users_controller_test.rb

diff --git a/app/assets/stylesheets/application.tailwind.css b/app/assets/stylesheets/application.tailwind.css
index 094625f..d8baf27 100644
--- a/app/assets/stylesheets/application.tailwind.css
+++ b/app/assets/stylesheets/application.tailwind.css
@@ -41,3 +41,14 @@ select {
     @apply bg-green-100 border-green-300;
   }
 }
+#flash {
+  @apply mx-auto flex items-center flex-col gap-1 p-2;
+  .flash {
+    @apply bg-slate-50 border-slate-100 border inline-block p-2 rounded-md shadow;
+  }
+}
+.main-nav {
+  a {
+    @apply underline hover:text-blue-600 hover:border-b hover:border-blue-600;
+  }
+}
\ No newline at end of file
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb
index 09705d1..001ed30 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -1,2 +1,9 @@
 class ApplicationController < ActionController::Base
+  helper_method :current_user
+
+  private
+
+  def current_user
+    @current_user ||= User.find_by(id: session[:user_id]) if session[:user_id]
+  end
 end
diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb
new file mode 100644
index 0000000..05b666e
--- /dev/null
+++ b/app/controllers/users_controller.rb
@@ -0,0 +1,47 @@
+class UsersController < ApplicationController
+  before_action :require_login, except: [:login]
+
+  def login
+    @users = User.all
+    if params[:user_id]
+      user = User.find(params[:user_id])
+      session[:user_id] = user.id
+      redirect_to conferences_path, notice: "Logged in as #{user.name}"
+    end
+  end
+
+  def logout
+    session[:user_id] = nil
+    redirect_to login_path, notice: "Logged out"
+  end
+
+  def profile
+    @user = current_user
+  end
+
+  def update_profile
+    @user = current_user
+    if @user.update(user_params)
+      flash.now[:notice] = "Profile updated successfully!"
+      respond_to do |format|
+        format.html { redirect_to profile_path, notice: "Profile updated successfully!"  }
+        #format.turbo_stream { render turbo_stream: turbo_stream.replace("flash", partial: "shared/flash") }
+      end
+    else
+      render :edit
+    end
+  end
+
+  private
+
+  def user_params
+    params.require(:user).permit(:name, :email, :avatar_color, :telegram_username)
+  end
+
+  def require_login
+    unless current_user
+      flash[:alert] = "You must be logged in to edit your profile."
+      redirect_to login_path
+    end
+  end
+end
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
index de6be79..6d16c9a 100644
--- a/app/helpers/application_helper.rb
+++ b/app/helpers/application_helper.rb
@@ -1,2 +1,5 @@
 module ApplicationHelper
+  def logged_in?
+    !session[:user_id].nil?
+  end
 end
diff --git a/app/helpers/users_helper.rb b/app/helpers/users_helper.rb
new file mode 100644
index 0000000..2310a24
--- /dev/null
+++ b/app/helpers/users_helper.rb
@@ -0,0 +1,2 @@
+module UsersHelper
+end
diff --git a/app/javascript/application.js b/app/javascript/application.js
index e239947..84b06eb 100644
--- a/app/javascript/application.js
+++ b/app/javascript/application.js
@@ -1,3 +1,18 @@
 // Configure your import map in config/importmap.rb. Read more: https://github.com/rails/importmap-rails
 import "controllers"
 import "@hotwired/turbo-rails"
+
+document.addEventListener("turbo:load", function() {
+  console.log('turbo:load');
+  const flashMessages = document.querySelectorAll(".flash");
+
+  flashMessages.forEach(flashMessage => {
+    flashMessage.querySelector('.close').addEventListener('click', () => {
+      flashMessage.parentNode.removeChild(flashMessage);
+    });
+
+    setTimeout(() => {
+      flashMessage.parentNode.removeChild(flashMessage);
+    }, 5000);
+  });
+});
\ No newline at end of file
diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb
index d8c6680..c817a0a 100644
--- a/app/views/layouts/application.html.erb
+++ b/app/views/layouts/application.html.erb
@@ -12,6 +12,24 @@
   </head>
 
   <body>
+    <nav class="main-nav bg-slate-100 border-b border-slate-200">
+      <div class="container mx-auto p-5 flex flex-row justify-between">
+        <div class="main-nav-left">
+          <%= link_to 'Conferences', conferences_path %>
+        </div>
+        <div class="main-nav-right">
+        <% if logged_in? %>
+          logged in as: <%= render partial: 'application/user_avatar', locals: { user: current_user } %>
+          <%= link_to 'Profile', profile_path %>
+          <%= link_to 'Assignments', user_assignments_path(current_user) %>
+          <%= link_to 'Logout', logout_path, data: { turbo_method: :post } %>
+        <% else %>
+          Not logged in
+        <% end %>
+        </div>
+      </div>
+    </nav>
+    <%= render partial: 'shared/flash' %>
     <main class="container mx-auto mt-8 px-5 flex">
       <%= yield %>
     </main>
diff --git a/app/views/shared/_flash.html.erb b/app/views/shared/_flash.html.erb
new file mode 100644
index 0000000..143f14f
--- /dev/null
+++ b/app/views/shared/_flash.html.erb
@@ -0,0 +1,10 @@
+<div id="flash">
+  <% flash.each do |type, message| %>
+    <div class="flash alert alert-<%= type %>">
+      <%= message %>
+      <button type="button" class="close" data-dismiss="alert" aria-label="Close">
+        <span aria-hidden="true">&times;</span>
+      </button>
+    </div>
+  <% end %>
+</div>
\ No newline at end of file
diff --git a/app/views/users/login.html.erb b/app/views/users/login.html.erb
new file mode 100644
index 0000000..7c99ca8
--- /dev/null
+++ b/app/views/users/login.html.erb
@@ -0,0 +1,10 @@
+<div>
+  <h1 class="font-bold text-4xl mb-8">Choose User</h1>
+  <ul class="flex flex-wrap gap-2">
+  <% @users.each do |user| %>
+    <li>
+      <%= button_to user.name, login_path(user_id: user.id), data: { turbo_method: :post }, class: "p-2 border hover:bg-slate-100 hover:shadow" %>
+    </li>
+  <% end %>
+</ul>
+</div>
diff --git a/app/views/users/logout.html.erb b/app/views/users/logout.html.erb
new file mode 100644
index 0000000..00a0820
--- /dev/null
+++ b/app/views/users/logout.html.erb
@@ -0,0 +1,4 @@
+<div>
+  <h1 class="font-bold text-4xl">Users#logout</h1>
+  <p>Find me in app/views/users/logout.html.erb</p>
+</div>
diff --git a/app/views/users/profile.html.erb b/app/views/users/profile.html.erb
new file mode 100644
index 0000000..f655398
--- /dev/null
+++ b/app/views/users/profile.html.erb
@@ -0,0 +1,28 @@
+<div>
+  <h1 class="font-bold text-4xl">Profile</h1>
+  <%= form_with(model: @user, url: update_profile_path, local: true) do |form| %>
+    <div class="field">
+      <%= form.label :name %>
+      <%= form.text_field :name %>
+    </div>
+
+    <div class="field">
+      <%= form.label :email %>
+      <%= form.text_field :email %>
+    </div>
+
+    <div class="field">
+      <%= form.label :avatar_color %>
+      <%= form.color_field :avatar_color %>
+    </div>
+
+    <div class="field">
+      <%= form.label :telegram_username %>
+      <%= form.text_field :telegram_username %>
+    </div>
+
+    <div class="actions">
+      <%= form.submit "Update Profile" %>
+    </div>
+  <% end %>
+</div>
diff --git a/app/views/users/update.html.erb b/app/views/users/update.html.erb
new file mode 100644
index 0000000..7ead44d
--- /dev/null
+++ b/app/views/users/update.html.erb
@@ -0,0 +1,4 @@
+<div>
+  <h1 class="font-bold text-4xl">Users#update</h1>
+  <p>Find me in app/views/users/update.html.erb</p>
+</div>
diff --git a/config/routes.rb b/config/routes.rb
index 6f22f91..80999f2 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -1,4 +1,10 @@
 Rails.application.routes.draw do
+  get 'login', to: 'users#login', as: :login
+  post 'login', to: 'users#login'
+  post 'logout', to: 'users#logout', as: :logout
+  get 'profile', to: 'users#profile', as: :profile
+  patch 'profile', to: 'users#update_profile', as: :update_profile
+  resources :users, only: [:show]
   # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
 
   # Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500.
diff --git a/test/controllers/users_controller_test.rb b/test/controllers/users_controller_test.rb
new file mode 100644
index 0000000..6753bae
--- /dev/null
+++ b/test/controllers/users_controller_test.rb
@@ -0,0 +1,23 @@
+require "test_helper"
+
+class UsersControllerTest < ActionDispatch::IntegrationTest
+  test "should get login" do
+    get users_login_url
+    assert_response :success
+  end
+
+  test "should get logout" do
+    get users_logout_url
+    assert_response :success
+  end
+
+  test "should get show" do
+    get users_show_url
+    assert_response :success
+  end
+
+  test "should get update" do
+    get users_update_url
+    assert_response :success
+  end
+end
-- 
GitLab