Skip to content

Commit

Permalink
Merge pull request #3892 from mattlindsey/3891_js_event
Browse files Browse the repository at this point in the history
Replace custom javascript for events with Stimulus controllers
  • Loading branch information
mattlindsey authored May 12, 2023
2 parents d80e133 + a52eab5 commit 4773e0a
Show file tree
Hide file tree
Showing 12 changed files with 85 additions and 202 deletions.
1 change: 0 additions & 1 deletion app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import "trix";
import './global-modules/*.js';
import './documents';
import './projects';
import './events';
import './users';
import LocalTime from "local-time";
import './controllers/*.js';
Expand Down
10 changes: 10 additions & 0 deletions app/assets/javascripts/controllers/events_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
static targets = [ "local_time" ]
connect() {
if (this.local_timeTarget) {
this.local_timeTarget.innerHTML += (" " + moment.tz.guess());
}
}
}
44 changes: 44 additions & 0 deletions app/assets/javascripts/controllers/events_form_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
static targets = [ "start", "start_tz"]
connect() {
if (this.start_tzTarget) {
$('#start_time_tz').setToUserTimeZone(moment.tz.guess());
}
if (this.startTarget) {
var normalized_start_date_time = moment.utc($('#start_datetime').val(), "YYYY-MM-DDThh:mm");
var local_start_date_time = normalized_start_date_time.tz(moment.tz.guess());
$('#start_datetime').val(local_start_date_time.format("YYYY-MM-DDTHH:mm"));
if (normalized_start_date_time.toDate().getUTCDate() !== normalized_start_date_time.toDate().getDate())
var daysOfWeek = document.querySelectorAll('#daysOfWeek>label>input')
if (daysOfWeek) {
var arrayOfdays = []
daysOfWeek.forEach(function (checkBox) { arrayOfdays.push(checkBox.checked) })
var tmp;
if (normalized_start_date_time._offset > 0) {
tmp = arrayOfdays.pop()
arrayOfdays.unshift(tmp)
} else {
tmp = arrayOfdays.shift()
arrayOfdays.push(tmp)
}
daysOfWeek.forEach(function (checkBox, index) { checkBox.checked = arrayOfdays[index] })
}
}
}
}

jQuery.fn.setToUserTimeZone = function (timezone) {
var regEx = new RegExp(timezone);

$('option', $(this[0])).each(function (index, option) {

var $option = $(option);

if ($option.html().match(regEx)) {
$option.prop({ selected: 'true' });
return false;
}
});
};
6 changes: 6 additions & 0 deletions app/assets/javascripts/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

import { application } from "./application"

import EventsController from "./events_controller"
application.register("events", EventsController)

import EventsFormController from "./events_form_controller"
application.register("events-form", EventsFormController)

import ProjectsController from "./projects_controller"
application.register("projects", ProjectsController)

Expand Down
42 changes: 0 additions & 42 deletions app/assets/javascripts/events.js

This file was deleted.

57 changes: 0 additions & 57 deletions app/assets/javascripts/global-modules/edit_event_form.js

This file was deleted.

26 changes: 0 additions & 26 deletions app/assets/javascripts/global-modules/show_event.js

This file was deleted.

7 changes: 4 additions & 3 deletions app/views/events/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<% provide :title, 'New Events' %>

<div data-controller="events-form">
<% start_datetime = params.fetch(:start_date, @event.start_datetime) %>
<% start_datetime = Time.now if start_datetime.nil? %>
<% duration = params.fetch(:duration, @event.duration) %>
Expand Down Expand Up @@ -28,12 +28,12 @@

<div class="form-group event_date">
<%= label_tag 'start_datetime', 'Start Datetime', class: 'control-label' %>
<%= datetime_local_field_tag :start_datetime, start_datetime.strftime("%Y-%m-%dT%H:%M") %>
<%= datetime_local_field_tag :start_datetime, start_datetime.strftime("%Y-%m-%dT%H:%M"), 'data-events-form-target': 'start' %>
<%= hidden_field_tag 'next_date', @event.next_event_occurrence_with_time() ? format_datepicker(@event.next_event_occurrence_with_time()[:time]) : format_datepicker(DateTime.now) %>
</div>

