Some more cleanup

1. typo with splitting token/session out
2. redefine `chunk` as `timeSlot`
3. move timeSlot sorting into its own method
This commit is contained in:
Matthew Taylor 2016-05-19 17:37:51 -04:00
parent 337b6dcfca
commit a477be4814
4 changed files with 47 additions and 43 deletions

View file

@ -10,7 +10,7 @@ var Types = keyMirror({
module.exports.scheduleReducer = function (state, action) { module.exports.scheduleReducer = function (state, action) {
if (typeof state === 'undefined') { if (typeof state === 'undefined') {
state = { state = {
chunks: [], timeSlots: [],
day: '' day: ''
}; };
} }
@ -54,6 +54,28 @@ module.exports.startGetSchedule = function (day) {
}; };
}; };
// group periods of time by start time
module.exports.sortTimeSlots = function (timeSlot1, timeSlot2) {
var timeSlot1Am = (timeSlot1.time.substr(timeSlot1.time.length - 1, timeSlot1.time.length) === 'a') ? true : false;
var timeSlot2Am = (timeSlot2.time.substr(timeSlot2.time.length - 1, timeSlot2.time.length) === 'a') ? true : false;
var timeSlot1Time = parseInt(timeSlot1.time.substr(0, timeSlot1.time.length - 1));
var timeSlot2Time = parseInt(timeSlot2.time.substr(0, timeSlot2.time.length - 1));
if (timeSlot1Time < timeSlot2Time) {
if (timeSlot2Am && !timeSlot1Am) {
return 1;
} else {
return -1;
}
} else {
if (timeSlot1Am && !timeSlot2Am) {
return -1;
} else {
return 1;
}
}
};
/** /**
* Gets the schedule for the given day from the api * Gets the schedule for the given day from the api
* @param {String} day Day of the conference (Thursday, Friday or Satrurday) * @param {String} day Day of the conference (Thursday, Friday or Satrurday)
@ -74,7 +96,7 @@ module.exports.getDaySchedule = function (day) {
var columns = body.columns; var columns = body.columns;
var rows = body.rows || []; var rows = body.rows || [];
// Group events by the time period in which they occur (for presentation) // Group events by the time period in which they occur (for presentation)
var scheduleByChunk = rows.reduce(function (prev, cur) { var scheduleByTimeSlot = rows.reduce(function (prev, cur) {
var cleanedRow = {}; var cleanedRow = {};
for (var i = 0; i < columns.length; i++) { for (var i = 0; i < columns.length; i++) {
if (cur[i].length > 0) { if (cur[i].length > 0) {
@ -82,48 +104,28 @@ module.exports.getDaySchedule = function (day) {
} }
} }
cleanedRow['uri'] = '/conference/' + cleanedRow.rowid + '/details'; cleanedRow['uri'] = '/conference/' + cleanedRow.rowid + '/details';
var chunk = cleanedRow.Chunk; var timeSlot = cleanedRow.Chunk;
if (typeof prev.chunks[chunk] === 'undefined') { if (typeof prev.timeSlots[timeSlot] === 'undefined') {
prev.chunks[chunk] = [cleanedRow]; prev.timeSlots[timeSlot] = [cleanedRow];
prev.info.push({ prev.info.push({
name: chunk, name: timeSlot,
time: cleanedRow.Start time: cleanedRow.Start
}); });
} else { } else {
prev.chunks[chunk].push(cleanedRow); prev.timeSlots[timeSlot].push(cleanedRow);
} }
return prev; return prev;
}, {chunks: [], info: []}); }, {timeSlots: [], info: []});
// group periods of time by start time
scheduleByChunk.info.sort(function compare (a, b) {
var aAm = (a.time.substr(a.time.length - 1, a.time.length) === 'a') ? true : false;
var bAm = (b.time.substr(b.time.length - 1, b.time.length) === 'a') ? true : false;
var aTime = parseInt(a.time.substr(0, a.time.length - 1));
var bTime = parseInt(b.time.substr(0, b.time.length - 1));
if (aTime < bTime) { scheduleByTimeSlot.info.sort(module.exports.sortTimeSlots);
if (bAm && !aAm) { var schedule = scheduleByTimeSlot.info.map(function (timeSlot) {
return 1; return {
} else { info: timeSlot,
return -1; items: scheduleByTimeSlot.timeSlots[timeSlot.name]
} };
} else {
if (aAm && !bAm) {
return -1;
} else {
return 1;
}
}
}); });
var schedule = [];
for (var i = 0; i < scheduleByChunk.info.length; i++) {
schedule.push({
info: scheduleByChunk.info[i],
items: scheduleByChunk.chunks[scheduleByChunk.info[i].name]
});
}
dispatch(module.exports.setSchedule({ dispatch(module.exports.setSchedule({
chunks: schedule, timeSlots: schedule,
day: day day: day
})); }));
return; return;

View file

@ -1,5 +1,7 @@
var keyMirror = require('keymirror'); var keyMirror = require('keymirror');
var api = require('../mixins/api.jsx').api; var api = require('../mixins/api.jsx').api;
var tokenActions = require('./token.js');
var Types = keyMirror({ var Types = keyMirror({
SET_SESSION: null, SET_SESSION: null,
@ -48,7 +50,7 @@ module.exports.refreshSession = function () {
if (body.banned) { if (body.banned) {
return window.location = body.url; return window.location = body.url;
} else { } else {
dispatch(module.exports.getToken()); dispatch(tokenActions.getToken());
dispatch(module.exports.setSession(body)); dispatch(module.exports.setSession(body));
return; return;
} }

View file

@ -49,7 +49,7 @@ module.exports.setToken = function (token) {
module.exports.setTokenError = function (error) { module.exports.setTokenError = function (error) {
return { return {
type: Types.SET_SESSION_ERROR, type: Types.SET_TOKEN_ERROR,
error: error error: error
}; };
}; };

View file

@ -24,8 +24,8 @@ var ConferenceSchedule = React.createClass({
handleScheduleChange: function (day) { handleScheduleChange: function (day) {
this.props.dispatch(scheduleActions.startGetSchedule(day)); this.props.dispatch(scheduleActions.startGetSchedule(day));
}, },
renderChunkItems: function (chunk) { renderChunkItems: function (timeSlot) {
return chunk.map(function (item) { return timeSlot.map(function (item) {
if (item.Presenter) { if (item.Presenter) {
return ( return (
<a href={item.uri} className="item-url"> <a href={item.uri} className="item-url">
@ -105,12 +105,12 @@ var ConferenceSchedule = React.createClass({
</li> </li>
</SubNavigation> </SubNavigation>
<div className="inner"> <div className="inner">
{this.props.conferenceSchedule.chunks.map(function (chunk) { {this.props.conferenceSchedule.timeSlots.map(function (timeSlot) {
return ([ return ([
<h2 key={chunk.info.name} className="breaking-title"> <h2 key={timeSlot.info.name} className="breaking-title">
<span>{chunk.info.name} {chunk.info.time}</span> <span>{timeSlot.info.name} {timeSlot.info.time}</span>
</h2>, </h2>,
this.renderChunkItems(chunk.items) this.renderChunkItems(timeSlot.items)
]); ]);
}.bind(this))} }.bind(this))}
</div> </div>