Skip to content

Commit

Permalink
Merge pull request #252 from kortirso/issue_221
Browse files Browse the repository at this point in the history
IS-221 modified rendering championships data
  • Loading branch information
kortirso authored Feb 6, 2024
2 parents 0b7b3c8 + 9e6cef8 commit dea3a5f
Show file tree
Hide file tree
Showing 10 changed files with 161 additions and 39 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

### Modified
- saving games without week
- rendering championships data

### Fixed
- rendering last points for list view at squad page
Expand Down
6 changes: 5 additions & 1 deletion app/assets/stylesheets/application.tailwind.css
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@
}

.season-link {
@apply relative h-40;
@apply relative;
}

.season-link .season-link-background {
Expand Down Expand Up @@ -348,6 +348,10 @@
@apply badge bg-orange-700 border border-orange-800 text-white;
}

.badge-small {
@apply px-1 py-0.5 text-xs;
}

.modal {
@apply relative p-4 sm:p-8 bg-white rounded overflow-y-scroll;

Expand Down
85 changes: 79 additions & 6 deletions app/controllers/homes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,95 @@

class HomesController < ApplicationController
before_action :find_seasons
before_action :find_overall_leagues
before_action :find_user_fantasy_teams
before_action :find_user_lineups
before_action :find_deadlines
before_action :sort_seasons

def show; end

private

def find_seasons
@seasons = Season.active.or(Season.coming).includes(:league).order('leagues.id ASC')
@seasons = @seasons.where(leagues: { sport_kind: League.sport_kinds[params['sport_kind']] }) if params['sport_kind']
@seasons = @seasons.hashable_pluck(:id, :uuid, :start_at, :name, 'leagues.name', 'leagues.sport_kind')
@seasons =
Rails.cache.fetch('homes_show_seasons_v1', expires_in: 24.hours, race_condition_ttl: 10.seconds) do
Season.active.or(Season.coming)
.joins(:league)
.hashable_pluck(:id, :uuid, :start_at, :name, :updated_at, 'leagues.name', 'leagues.sport_kind')
end
end

def find_overall_leagues
@overall_leagues =
FantasyLeague
.where(name: 'Overall', leagueable_type: 'Season', leagueable_id: @seasons.pluck(:id))
.hashable_pluck(:fantasy_leagues_teams_count, :leagueable_id)
end

def find_user_fantasy_teams
@user_fantasy_teams =
FantasyTeam
.where(user: Current.user, season_id: @seasons.pluck(:id))
.hashable_pluck(:uuid, :name, :completed, :season_id)
if @seasons.any?
FantasyTeam
.where(user: Current.user, season_id: @seasons.pluck(:id))
.hashable_pluck(:id, :uuid, :name, :completed, :season_id)
else
[]
end
end

def find_user_lineups
@user_lineups =
if @user_fantasy_teams.any?
Lineup
.joins(:week)
.where(weeks: { status: Week::ACTIVE })
.where(fantasy_team_id: @user_fantasy_teams.pluck(:id))
.hashable_pluck(:points, :final_points, :fantasy_team_id)
else
[]
end
end

def find_deadlines
@deadlines =
if @seasons.any?
Rails.cache.fetch(
['homes_show_deadlines_v1', @seasons.pluck(:updated_at).max],
expires_in: 24.hours,
race_condition_ttl: 10.seconds
) do
Week
.coming
.where(season_id: @seasons.pluck(:id))
.hashable_pluck(:deadline_at, :season_id)
end
else
[]
end
end

def sort_seasons
return if @user_fantasy_teams.blank?

@seasons =
@seasons
.sort_by do |season|
team = @user_fantasy_teams.find { |element| element[:season_id] == season[:id] }
[completed_priority(team), week_deadline_priority(season[:id])]
end
end

def completed_priority(team)
return 2 unless team

team[:completed] ? 0 : 1
end

def week_deadline_priority(season_id)
week = @deadlines.find { |element| element[:season_id] == season_id }
return 0 unless week

week[:deadline_at]
end
end
18 changes: 3 additions & 15 deletions app/views/components/page_wrappers/page_component.html.erb
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
<div class="flex flex-col lg:flex-row flex-1 max-h-screen">
<div class="w-full lg:w-64 bg-stone-700 flex flex-col px-4 text-lg">
<div class="flex-1">
<% cache(['page_component_top_v1', I18n.locale], expires_in: 24.hours) do %>
<section class="flex flex-col">
<%= link_to t('components.views.shared.fantasy_team_navigation_component.home'), home_path, class: 'p-4 mt-2 text-xl text-white' %>
<div class="hidden lg:block">
<%= link_to home_path(sport_kind: Sportable::FOOTBALL), class: 'user-navigation-link' do %>
<%= image_tag 'icons/football.svg' %>
<span><%= t('views.rules.index.football') %></span>
<% end %>
<%= link_to home_path(sport_kind: Sportable::BASKETBALL), class: 'user-navigation-link' do %>
<%= image_tag 'icons/basketball.svg' %>
<span><%= t('views.rules.index.basketball') %></span>
<% end %>
</div>
</section>
<% end %>
<section class="flex flex-col">
<%= link_to t('components.views.shared.fantasy_team_navigation_component.home'), home_path, class: 'p-4 mt-2 text-xl text-white' %>
</section>
<% if @fantasy_team&.completed? %>
<section class="flex flex-wrap lg:flex-col px-4 lg:px-0 mb-4 lg:mb-0 mt-0 lg:mt-4">
<h2 class="hidden lg:block p-4 pl-4 text-xl text-white m-0">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,61 @@
<% if @fantasy_team %>
<% if @fantasy_team[:completed] %>
<%= link_to fantasy_team_points_path(@fantasy_team[:uuid]), class: 'season-link' do %>
<%= content %>
<%= image_tag league_background(@season[:leagues_name]['en']), alt: 'league-background', class: 'season-link-background' %>
<div class="text-center py-1 bg-amber-200 border-t border-amber-300 font-semibold">
<%= @fantasy_team[:name] %>
</div>
<% if @deadline %>
<div class="badge-light badge-small absolute right-2 top-2">
<span>Next week starts at <%= @deadline[:deadline_at].strftime('%Y-%m-%d %H:%M') %></span>
</div>
<% end %>
<% if @lineup %>
<div class="badge-light badge-small absolute right-2 bottom-10">
<% if @lineup[:final_points] %>
<span>Week final points <%= @lineup[:points] %></span>
<% else %>
<span>Week points <%= @lineup[:points] %></span>
<% end %>
</div>
<% else %>
<div class="badge-light badge-small absolute right-2 bottom-10">
<span>No active week</span>
</div>
<% end %>
<div class="season-link-content">
<%= image_tag "icons/#{@season[:leagues_sport_kind]}.svg", class: 'season-link-sport' %>
</div>
<% end %>
<% else %>
<%= link_to fantasy_team_transfers_path(@fantasy_team[:uuid]), class: 'season-link' do %>
<%= content %>
<%= image_tag league_background(@season[:leagues_name]['en']), alt: 'league-background', class: 'season-link-background' %>
<div class="text-center py-1 border-t border-stone-300 font-semibold">
Make transfers
</div>
<% if @deadline %>
<div class="badge-light badge-small absolute right-2 top-2">
<span>Next week starts at <%= @deadline[:deadline_at].strftime('%Y-%m-%d %H:%M') %></span>
</div>
<% end %>
<div class="season-link-content">
<%= image_tag "icons/#{@season[:leagues_sport_kind]}.svg", class: 'season-link-sport' %>
</div>
<% end %>
<% end %>
<% else %>
<%= button_to fantasy_teams_path(season_id: @season_uuid), class: 'season-link inline-block w-full' do %>
<%= content %>
<%= button_to fantasy_teams_path(season_id: @season[:uuid]), class: 'season-link inline-block w-full' do %>
<%= image_tag league_background(@season[:leagues_name]['en']), alt: 'league-background', class: 'season-link-background' %>
<div class="text-center py-1 border-t border-stone-300">
<strong>Join league</strong> (<%= @overall_league[:fantasy_leagues_teams_count] %> members)
</div>
<% if @deadline %>
<div class="badge-light badge-small absolute right-2 top-2">
<span>Next week starts at <%= @deadline[:deadline_at].strftime('%Y-%m-%d %H:%M') %></span>
</div>
<% end %>
<div class="season-link-content">
<%= image_tag "icons/#{@season[:leagues_sport_kind]}.svg", class: 'season-link-sport' %>
</div>
<% end %>
<% end %>
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ module Views
module Homes
module Show
class FantasyTeamLinkComponent < ApplicationViewComponent
def initialize(season_uuid:, fantasy_team: nil)
@season_uuid = season_uuid
def initialize(season:, fantasy_team: nil, lineup: nil, deadline: nil, overall_league: nil)
@season = season
@fantasy_team = fantasy_team
@lineup = lineup
@deadline = deadline
@overall_league = overall_league

super()
end
Expand Down
17 changes: 8 additions & 9 deletions app/views/controllers/homes/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@
<% if @seasons.any? %>
<section class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-4">
<% @seasons.each do |season| %>
<div class="flex flex-col justify-between bg-white rounded overflow-hidden h-40 border border-stone-300">
<div class="flex flex-col justify-between bg-white rounded overflow-hidden border border-stone-300">
<% if season[:start_at].nil? || DateTime.now > season[:start_at] %>
<%- fantasy_team = @user_fantasy_teams.find { |element| element[:season_id] == season[:id] } %>
<%=
render Views::Homes::Show::FantasyTeamLinkComponent.new(
season_uuid: season[:uuid],
fantasy_team: @user_fantasy_teams.find { |element| element[:season_id] == season[:id] }
) do
season: season,
fantasy_team: fantasy_team,
lineup: fantasy_team ? @user_lineups.find { |element| element[:fantasy_team_id] == fantasy_team[:id] } : nil,
deadline: @deadlines.find { |element| element[:season_id] == season[:id] },
overall_league: @overall_leagues.find { |element| element[:leagueable_id] == season[:id] }
)
%>
<%= image_tag league_background(season[:leagues_name]['en']), alt: 'league-background', class: 'season-link-background' %>
<div class="season-link-content">
<%= image_tag "icons/#{season[:leagues_sport_kind]}.svg", class: 'season-link-sport' %>
</div>
<% end %>
<% else %>
<div class="season-link">
<%= image_tag league_background(season[:leagues_name]['en']), alt: 'league-background', class: 'season-link-background' %>
Expand Down
2 changes: 1 addition & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ en:
logout: Logout
change_locale: Change locale
fantasy_team_navigation_component:
home: Home
home: Championships
team: Team squad
transfers: Transfers
points: Points
Expand Down
2 changes: 1 addition & 1 deletion config/locales/ru.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ ru:
logout: Выход
change_locale: Изменить локаль
fantasy_team_navigation_component:
home: Главная
home: Чемпионаты
team: Состав команды
transfers: Трансферы
points: Результаты
Expand Down
8 changes: 8 additions & 0 deletions spec/controllers/homes_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@
context 'for logged users' do
sign_in_user

before do
season = create :season, active: true
create :fantasy_league, season: season, leagueable: season, name: 'Overall'
fantasy_team = create :fantasy_team, season: season, user: @current_user
week = create :week, status: Week::ACTIVE
create :lineup, week: week, fantasy_team: fantasy_team
end

it 'renders show template' do
do_request

Expand Down

0 comments on commit dea3a5f

Please sign in to comment.