Skip to content

Commit

Permalink
Warn when selected leave days are in the past (#85)
Browse files Browse the repository at this point in the history
* Make text-warning class match the name of its variable

* Fix file resolution in asdf

* Show a warning if some leave dates are in the past

* Add test

* Expand test
  • Loading branch information
daniel-nerdgeschoss authored Feb 6, 2024
1 parent 4df746d commit 4f58712
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

source "https://rubygems.org"
ruby File.read(".ruby-version").strip
ruby File.read(File.join(__dir__, ".ruby-version")).strip

# Core
gem "puma"
Expand Down
6 changes: 5 additions & 1 deletion app/assets/stylesheets/application/util/reset.scss
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ strong {
text-align: right !important;
}

.text-warn {
.text-warning {
color: var(--color-warning);
}

.text-warn {
color: var(--color-warn);
}
18 changes: 17 additions & 1 deletion app/javascript/controllers/calendar_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,36 @@ import { Controller } from '@hotwired/stimulus';
import flatpickr from 'flatpickr';

export default class extends Controller {
static targets = ['input', 'pastWarning'];

private instance?: flatpickr.Instance;
declare readonly inputTarget: HTMLTextAreaElement;
declare readonly pastWarningTarget: HTMLDivElement;

connect(): void {
this.instance = flatpickr(this.element, {
this.handleWarning = this.handleWarning.bind(this);

this.instance = flatpickr(this.inputTarget, {
inline: true,
mode: 'multiple',
disable: [(date) => date.getDay() === 0 || date.getDay() === 6],
weekNumbers: true,
locale: {
firstDayOfWeek: 1,
},
onReady: this.handleWarning,
onChange: this.handleWarning,
});
}

handleWarning(selectedDates: Date[]): void {
const today = new Date();
today.setHours(0, 0, 0, 0);

const showWarning = selectedDates.some((date) => date < today);
this.pastWarningTarget.hidden = !showWarning;
}

disconnect(): void {
this.instance?.destroy();
}
Expand Down
4 changes: 3 additions & 1 deletion app/views/leaves/_form.html.slim
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
= form_with model: @leave, class: :stack, builder: AwesomeForm do |f|
= f.text_field :days, data: { controller: "calendar" }, style: "display: none"
.stack data-controller="calendar"
= f.text_field :days, data: { calendar_target: "input" }, style: "display: none"
.text-warn data-calendar-target="pastWarning" hidden="hidden" = t(".some_days_in_the_past")
= @leave.errors[:days].to_sentence
- if policy(@leave).permitted_attributes.include?(:user_id)
= f.input :user_id
Expand Down
2 changes: 1 addition & 1 deletion app/views/sprints/_sprint.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
- if feedback.turnover_per_storypoint
div = "#{number_to_currency feedback.turnover_per_storypoint} / point"
- if feedback.turnover_per_storypoint
div class=("text-warn" if feedback.turnover_per_storypoint_against_avarage > 1) = number_to_percentage feedback.turnover_per_storypoint_against_avarage * 100
div class=("text-warning" if feedback.turnover_per_storypoint_against_avarage > 1) = number_to_percentage feedback.turnover_per_storypoint_against_avarage * 100
- if policy(sprint).overview?
tr
th
Expand Down
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ en:
leaves:
form:
request_leave: Request leave
some_days_in_the_past: 'Heads up: Some of the selected days are in the past.'
index:
all_users: All users
leaves: Leaves
Expand Down
19 changes: 19 additions & 0 deletions spec/system/leaves_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,23 @@
expect(status.slack_id).to eq "slack-john"
expect(status.until_time).to eq Time.zone.today.end_of_day
end

it "shows a warning if one of the leave days is in the past" do
login :john
visit leaves_path
click_on "Request leave"

expect(page).not_to have_content "Heads up: Some of the selected days are in the past."

within ".modal" do
find(".flatpickr-day.today").click

expect(page).not_to have_content "Heads up: Some of the selected days are in the past."

find(".flatpickr-prev-month").click
first(".dayContainer .flatpickr-day").click
end

expect(page).to have_content "Heads up: Some of the selected days are in the past."
end
end

0 comments on commit 4f58712

Please sign in to comment.