1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# frozen_string_literal: true

require "phlex"

require_relative "../layout"

module RankKing
  module Views
    module ListViews
      class Compare < Phlex::HTML
        RECENT = 5

        def initialize(list:, source:, axis:, csrf:)
          @list = list
          @source = source
          @axis = axis
          @csrf = csrf
        end

        def view_template
          render Layout.new(title: "#{@axis.name} · #{@list.name} — Rank King") do
            render PageHeader.new(title: @list.name, back: "/lists/#{@list.id}?axis=#{@axis.id}", back_label: @list.name)

            section do
              h2(class: "type-label") { "Which wins on #{@axis.name}?" }
              render_matchup
            end

            render_recent
          end
        end

        private

        def render_matchup
          first, second = @list.items.sample(2)

          div(data_matchup: true) do
            render_choice(first, second)
            render_choice(second, first)
          end

          div(data_matchup_actions: true) do
            render_tie(first, second)
            a(href: "/lists/#{@list.id}/axes/#{@axis.id}/compare", class: "btn", data_variant: "ghost") { "Skip" }
          end
        end

        def render_choice(winner, loser)
          result_form(winner, loser) do
            button(type: "submit") { render @source.item(winner) }
          end
        end

        def render_tie(first, second)
          result_form(first, second) do
            input(type: "hidden", name: "tie", value: "1")
            button(type: "submit", data_variant: "ghost", aria_label: "Tie: #{first.name} and #{second.name}") { "Tie" }
          end
        end

        def result_form(winner, loser)
          action = "/lists/#{@list.id}/results"
          form(method: "post", action: action) do
            input(type: "hidden", name: "_csrf", value: @csrf.call(action))
            input(type: "hidden", name: "axis_id", value: @axis.id)
            input(type: "hidden", name: "winner_id", value: winner.id)
            input(type: "hidden", name: "loser_id", value: loser.id)
            yield
          end
        end

        def render_recent
          results = @axis.results_dataset.reverse(:id).limit(RECENT).all
          return if results.empty?

          section do
            h2(class: "type-label") { "Recent" }
            ul(class: "card") do
              results.each do |result|
                li do
                  span do
                    strong { result.winner.name }
                    if result.tie
                      plain " tied "
                      strong { result.loser.name }
                    else
                      plain " over #{result.loser.name}"
                    end
                  end
                end
              end
            end
          end
        end
      end
    end
  end
end