55 lines
1.6 KiB
JavaScript
55 lines
1.6 KiB
JavaScript
// Timetable group list page renderer.
|
|
|
|
const { renderView } = require('../../../view');
|
|
const { normalizeTimeZone } = require('../../../../data/timetables');
|
|
|
|
function formatDateInTimeZone(value, timeZone) {
|
|
if (!value) {
|
|
return '';
|
|
}
|
|
|
|
const date = new Date(value);
|
|
if (Number.isNaN(date.getTime())) {
|
|
return '';
|
|
}
|
|
|
|
const resolvedTimeZone = normalizeTimeZone(timeZone, 'Europe/London');
|
|
return new Intl.DateTimeFormat('en-US', {
|
|
timeZone: resolvedTimeZone,
|
|
month: 'short',
|
|
day: '2-digit',
|
|
year: 'numeric',
|
|
hour: 'numeric',
|
|
minute: '2-digit',
|
|
second: '2-digit'
|
|
}).format(date);
|
|
}
|
|
|
|
function formatNextStartLabel(value, timeZone) {
|
|
if (!value) {
|
|
return 'No entries';
|
|
}
|
|
|
|
const label = formatDateInTimeZone(value, timeZone) || String(value);
|
|
return label || 'No entries';
|
|
}
|
|
|
|
module.exports = function renderTimetableGroupsPage(data, message, currentUser) {
|
|
const timetableGroups = (data.timetableGroups || []).map(function (timetableGroup) {
|
|
const timeZone = normalizeTimeZone(timetableGroup.timezone, 'Europe/London');
|
|
return Object.assign({}, timetableGroup, {
|
|
timeZone: timeZone,
|
|
nextStartLabel: formatNextStartLabel(timetableGroup.next_start_datetime, timeZone),
|
|
nextStartValue: timetableGroup.next_start_datetime ? new Date(timetableGroup.next_start_datetime).toISOString() : ''
|
|
});
|
|
});
|
|
|
|
return renderView('data-sources/timetables/list', {
|
|
title: 'Timetables',
|
|
active: 'timetables',
|
|
message: message,
|
|
currentUser: currentUser || null,
|
|
timetableGroups: timetableGroups,
|
|
pagination: data.pagination || null
|
|
});
|
|
}; |