-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2064 from alphagov/transition-downtime-message-js
Transition downtime message Javascript
- Loading branch information
Showing
7 changed files
with
595 additions
and
180 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
//= require_directory ./modules | ||
|
||
//= require govuk_publishing_components/dependencies | ||
//= require govuk_publishing_components/all_components |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
//= require govuk_publishing_components/vendor/polyfills/closest | ||
|
||
window.GOVUK = window.GOVUK || {} | ||
window.GOVUK.Modules = window.GOVUK.Modules || {}; | ||
|
||
(function (Modules) { | ||
function DowntimeMessage ($module) { | ||
this.module = $module | ||
} | ||
|
||
DowntimeMessage.prototype.init = function () { | ||
const form = this.module | ||
|
||
form.addEventListener('change', updateMessage) | ||
|
||
function updateMessage () { | ||
const fromDate = getDate('from') | ||
const toDate = getDate('to') | ||
|
||
let message = '' | ||
if (isValidSchedule(fromDate, toDate)) { | ||
message = downtimeMessage(fromDate, toDate) | ||
} | ||
form.elements.message.value = message | ||
} | ||
|
||
function getDate (selector) { | ||
const day = form.elements[`${selector}-date[day]`].value | ||
const month = form.elements[`${selector}-date[month]`].value | ||
const year = form.elements[`${selector}-date[year]`].value | ||
const hours = form.elements[`${selector}-time[hour]`].value | ||
const minutes = form.elements[`${selector}-time[minute]`].value | ||
|
||
// The Date class treats 1 as February, but in the UI we expect 1 to be January | ||
const zeroIndexedMonth = parseInt(month) - 1 | ||
return new Date(year, zeroIndexedMonth, day, hours, minutes) | ||
} | ||
|
||
function isValidSchedule (fromDate, toDate) { | ||
return toDate.getTime() > fromDate.getTime() | ||
} | ||
|
||
function downtimeMessage (fromDate, toDate) { | ||
let message = 'This service will be unavailable from' | ||
const fromDayText = getDayText(fromDate) | ||
const fromTime = getTime(fromDate) | ||
const toTime = getTime(toDate) | ||
const toDayText = getDayText(toDate) | ||
const sameDay = isSameDay(fromDate, toDate) | ||
|
||
if (!isValidSchedule(fromDate, toDate)) { | ||
return '' | ||
} | ||
|
||
if (sameDay) { | ||
message = `${message} ${fromTime} to ${toTime} on ${fromDayText}.` | ||
} else { | ||
message = `${message} ${fromTime} on ${fromDayText} to ${toTime} on ${toDayText}.` | ||
} | ||
|
||
return message | ||
} | ||
|
||
function getTime (date) { | ||
const time = date.toLocaleString( | ||
'en-GB', | ||
{ hour: 'numeric', minute: 'numeric', hourCycle: 'h12' }) | ||
return time.replace(/:00/, '') | ||
.replace(/ am/, 'am') | ||
.replace(/ pm/, 'pm') | ||
.replace(/12am/, 'midnight') | ||
.replace(/12pm/, 'midday') | ||
} | ||
|
||
function getDayText (date) { | ||
const dayText = date.toLocaleDateString( | ||
'en-GB', | ||
{ weekday: 'long', month: 'long', day: 'numeric' } | ||
) | ||
return dayText.replace(/,/, '') | ||
} | ||
|
||
function isSameDay (fromDate, toDate) { | ||
// Treat a midnight stop date as being on the same day as | ||
// the hours before it. eg | ||
// Unavailable from 10pm to midnight on Thursday 8 January | ||
const toDateOneMinuteEarlier = new Date(toDate.valueOf()) | ||
toDateOneMinuteEarlier.setMinutes(toDate.getMinutes() - 1) | ||
|
||
return fromDate.getFullYear() === toDateOneMinuteEarlier.getFullYear() && | ||
fromDate.getMonth() === toDateOneMinuteEarlier.getMonth() && | ||
fromDate.getDate() === toDateOneMinuteEarlier.getDate() | ||
} | ||
} | ||
|
||
Modules.DowntimeMessage = DowntimeMessage | ||
})(window.GOVUK.Modules) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.