From f4106b9fb6d2c8f994213529f5ff32ecbd4e69ee Mon Sep 17 00:00:00 2001 From: Charlotte Van Petegem Date: Sun, 25 May 2025 21:54:34 +0200 Subject: [PATCH] Add basic deadline to sections index --- .../stylesheets/application.postcss.css | 24 +++++++++++++++++ app/controllers/extensions_controller.rb | 7 +++++ app/controllers/sections_controller.rb | 5 ++++ app/helpers/extensions_helper.rb | 2 ++ app/javascript/application.js | 2 ++ app/javascript/deadline_display.js | 27 +++++++++++++++++++ app/models/extension.rb | 21 +++++++++++++++ app/views/sections/index.html.erb | 18 +++++++++++++ config/application.rb | 2 ++ config/routes.rb | 6 +++++ .../20250525115606_create_extensions.rb | 10 +++++++ db/schema.rb | 9 ++++++- .../controllers/extensions_controller_test.rb | 7 +++++ test/fixtures/extensions.yml | 19 +++++++++++++ test/models/extension_test.rb | 17 ++++++++++++ 15 files changed, 175 insertions(+), 1 deletion(-) create mode 100644 app/controllers/extensions_controller.rb create mode 100644 app/helpers/extensions_helper.rb create mode 100644 app/javascript/deadline_display.js create mode 100644 app/models/extension.rb create mode 100644 db/migrate/20250525115606_create_extensions.rb create mode 100644 test/controllers/extensions_controller_test.rb create mode 100644 test/fixtures/extensions.yml create mode 100644 test/models/extension_test.rb diff --git a/app/assets/stylesheets/application.postcss.css b/app/assets/stylesheets/application.postcss.css index fd69fce..215f2d4 100644 --- a/app/assets/stylesheets/application.postcss.css +++ b/app/assets/stylesheets/application.postcss.css @@ -44,6 +44,30 @@ header { text-decoration-line: underline; } +.deadline-info-container { + padding: 2rem 0; + display: flex; + flex-direction: column; + gap: 2rem; +} + +.extension-grid { + display: grid; + grid-template-columns: 1fr 1fr; + gap: 3rem; +} + +.extension { + align-self: stretch; + color: black; + background-color: white; + padding: 1rem; + display: flex; + flex-direction: row; + align-items: center; + justify-content: space-between; +} + .title-card-grid { display: grid; grid-template-columns: 1fr 1fr 1fr; diff --git a/app/controllers/extensions_controller.rb b/app/controllers/extensions_controller.rb new file mode 100644 index 0000000..10d9484 --- /dev/null +++ b/app/controllers/extensions_controller.rb @@ -0,0 +1,7 @@ +class ExtensionsController < ApplicationController + def confirm + Extension.find(params[:id]).confirm! + + redirect_to sections_url + end +end diff --git a/app/controllers/sections_controller.rb b/app/controllers/sections_controller.rb index 7ab8cba..dd1bece 100644 --- a/app/controllers/sections_controller.rb +++ b/app/controllers/sections_controller.rb @@ -1,6 +1,11 @@ class SectionsController < ApplicationController def index @sections = Section.all.order(id: :asc) + @extensions = Extension.all.order(id: :asc) + @deadline = Time.zone.local(2025, 7, 8, 0, 0, 0) + @extensions.each do |e| + @deadline = e.affect_deadline(@deadline) + end end def show diff --git a/app/helpers/extensions_helper.rb b/app/helpers/extensions_helper.rb new file mode 100644 index 0000000..383b12c --- /dev/null +++ b/app/helpers/extensions_helper.rb @@ -0,0 +1,2 @@ +module ExtensionsHelper +end diff --git a/app/javascript/application.js b/app/javascript/application.js index 391047e..f3bc11e 100644 --- a/app/javascript/application.js +++ b/app/javascript/application.js @@ -5,6 +5,7 @@ import initPoliticianQuestions from './politicians_answer.js' import initAcrosticQuestions from './acrostic_answer.js' import initConnectionsQuestions from './connections_answer.js' import initLyricsQuestions from './lyrics_answer.js' +import initDeadlineDisplay from './deadline_display.js' import { Sortable, Swap } from 'sortablejs' @@ -17,4 +18,5 @@ addEventListener("DOMContentLoaded", () => { initAcrosticQuestions() initConnectionsQuestions() initLyricsQuestions() + initDeadlineDisplay() }) diff --git a/app/javascript/deadline_display.js b/app/javascript/deadline_display.js new file mode 100644 index 0000000..0c25cb2 --- /dev/null +++ b/app/javascript/deadline_display.js @@ -0,0 +1,27 @@ +export default function initDeadlineDisplay() { + document.querySelectorAll('[data-behaviour="deadline_display"]').forEach((span) => { + const deadline = new Date(span.dataset.deadline) + + function update() { + const remaining = Math.floor((deadline - new Date()) / 1000) + const days = Math.floor(remaining / (60 * 60 * 24)) + const hours = Math.floor((remaining % (60 * 60 * 24)) / (60 * 60)) + const minutes = Math.floor((remaining % (60 * 60)) / 60) + const seconds = remaining % 60 + let remainingText = "" + if (days > 0) { + remainingText += `${days} dagen, ` + } + if (days > 0 || hours > 0) { + remainingText += `${hours} uren, ` + } + if (days > 0 || hours > 0 || minutes > 0) { + remainingText += `${minutes} minuten en ` + } + remainingText += `${seconds} seconden` + span.innerText = remainingText + } + + setInterval(update, 200) + }) +} diff --git a/app/models/extension.rb b/app/models/extension.rb new file mode 100644 index 0000000..ac27a27 --- /dev/null +++ b/app/models/extension.rb @@ -0,0 +1,21 @@ +# == Schema Information +# +# Table name: extensions +# +# id :integer not null, primary key +# confirmed :boolean +# reason :text +# created_at :datetime not null +# updated_at :datetime not null +# +class Extension < ApplicationRecord + def affect_deadline(deadline) + return deadline unless confirmed + + deadline + 6.weeks + end + + def confirm! + update!(confirmed: true) + end +end diff --git a/app/views/sections/index.html.erb b/app/views/sections/index.html.erb index b99360f..8883cb9 100644 --- a/app/views/sections/index.html.erb +++ b/app/views/sections/index.html.erb @@ -1,3 +1,21 @@ +
+
+ Je hebt nog + tijd tot <%= @deadline.to_fs(:short) %>. +
+ Je kan extra tijd krijgen door volgende dingen te organiseren. Je krijgt dan 6 weken extra. +
+ <% @extensions.each do |extension| %> +
+ <%= extension.reason %> + <%= form_with url: confirm_extension_path(extension), method: :put do |form| %> + <%= form.submit "Bevestig", disabled: extension.confirmed %> + <% end %> +
+ <% end %> +
+
+
<% @sections.each do |section| %> <%= link_to(section_url(section), html_options = { class: 'title-card-container' }) do %> diff --git a/config/application.rb b/config/application.rb index 8187854..d6e5619 100644 --- a/config/application.rb +++ b/config/application.rb @@ -28,6 +28,8 @@ module EntranceExam # Common ones are `templates`, `generators`, or `middleware`, for example. config.autoload_lib(ignore: %w[assets tasks]) + config.time_zone = 'Europe/Brussels' + # Configuration for the application, engines, and railties goes here. # # These settings can be overridden in specific environments using the files diff --git a/config/routes.rb b/config/routes.rb index 23a99af..d63773a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -21,4 +21,10 @@ Rails.application.routes.draw do put :answer end end + + resources "extensions", only: [] do + member do + put :confirm + end + end end diff --git a/db/migrate/20250525115606_create_extensions.rb b/db/migrate/20250525115606_create_extensions.rb new file mode 100644 index 0000000..fe606be --- /dev/null +++ b/db/migrate/20250525115606_create_extensions.rb @@ -0,0 +1,10 @@ +class CreateExtensions < ActiveRecord::Migration[8.0] + def change + create_table :extensions do |t| + t.text :reason + t.boolean :confirmed + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 0a765e0..977373f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[8.0].define(version: 2025_05_15_124656) do +ActiveRecord::Schema[8.0].define(version: 2025_05_25_115606) do create_table "active_storage_attachments", force: :cascade do |t| t.string "name", null: false t.string "record_type", null: false @@ -47,6 +47,13 @@ ActiveRecord::Schema[8.0].define(version: 2025_05_15_124656) do t.index ["question_id"], name: "index_answers_on_question_id" end + create_table "extensions", force: :cascade do |t| + t.text "reason" + t.boolean "confirmed" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + create_table "questions", force: :cascade do |t| t.integer "section_id", null: false t.text "text", null: false diff --git a/test/controllers/extensions_controller_test.rb b/test/controllers/extensions_controller_test.rb new file mode 100644 index 0000000..652a5b4 --- /dev/null +++ b/test/controllers/extensions_controller_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class ExtensionsControllerTest < ActionDispatch::IntegrationTest + # test "the truth" do + # assert true + # end +end diff --git a/test/fixtures/extensions.yml b/test/fixtures/extensions.yml new file mode 100644 index 0000000..133044a --- /dev/null +++ b/test/fixtures/extensions.yml @@ -0,0 +1,19 @@ +# == Schema Information +# +# Table name: extensions +# +# id :integer not null, primary key +# confirmed :boolean +# reason :text +# created_at :datetime not null +# updated_at :datetime not null +# +extension_housewarming: + id: 2 + reason: Housewarming + confirmed: false + +extension_dinner: + id: 1 + reason: Diner + confirmed: false diff --git a/test/models/extension_test.rb b/test/models/extension_test.rb new file mode 100644 index 0000000..caa83a0 --- /dev/null +++ b/test/models/extension_test.rb @@ -0,0 +1,17 @@ +# == Schema Information +# +# Table name: extensions +# +# id :integer not null, primary key +# confirmed :boolean +# reason :text +# created_at :datetime not null +# updated_at :datetime not null +# +require "test_helper" + +class ExtensionTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end