mirror of
https://github.com/scratchfoundation/scratch-www.git
synced 2024-11-22 15:17:53 -05:00
refactored uri pathname parsing to library
This commit is contained in:
parent
75f8c6429a
commit
81678b70a7
3 changed files with 45 additions and 30 deletions
15
src/lib/route.js
Normal file
15
src/lib/route.js
Normal file
|
@ -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;
|
||||
};
|
|
@ -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(<IntlStudentRegistration {...props} />, document.getElementById('app'));
|
||||
|
|
26
test/unit/lib/route.test.js
Normal file
26
test/unit/lib/route.test.js
Normal file
|
@ -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');
|
||||
});
|
||||
});
|
Loading…
Reference in a new issue