mirror of
https://github.com/Freika/dawarich.git
synced 2026-01-10 01:01:39 -05:00
Persist app theme in user settings
This commit is contained in:
parent
8c70dd484a
commit
90f6c82dc1
11 changed files with 91 additions and 12 deletions
File diff suppressed because one or more lines are too long
11
app/controllers/settings_controller.rb
Normal file
11
app/controllers/settings_controller.rb
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class SettingsController < ApplicationController
|
||||
before_action :authenticate_user!
|
||||
|
||||
def theme
|
||||
current_user.update(theme: params[:theme])
|
||||
|
||||
redirect_back(fallback_location: root_path)
|
||||
end
|
||||
end
|
||||
|
|
@ -1,3 +1,5 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module ApplicationHelper
|
||||
def classes_for_flash(flash_type)
|
||||
case flash_type.to_sym
|
||||
|
|
@ -47,7 +49,7 @@ module ApplicationHelper
|
|||
Stat.year_distance(year).sum { _1[1] }
|
||||
end
|
||||
|
||||
def is_past?(year, month)
|
||||
def past?(year, month)
|
||||
DateTime.new(year, month).past?
|
||||
end
|
||||
|
||||
|
|
@ -66,4 +68,8 @@ module ApplicationHelper
|
|||
def app_version
|
||||
File.read('.app_version').strip
|
||||
end
|
||||
|
||||
def app_theme
|
||||
current_user&.theme == 'light' ? 'light' : 'dark'
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,12 +1,12 @@
|
|||
<!DOCTYPE html>
|
||||
<html data-theme="dark">
|
||||
<html data-theme="<%= app_theme %>">
|
||||
<head>
|
||||
<title>Da War Ich</title>
|
||||
<title>DaWarIch</title>
|
||||
<meta name="viewport" content="width=device-width,initial-scale=1">
|
||||
<%= csrf_meta_tags %>
|
||||
<%= csp_meta_tag %>
|
||||
|
||||
<link href="https://cdn.jsdelivr.net/npm/daisyui@4.9.0/dist/full.css" rel="stylesheet" type="text/css">
|
||||
<link href="https://cdn.jsdelivr.net/npm/daisyui@4.11.1/dist/full.css" rel="stylesheet" type="text/css">
|
||||
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css" integrity="sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY=" crossorigin="" />
|
||||
|
||||
<%= stylesheet_link_tag "tailwind", "inter-font", "data-turbo-track": "reload" %>
|
||||
|
|
|
|||
|
|
@ -21,11 +21,18 @@
|
|||
<% end %>
|
||||
</a>
|
||||
</div>
|
||||
<label class="flex cursor-pointer gap-2">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="5"/><path d="M12 1v2M12 21v2M4.2 4.2l1.4 1.4M18.4 18.4l1.4 1.4M1 12h2M21 12h2M4.2 19.8l1.4-1.4M18.4 5.6l1.4-1.4"/></svg>
|
||||
<input type="checkbox" value="light" class="toggle theme-controller"/>
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"></path></svg>
|
||||
</label>
|
||||
|
||||
<% if user_signed_in? %>
|
||||
<% if current_user.theme == 'light' %>
|
||||
<%= link_to settings_theme_path(theme: 'dark'), data: { turbo: false }, class: 'btn btn-ghost' do %>
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"></path></svg>
|
||||
<% end %>
|
||||
<% else %>
|
||||
<%= link_to settings_theme_path(theme: 'light'), data: { turbo: false }, class: 'btn btn-ghost' do %>
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="5"/><path d="M12 1v2M12 21v2M4.2 4.2l1.4 1.4M18.4 18.4l1.4 1.4M1 12h2M21 12h2M4.2 19.8l1.4-1.4M18.4 5.6l1.4-1.4"/></svg>
|
||||
<% end %>
|
||||
<% end %>
|
||||
<% end %>
|
||||
</div>
|
||||
<div class="navbar-center hidden lg:flex">
|
||||
<ul class="menu menu-horizontal px-1">
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@
|
|||
<div class='grid grid-cols-3 gap-3'>
|
||||
<% (1..12).to_a.each_slice(3) do |months| %>
|
||||
<% months.each do |month_number| %>
|
||||
<% if is_past?(year, month_number) && points_exist?(year, month_number) %>
|
||||
<% if past?(year, month_number) && points_exist?(year, month_number) %>
|
||||
<%= link_to Date::ABBR_MONTHNAMES[month_number], points_url(timespan(month_number, year)), class: 'btn btn-default' %>
|
||||
<% else %>
|
||||
<div class='btn btn-disabled'><%= Date::ABBR_MONTHNAMES[month_number] %></div>
|
||||
|
|
|
|||
|
|
@ -1,4 +1,7 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
Rails.application.routes.draw do
|
||||
get 'settings/theme', to: 'settings#theme'
|
||||
get 'export', to: 'export#index'
|
||||
get 'export/download', to: 'export#download'
|
||||
|
||||
|
|
|
|||
7
db/migrate/20240518095848_add_theme_to_users.rb
Normal file
7
db/migrate/20240518095848_add_theme_to_users.rb
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class AddThemeToUsers < ActiveRecord::Migration[7.1]
|
||||
def change
|
||||
add_column :users, :theme, :string, default: 'dark', null: false
|
||||
end
|
||||
end
|
||||
3
db/schema.rb
generated
3
db/schema.rb
generated
|
|
@ -10,7 +10,7 @@
|
|||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema[7.1].define(version: 2024_04_25_200155) do
|
||||
ActiveRecord::Schema[7.1].define(version: 2024_05_18_095848) do
|
||||
# These are extensions that must be enabled in order to support this database
|
||||
enable_extension "plpgsql"
|
||||
|
||||
|
|
@ -118,6 +118,7 @@ ActiveRecord::Schema[7.1].define(version: 2024_04_25_200155) do
|
|||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.string "api_key", default: "", null: false
|
||||
t.string "theme", default: "dark", null: false
|
||||
t.index ["email"], name: "index_users_on_email", unique: true
|
||||
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
|
||||
end
|
||||
|
|
|
|||
|
|
@ -33,6 +33,7 @@ services:
|
|||
tty: true
|
||||
entrypoint: dev-entrypoint.sh
|
||||
command: ['bin/dev']
|
||||
restart: on-failure
|
||||
environment:
|
||||
RAILS_ENV: development
|
||||
REDIS_URL: redis://dawarich_redis:6379/0
|
||||
|
|
|
|||
43
spec/requests/settings_spec.rb
Normal file
43
spec/requests/settings_spec.rb
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe 'Settings', type: :request do
|
||||
describe 'GET /theme' do
|
||||
let(:params) { { theme: 'light' } }
|
||||
|
||||
context 'when user is not signed in' do
|
||||
it 'redirects to the sign in page' do
|
||||
get '/settings/theme', params: params
|
||||
expect(response).to redirect_to(new_user_session_path)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when user is signed in' do
|
||||
let(:user) { create(:user) }
|
||||
|
||||
before do
|
||||
sign_in user
|
||||
end
|
||||
|
||||
it 'updates the user theme' do
|
||||
get '/settings/theme', params: params
|
||||
expect(user.reload.theme).to eq('light')
|
||||
end
|
||||
|
||||
it 'redirects to the root path' do
|
||||
get '/settings/theme', params: params
|
||||
expect(response).to redirect_to(root_path)
|
||||
end
|
||||
|
||||
context 'when theme is dark' do
|
||||
let(:params) { { theme: 'dark' } }
|
||||
|
||||
it 'updates the user theme' do
|
||||
get '/settings/theme', params: params
|
||||
expect(user.reload.theme).to eq('dark')
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Reference in a new issue