Add buttons to update stats for a year or a month

This commit is contained in:
Eugene Burmakin 2024-12-20 15:26:25 +01:00
parent e7a38fa55d
commit 6c18fbe41f
9 changed files with 54 additions and 17 deletions

View file

@ -1 +1 @@
0.20.2 0.20.3

View file

@ -5,6 +5,14 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/) The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/). and this project adheres to [Semantic Versioning](http://semver.org/).
# 0.20.3 - 2024-12-20
### Added
- A button on a year stats card to update stats for the whole year.
- A button on a month stats card to update stats for a specific month.
- A confirmation alert on the Notifications page before deleting all notifications.
# 0.20.2 - 2024-12-17 # 0.20.2 - 2024-12-17
### Added ### Added

View file

@ -16,6 +16,22 @@ class StatsController < ApplicationController
end end
def update def update
if params[:month] == 'all'
(1..12).each do |month|
Stats::CalculatingJob.perform_later(current_user.id, params[:year], month)
end
target = "the whole #{params[:year]}"
else
Stats::CalculatingJob.perform_later(current_user.id, params[:year], params[:month])
target = "#{Date::MONTHNAMES[params[:month].to_i]} of #{params[:year]}"
end
redirect_to stats_path, notice: "Stats for #{target} are being updated", status: :see_other
end
def update_all
current_user.years_tracked.each do |year| current_user.years_tracked.each do |year|
year[:months].each do |month| year[:months].each do |month|
Stats::CalculatingJob.perform_later( Stats::CalculatingJob.perform_later(

View file

@ -19,8 +19,8 @@ class Stats::CalculatingJob < ApplicationJob
Notifications::Create.new( Notifications::Create.new(
user:, user:,
kind: :info, kind: :info,
title: "Stats updated: #{year}-#{month}", title: "Stats updated for #{Date::MONTHNAMES[month.to_i]} of #{year}",
content: "Stats updated for #{year}-#{month}" content: "Stats updated for #{Date::MONTHNAMES[month.to_i]} of #{year}"
).call ).call
end end

View file

@ -3,8 +3,8 @@
class Stats::CalculateMonth class Stats::CalculateMonth
def initialize(user_id, year, month) def initialize(user_id, year, month)
@user = User.find(user_id) @user = User.find(user_id)
@year = year @year = year.to_i
@month = month @month = month.to_i
end end
def call def call

View file

@ -7,7 +7,7 @@
<%= link_to "Mark all as read", mark_notifications_as_read_path, method: :post, data: { turbo_method: :post }, class: "btn btn-sm btn-primary" %>&nbsp; <%= link_to "Mark all as read", mark_notifications_as_read_path, method: :post, data: { turbo_method: :post }, class: "btn btn-sm btn-primary" %>&nbsp;
<% end %> <% end %>
<% if @notifications.any? %> <% if @notifications.any? %>
<%= link_to "Delete all", delete_all_notifications_path, method: :post, data: { turbo_method: :post }, class: "btn btn-sm btn-warning" %> <%= link_to "Delete all", delete_all_notifications_path, method: :post, data: { turbo_method: :post, turbo_confirm: 'Are you sure you want to delete all notifications?' }, class: "btn btn-sm btn-warning" %>
<% end %> <% end %>
</div> </div>
<div class="mb-4"> <div class="mb-4">

View file

@ -1,10 +1,16 @@
<div id="<%= dom_id stat %>" class="card w-full bg-base-200 shadow-xl"> <div id="<%= dom_id stat %>" class="card w-full bg-base-200 shadow-xl">
<div class="card-body"> <div class="card-body">
<h2 class="card-title"> <div class="flex justify-between items-center">
<%= link_to map_url(timespan(stat.month, stat.year)), class: "underline hover:no-underline text-#{header_colors.sample}" do %> <h2 class="card-title">
<%= "#{Date::MONTHNAMES[stat.month]} of #{stat.year}" %> <%= link_to map_url(timespan(stat.month, stat.year)), class: "underline hover:no-underline text-#{header_colors.sample}" do %>
<% end %> <%= Date::MONTHNAMES[stat.month] %>
</h2> <% end %>
</h2>
<div class="flex items-center gap-2">
<%= link_to '[Update]', update_year_month_stats_path(stat.year, stat.month), data: { turbo_method: :put }, class: 'text-sm text-gray-500 hover:underline' %>
</div>
</div>
<p><%= stat.distance %><%= DISTANCE_UNIT %></p> <p><%= stat.distance %><%= DISTANCE_UNIT %></p>
<% if REVERSE_GEOCODING_ENABLED %> <% if REVERSE_GEOCODING_ENABLED %>
<div class="card-actions justify-end"> <div class="card-actions justify-end">

View file

@ -21,15 +21,18 @@
<% end %> <% end %>
</div> </div>
<%= link_to 'Update stats', stats_path, data: { 'turbo-method' => :post }, class: 'btn btn-primary mt-5' %> <%= link_to 'Update stats', update_all_stats_path, data: { turbo_method: :put }, class: 'btn btn-primary mt-5' %>
<div class="mt-5 grid grid-cols-1 sm:grid-cols-1 md:grid-cols-2 lg:grid-cols-2 gap-6 p-4"> <div class="mt-6 grid grid-cols-1 sm:grid-cols-1 md:grid-cols-2 lg:grid-cols-2 gap-6">
<% @stats.each do |year, stats| %> <% @stats.each do |year, stats| %>
<div class="card w-full bg-base-200 shadow-xl"> <div class="card w-full bg-base-200 shadow-xl">
<div class="card-body"> <div class="card-body">
<h2 class="card-title text-<%= header_colors.sample %>"> <h2 class="card-title justify-between text-<%= header_colors.sample %>">
<%= link_to year, "/stats/#{year}", class: 'underline hover:no-underline' %> <div>
<%= link_to '[Map]', map_url(year_timespan(year)), class: 'underline hover:no-underline' %> <%= link_to year, "/stats/#{year}", class: 'underline hover:no-underline' %>
<%= link_to '[Map]', map_url(year_timespan(year)), class: 'underline hover:no-underline' %>
</div>
<%= link_to '[Update]', update_year_month_stats_path(year, :all), data: { turbo_method: :put }, class: 'text-sm text-gray-500 hover:underline' %>
</h2> </h2>
<p> <p>
<% cache [current_user, 'year_distance_stat', year], skip_digest: true do %> <% cache [current_user, 'year_distance_stat', year], skip_digest: true do %>

View file

@ -41,10 +41,14 @@ Rails.application.routes.draw do
post 'notifications/destroy_all', to: 'notifications#destroy_all', as: :delete_all_notifications post 'notifications/destroy_all', to: 'notifications#destroy_all', as: :delete_all_notifications
resources :stats, only: :index do resources :stats, only: :index do
collection do collection do
post :update put :update_all
end end
end end
get 'stats/:year', to: 'stats#show', constraints: { year: /\d{4}/ } get 'stats/:year', to: 'stats#show', constraints: { year: /\d{4}/ }
put 'stats/:year/:month/update',
to: 'stats#update',
as: :update_year_month_stats,
constraints: { year: /\d{4}/, month: /\d{1,2}|all/ }
root to: 'home#index' root to: 'home#index'
devise_for :users, skip: [:registrations] devise_for :users, skip: [:registrations]