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/)
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
### Added

View file

@ -16,6 +16,22 @@ class StatsController < ApplicationController
end
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|
year[:months].each do |month|
Stats::CalculatingJob.perform_later(

View file

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

View file

@ -3,8 +3,8 @@
class Stats::CalculateMonth
def initialize(user_id, year, month)
@user = User.find(user_id)
@year = year
@month = month
@year = year.to_i
@month = month.to_i
end
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;
<% end %>
<% 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 %>
</div>
<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 class="card-body">
<div class="flex justify-between items-center">
<h2 class="card-title">
<%= link_to map_url(timespan(stat.month, stat.year)), class: "underline hover:no-underline text-#{header_colors.sample}" do %>
<%= "#{Date::MONTHNAMES[stat.month]} of #{stat.year}" %>
<%= Date::MONTHNAMES[stat.month] %>
<% 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>
<% if REVERSE_GEOCODING_ENABLED %>
<div class="card-actions justify-end">

View file

@ -21,15 +21,18 @@
<% end %>
</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| %>
<div class="card w-full bg-base-200 shadow-xl">
<div class="card-body">
<h2 class="card-title text-<%= header_colors.sample %>">
<h2 class="card-title justify-between text-<%= header_colors.sample %>">
<div>
<%= 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>
<p>
<% 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
resources :stats, only: :index do
collection do
post :update
put :update_all
end
end
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'
devise_for :users, skip: [:registrations]