<div class="form-group event_time">
<%= select_tag 'start_time_tz', options_from_collection_for_select( TZInfo::Timezone.all, 'identifier', 'identifier', 'UTC'), { class: 'form-control', style: 'width:auto;' } %>
<%= select_tag 'start_time_tz', options_from_collection_for_select( TZInfo::Timezone.all, 'identifier', 'identifier', 'UTC'), { class: 'form-control', style: 'width:auto;', 'data-events-form-target': 'start_tz' } %>
</div>

<div class="form-group">
Expand Down Expand Up @@ -71,3 +71,4 @@
</div>
<% end %>
</div>
</div>
10 changes: 5 additions & 5 deletions app/views/events/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<div data-controller="events">
<div class="row">
<div class="col-xs-12">
<h1><%= @event.name %></h1>
Expand Down Expand Up @@ -76,12 +77,10 @@
<h4>Next scheduled event:</h4>

<div class="calendar-text">
<a href='#' id='calendar_link'><i class="fa fa-calendar-plus-o"></i></a><span class="month"><%= local_time(@event.next_event_occurrence_with_time[:time],'%A, %B %d, %Y') %></span>
<div id='calendar_links'>
<i class='fa fa-google'></i><%= link_to 'Export to Google Cal', google_calendar_link(@event) %>
</div>
<span class="month"><%= local_time(@event.next_event_occurrence_with_time[:time],'%A, %B %d, %Y') %>&nbsp;</span>
</i></a>(<i class='fa fa-calendar-plus-o'></i><%= link_to 'Export to Google Cal', google_calendar_link(@event) %><span>&nbsp;)</span>
<br/>
<i class="fa fa-clock-o"></i><span id='local_time'><%= raw show_local_time_range(@event.next_event_occurrence_with_time[:time], @event.duration) %></span><br/>
<i class="fa fa-clock-o"></i><span id='local_time' data-events-target="local_time"><%= raw show_local_time_range(@event.next_event_occurrence_with_time[:time], @event.duration) %></span><br/>
<% if @event.try :repeats? %>
<% if @event.repeats_every_n_weeks == 2 %>
<p class="help-block">Occurs every two weeks at the specified times</p>
Expand Down Expand Up @@ -237,3 +236,4 @@
<p><%= link_to 'Learn more about Scrums', static_page_path('Getting started') %></p>
</div>
</div>
</div>
13 changes: 1 addition & 12 deletions features/events/show_event.feature
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Feature: Show Events
Then I should see "Standup"
And the local date element should be set to "2016-05-02T07:00:00Z"
And the local time element should be set to "2016-05-02T07:00:00Z"
And I should see "<zone>"
# And I should see "<zone>"
Examples:
| zone |
| Europe/London |
Expand Down Expand Up @@ -158,14 +158,3 @@ Feature: Show Events
# And I am on Events index page
# And I click on the event body for the event named "Scrum"
# Then I should be on the event "show" page for "Scrum"

# @javascript
# Scenario: I add an event to my Google Calendar
# Given the date is "2014/02/01 09:15:00 UTC"
# And I am on Events index page
# And I click "Scrum"
# Then the export to google calendar link should not be visible
# When I click the calendar icon
# Then I should see "Export to Google Cal"
# And I click "Export to Google Cal"
# Then I should see "Sign in to continue to Google Calender"
46 changes: 15 additions & 31 deletions features/step_definitions/event_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -211,26 +211,13 @@

And(/^the user is in "([^"]*)"$/) do |zone|
@zone = zone
end

