diff --git a/src/lib/route.js b/src/lib/route.js new file mode 100644 index 000000000..4eee7620d --- /dev/null +++ b/src/lib/route.js @@ -0,0 +1,15 @@ +module.exports = {}; + +// try to extract classroom token from either of two routes +module.exports.getURIClassroomToken = uriPathname => { + // first try to match /classes/CLASSROOM_ID/register/CLASSROOM_TOKEN + const classRegisterRegexp = /^\/?classes\/\d*\/register\/([a-zA-Z0-9]*)\/?$/; + const classRegisterMatch = classRegisterRegexp.exec(uriPathname); + if (classRegisterMatch) return classRegisterMatch[1]; + // if regex match failed, try to match /join/CLASSROOM_TOKEN + const joinTokenRegexp = /^\/?join\/([a-zA-Z0-9]*)\/?$/; + const joinTokenMatch = joinTokenRegexp.exec(uriPathname); + if (joinTokenMatch) return joinTokenMatch[1]; + // if neither matched + return null; +}; diff --git a/src/views/studentregistration/studentregistration.jsx b/src/views/studentregistration/studentregistration.jsx index 78443686b..35bfee2ed 100644 --- a/src/views/studentregistration/studentregistration.jsx +++ b/src/views/studentregistration/studentregistration.jsx @@ -6,6 +6,7 @@ const React = require('react'); const api = require('../../lib/api'); const injectIntl = require('../../lib/intl.jsx').injectIntl; const intlShape = require('../../lib/intl.jsx').intlShape; +const route = require('../../lib/route'); const Deck = require('../../components/deck/deck.jsx'); const Progression = require('../../components/progression/progression.jsx'); @@ -33,21 +34,8 @@ class StudentRegistration extends React.Component { componentDidMount () { this.setState({waiting: true}); // eslint-disable-line react/no-did-mount-set-state - // set uri and params - let uri; - let params; - if (this.props.classroomId === null || typeof this.props.classroomId === 'undefined') { - // configure for token-only endpoint - uri = `/classtoken/${this.props.classroomToken}`; - } else { - // configure for endpoint expecting classroomId and token - uri = `/classrooms/${this.props.classroomId}`; - params = {token: this.props.classroomToken}; - } - api({ - uri: uri, - params: params + uri: `/classtoken/${this.props.classroomToken}` }, (err, body, res) => { this.setState({waiting: false}); if (err) { @@ -57,7 +45,7 @@ class StudentRegistration extends React.Component { }) }); } - if (res.statusCode === 404) { + if (res.statusCode >= 400) { // TODO: Use react-router for this window.location = '/404'; } @@ -178,20 +166,6 @@ const IntlStudentRegistration = injectIntl(StudentRegistration); // parse either format of student registration url: // "class register": http://scratch.mit.edu/classes/3/register/c0256654e1be // "join token": http://scratch.mit.edu/join/c025r54ebe -let classroomId = null; -let classroomToken = null; -const classRegisterRegexp = /^\/?classes\/(\d*)\/register\/([a-zA-Z0-9]*)\/?$/; -const classRegisterMatch = classRegisterRegexp.exec(document.location.pathname); -if (classRegisterMatch) { - classroomId = classRegisterMatch[1]; - classroomToken = classRegisterMatch[2]; -} else { - const joinTokenRegexp = /^\/?join\/([a-zA-Z0-9]*)\/?$/; - const joinTokenMatch = joinTokenRegexp.exec(document.location.pathname); - if (joinTokenMatch) { - classroomToken = joinTokenMatch[1]; - } -} -const props = {classroomId, classroomToken}; +const props = {classroomToken: route.getURIClassroomToken(document.location.pathname)}; render(, document.getElementById('app')); diff --git a/test/unit/lib/route.test.js b/test/unit/lib/route.test.js new file mode 100644 index 000000000..2acb17599 --- /dev/null +++ b/test/unit/lib/route.test.js @@ -0,0 +1,26 @@ +const route = require('../../../src/lib/route'); + +describe('unit test lib/route.js', () => { + + test('getURIClassroomToken exists', () => { + expect(typeof route.getURIClassroomToken).toBe('function'); + }); + + test('getURIClassroomToken parses URI paths like /classes/21/register/r9n5f5xk', () => { + let response; + response = route.getURIClassroomToken('/classes/21/register/r9n5f5xk'); + expect(response).toEqual('r9n5f5xk'); + }); + + test('getURIClassroomToken parses URI paths like /join/e2dcfkx95', () => { + let response; + response = route.getURIClassroomToken('/join/e2dcfkx95'); + expect(response).toEqual('e2dcfkx95'); + }); + + test('getURIClassroomToken works with trailing slash', () => { + let response; + response = route.getURIClassroomToken('/join/r9n5f5xk/'); + expect(response).toEqual('r9n5f5xk'); + }); +});