aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Étienne Deparis 2022-05-31 19:15:31 +0200
committerGravatar Étienne Deparis 2022-05-31 19:29:16 +0200
commitaedcaa43687e69a69d53277f50ee0b4ad1a7ef6b (patch)
treef8cd1b3b7217c7a7480181dcba8ae0777c34e250
parent1f6c0ce94cea6fa34f4ebbf57849954b0f510594 (diff)
downloadbiberons-aedcaa43687e69a69d53277f50ee0b4ad1a7ef6b.tar.gz
biberons-aedcaa43687e69a69d53277f50ee0b4ad1a7ef6b.tar.xz
biberons-aedcaa43687e69a69d53277f50ee0b4ad1a7ef6b.zip
Add a settings page to select language and filter columns to display
-rw-r--r--lib/biberon.rb40
-rw-r--r--locales/en.yml4
-rw-r--r--locales/fr.yml4
-rw-r--r--views/baby.slim38
-rw-r--r--views/layout.slim6
-rw-r--r--views/settings.slim32
6 files changed, 107 insertions, 17 deletions
diff --git a/lib/biberon.rb b/lib/biberon.rb
index 2ddebf1..0a8df8b 100644
--- a/lib/biberon.rb
+++ b/lib/biberon.rb
@@ -6,7 +6,6 @@ require 'r18n-core'
require_relative 'biberon/models'
R18n.default_places = File.expand_path('../locales', __dir__)
-R18n.set 'fr'
module Biberon
# Main application, associates resources with their corresponding classes
@@ -15,6 +14,9 @@ module Biberon
plugin :public, gzip: true, brotli: true
plugin :route_csrf
plugin :sessions, secret: ENV['BIBERON_SESSION_SECRET'], key: 'biberon_auth'
+ plugin :cookies, domain: ENV['BIBERON_COOKIE_DOMAIN'],
+ path: '/', secure: ENV['RACK_ENV'] != 'development',
+ httponly: true
# Log to stderr
plugin :common_logger
@@ -24,6 +26,9 @@ module Biberon
check_csrf!
+ R18n.set cookie('l', 'fr')
+ @settings = cookie('c', 'c,t,u,v,w').split(',')
+
r.root do
r.redirect "/#{session['baby_id']}" if session['baby_id']
view 'index'
@@ -49,6 +54,25 @@ module Biberon
end
end
+ r.is 'settings' do
+ r.get { view 'settings' }
+
+ r.post do
+ params = request.params
+ settings = []
+ if params['lang'] && ['fr', 'en'].include?(params['lang'])
+ settings << "l:#{params['lang']}"
+ end
+ opts = []
+ ['comment', 'temp', 'urines', 'vitamind', 'weight'].each do |opt_name|
+ opts << opt_name[0] if params[opt_name]
+ end
+ settings << "c:#{opts.join(',')}"
+ response.set_cookie('biberon_settings', settings.join(';'))
+ r.redirect '/'
+ end
+ end
+
r.on Integer do |baby_id|
r.redirect '/' unless session['baby_id'] == baby_id
@baby = ::Biberon::Baby[baby_id]
@@ -115,6 +139,20 @@ module Biberon
routing.redirect route
end
+ def cookie(key, default)
+ parse_cookie! unless @_cookie
+ @_cookie[key] || default
+ end
+
+ def parse_cookie!
+ @_cookie = {}
+ return @_cookie unless request.cookies['biberon_settings']
+ request.cookies['biberon_settings'].split(';').each do |cook|
+ cook.split(':').tap { |data| @_cookie[data[0]] = data[1] || '-' }
+ end
+ @_cookie
+ end
+
def logger(message, level = :info)
env = request.env
$stderr << "#{env['HTTP_X_FORWARDED_FOR'] || env['REMOTE_ADDR'] || '-'} - #{env['REMOTE_USER'] || '-'} [#{Time.now.strftime('%d/%b/%Y:%H:%M:%S %z')}] [#{level.to_s.upcase}] #{message}\n"
diff --git a/locales/en.yml b/locales/en.yml
index e7a3714..75e5cd5 100644
--- a/locales/en.yml
+++ b/locales/en.yml
@@ -28,3 +28,7 @@ biberon:
quantity: cannot be empty if temperature, weight or comment are empty too
graph:
back_to_bibs: Back to measures
+settings:
+ settings: Settings
+ language: Language
+ columns: Displayed columns
diff --git a/locales/fr.yml b/locales/fr.yml
index 998e8fd..ddc975b 100644
--- a/locales/fr.yml
+++ b/locales/fr.yml
@@ -29,3 +29,7 @@ biberon:
sont également vides
graph:
back_to_bibs: Retour aux mesures
+settings:
+ settings: Paramètres
+ language: Langue
+ columns: Colonnes affichées
diff --git a/views/baby.slim b/views/baby.slim
index 755c103..af10a73 100644
--- a/views/baby.slim
+++ b/views/baby.slim
@@ -21,30 +21,40 @@ table.biblist
tr
th = R18n.t.biberon.hour
th = R18n.t.biberon.quantity
- th = R18n.t.biberon.temp
+ - if @settings.include?('t')
+ th = R18n.t.biberon.temp
th = R18n.t.biberon.feces
- th = R18n.t.biberon.urines
- th = R18n.t.biberon.vitamind
- th = R18n.t.biberon.weight
- th = R18n.t.biberon.comment
+ - if @settings.include?('u')
+ th = R18n.t.biberon.urines
+ - if @settings.include?('v')
+ th = R18n.t.biberon.vitamind
+ - if @settings.include?('w')
+ th = R18n.t.biberon.weight
+ - if @settings.include?('c')
+ th = R18n.t.biberon.comment
th = R18n.t.biberon.actions
tbody
- @baby.bibs_of_day(@today).each do |bib|
tr
td = bib.hour
td = bib.quantity
- td = bib.temperature
+ - if @settings.include?('t')
+ td = bib.temperature
td
- if bib.feces
| 💩
- td
- - if bib.urines
- | 💦
- td
- - if bib.vitamind
- | ✅
- td = bib.weight
- td = bib.comment
+ - if @settings.include?('u')
+ td
+ - if bib.urines
+ | 💦
+ - if @settings.include?('v')
+ td
+ - if bib.vitamind
+ | ✅
+ - if @settings.include?('w')
+ td = bib.weight
+ - if @settings.include?('c')
+ td = bib.comment
td
form action="/#{@baby.id}/delete" method='post'
fieldset
diff --git a/views/layout.slim b/views/layout.slim
index 6989d6e..0d5b265 100644
--- a/views/layout.slim
+++ b/views/layout.slim
@@ -16,6 +16,8 @@ html
footer
- inner_span = "<span class=\"geneston\" title=\"#{R18n.t.footer.in_france}\">Geneston</span>"
p.center-text == R18n.t.footer.made_with_love(inner_span)
- - if @baby
- p.center-text
+ p.center-text
+ a> href='/settings' = R18n.t.settings.settings
+ - if session['baby_id']
+ ' -
a href='/session/logout' = R18n.t.session.logout
diff --git a/views/settings.slim b/views/settings.slim
new file mode 100644
index 0000000..cf74d5a
--- /dev/null
+++ b/views/settings.slim
@@ -0,0 +1,32 @@
+form action='/settings' method='post'
+ fieldset
+ == csrf_tag '/settings'
+
+ label for='language' = R18n.t.settings.language
+ select#language name='lang'
+ - lang = cookie('l', 'fr')
+ option value='fr' selected=(lang == 'fr') Français
+ option value='en' selected=(lang == 'en') English
+
+ - if session['baby_id']
+ fieldset
+ legend = R18n.t.settings.columns
+
+ input#comment type='checkbox' name='comment' checked=@settings.include?('c')
+ label.label-inline for='comment' = R18n.t.biberon.comment
+ br
+ input#temp type='checkbox' name='temp' checked=@settings.include?('t')
+ label.label-inline for='temp' = R18n.t.biberon.temperature
+ br
+ input#urines type='checkbox' name='urines' checked=@settings.include?('u')
+ label.label-inline for='urines' = R18n.t.biberon.urines
+ br
+ input#vitamind type='checkbox' name='vitamind' checked=@settings.include?('v')
+ label.label-inline for='vitamind' = R18n.t.biberon.vitamind
+ br
+ input#weight type='checkbox' name='weight' checked=@settings.include?('w')
+ label.label-inline for='weight' = R18n.t.biberon.weight
+
+
+ input.button-primary type='submit' value=R18n.t.save
+ input.button-outline< type='reset' value=R18n.t.cancel