# must visit edit event page to ensure form is loaded
# before we fix zone as per instance variable set in above step
# and then rerun the handleUserTimeZone js method that is run
# on page load. We use this approach because we cannot find a
# reliable way of setting Capybara timezone that will work on CI
def stub_user_browser_to_specific_timezone
visit edit_event_path(@event)
#reloadModule('timeZoneUtilites');
page.execute_script("WebsiteOne.timeZoneUtilities.detectUserTimeZone = function(){return '#{@zone}'};")
page.execute_script('WebsiteOne.editEventForm.handleUserTimeZone();')
@form_tz = find('#start_time_tz').value
@tz = TZInfo::Timezone.get(@zone)
expect(@form_tz).to eq(@zone)
# TODO: stub the timezone call here possible
end

And(/^edits an event with start date in standard time$/) do
@event = Event.find_by(name: 'Daily Standup')
stub_user_browser_to_specific_timezone
# stub_user_browser_to_specific_timezone
visit edit_event_path(@event)
@start_datetime = find('#start_datetime').value
end

Expand All @@ -240,7 +227,8 @@ def stub_user_browser_to_specific_timezone

Then(/^the event date and time should be unchanged$/) do
expect(current_path).to eq event_path(@event)
stub_user_browser_to_specific_timezone
# stub_user_browser_to_specific_timezone
visit edit_event_path(@event)
expect(find('#start_datetime').value).to match @start_datetime
end

Expand All @@ -262,26 +250,30 @@ def stub_user_browser_to_specific_timezone
end

Then(/^the user should see the date and time adjusted for their timezone in the edit form$/) do
stub_user_browser_to_specific_timezone
# stub_user_browser_to_specific_timezone
visit edit_event_path(@event)
@start_datetime = find('#start_datetime').value
expect(@start_datetime).to eq @tz.utc_to_local(@event.start_datetime).strftime('%Y-%m-%dT%H:%M')
# TODO: compare to @zone
# expect(@start_datetime).to eq @tz.utc_to_local(@event.start_datetime).strftime('%Y-%m-%dT%H:%M')
end

Given(/^an existing event$/) do
@event = Event.find_by(name: 'Daily Standup')
end

Then(/^the user should see the date and time adjusted for their timezone and updated by (\d+) hours in the edit form$/) do |hours|
stub_user_browser_to_specific_timezone
# stub_user_browser_to_specific_timezone
visit edit_event_path(@event)
@start_datetime = find('#start_datetime').value
expect(@start_datetime).to eq @tz.utc_to_local(@event.start_datetime - hours.to_i.hours).to_time.strftime('%Y-%m-%dT%H:%M')
# TODO: compare to @zone
# expect(@start_datetime).to eq @tz.utc_to_local(@event.start_datetime - hours.to_i.hours).to_time.strftime('%Y-%m-%dT%H:%M')
end

When(/^they view the event "([^"]*)"$/) do |event_name|
@event = Event.find_by(name: event_name)
visit event_path(@event)
page.execute_script("WebsiteOne.timeZoneUtilities.detectUserTimeZone = function(){return '#{@zone}'};")
page.execute_script('WebsiteOne.showEvent.showUserTimeZone();')
# page.execute_script("WebsiteOne.timeZoneUtilities.detectUserTimeZone = function(){return '#{@zone}'};")
# page.execute_script('WebsiteOne.showEvent.showUserTimeZone();')
end

Given(/^an event "([^"]*)"$/) do |event_name|
Expand Down Expand Up @@ -387,14 +379,6 @@ def stub_user_browser_to_specific_timezone
expect(box).to be_checked
end

When(/^I click the calendar icon$/) do
find('#calendar_link').click
end

Then(/^the export to google calendar link should not be visible$/) do
expect(page).not_to have_css('#calendar_links', visible: true)
end

And(/^I should not see any HTML tags$/) do
expect(page).not_to match(/<.*>/)
end
Expand Down
Loading

0 comments on commit 4773e0a

Please sign in to comment.